An assignment I was really looking forward to was making a Progressive Web App (PWA). This way you can "install" a website as if it could be a native app. When you then launch this "app", it will open in a separate window. PWA's also have more possibilities when it comes to permissions. For instance camera permissions, or the permission to send notifications. The first workshop to make a PWA was mainly getting the right files in the right place. And as there are not really different ways to do this, these steps were mostly copy-paste from the CodePen from Maikel. However, instead of trying to make a sample site he prepared into a PWA, I immediately wanted to try it our on my portfolio website. At first I had some trouble getting the manifest active, but that was because I didn't link it properly in the html. At first, the teacher thought it was because I had my manifest file in a subfolder, and that this would never work. But after proper linking in the
index.html, everything worked, much to the surprise of the teacher.
A couple days later, we would learn how to send notifications. The way these notifications basically work is that when the website loads, the browser knows that the website has notification options, and asks the user if they allow this. Any website can ask you to receive notifications when you load the page, except on iOS. On iOS, a user first has to add the website as PWA, and then a user input (like clicking a button) is needed to prompt the user if they want to allow notifications. So besides implementing the standard prompt into my portfolio website, I also had to implement a button which would trigger the same script, just for it to work on an iOS PWA. I eventually had it working well, so that if you haven't given permission yet, you can click the little bell icon in the top right corner, to prompt the user with a permission request again.
But now, something that went a little bit less smooth. The actual notification. We had to generate a vapid key, and copy-paste it in a couple places, then run a JS script to send a notification. This quickly worked on my laptop. However, even when I gave permission to my iOS PWA, it didn't display any kind of message. So I first started looking if there were some Apple-like quirks that limit or change the way notifications can be sent. I couldn't find anything specific about this, as notifications should just be possible since iOS 16.4. I went back to the script that would send the message out, and saw that it said "
-- Message NOT sent to https://web.push.apple.com/(...)" in the terminal, without giving me a reason. I checked the code itself and saw that there was a "
.catch(error => { console.log(`-- Message NOT sent to (...)`);". I then thought, why not include the error message it catches anyway in the message it gives in the terminal? So I added a little line where it also includes the error message, and after running the script again, I knew that the reason was "
WebPushError: Received unexpected response code". I copy-pasted this in Google, included iOS in the search result, and
this was the first result that popped up. I then found out that the information from the vapid key is formatted in a way that Apple's notification service doesn't recognize. Where the directly copy-pasted line was "
mailto: <jannes2001@outlook.com>", it needed to be formatted like "
mailto:jannes2001@outlook.com", without brackets or spaces. After changing this one line, everything worked and my iPhone also got notifications from the PWA. I also edited the service worker file to not check if the notification has an empty title or message, as I also wanted to try what happened when I don't enter anything in the send script. Then it will just display a generic empty notification, depending on the device.