phayes0289's avatar

I Need Help Implementing Apple Push Notifications (APN) for Web Apps (PWA)

’ve been watching several Apple Developer videos on IOS and Mac notifications using Apple Web Push for Safari. As I understand it, once I get it working for Safari, it should seamlessly work for my web app, both on the Mac and IOS (Apps saved to the home screen or dock). They said that the app does not even need to be running once permission is granted.

According to the videos from WWDC, I need a service worker js file that will handle the granting of permissions after a button click. It requires a VAPD_Public_Key, which I know nothing about yet, but I guess I will just Google. In return, the service worker should get a PushSubscriptionObject that has data that I will use to send future push notifications.

What I am looking to do is to send standard and “Critical” alerts to IOS devices (and Mac desktop web apps) to notify Firefighters of an emergency incident. In concept, they will then click on the standard or critical notification (criticals bypass the focus controls). Critical notifications were introduced in iOS 12 and are designed for sending high-priority notifications that you don't want to miss - for example, security system, water leak sensor, and smoke/CO2 alarm alerts. iOS gives special priority to this type of notification. It sounds like I will need special permission to use them. I know of another company already doing something similar so I know it is possible.

I have not seen too much talk about this in relation to Laravel. No useful videos on YouTube unless I am looking for the wrong topic.

I did find the “Laravel APN (Apple Push) Notification Channel” package, but I am confused by the instructions. It refers to a bunch of Apple developer settings such as APN_Bundle_ID and other settings that I did not think were required for APN. For instance, doesn’t APN_Bundle_ID refer to a native app in the Apple store?

Does anybody know of any tutorials I can follow to get something like this working?

Any help will be greatly appreciated.

0 likes
4 replies
LaryAI's avatar
Level 58

Implementing Apple Push Notifications (APN) for web apps, including Progressive Web Apps (PWAs), can be a bit complex due to the various steps involved in setting up the necessary certificates, keys, and service workers. Here's a step-by-step guide to help you get started:

  1. Apple Developer Account: You need an Apple Developer account to access the necessary certificates and keys for APN.

  2. Create a Website Push ID: Log in to your Apple Developer account and create a Website Push ID. This ID is used to uniquely identify your website to APN.

  3. Generate a Certificate: After creating a Website Push ID, you need to create a certificate for your website. This certificate will be used to secure the communication between your server and APN.

  4. Export the Certificate: Export the certificate from the Apple Developer portal in .p12 format and convert it to .pem format using OpenSSL or a similar tool.

  5. Service Worker: Implement a service worker in your web app that handles the registration and management of push notifications. The service worker should listen for the push event and display notifications when they are received.

  6. Request Permission: On the client side, you need to request permission from the user to receive push notifications. This is typically done by calling window.Notification.requestPermission() after a user interaction, like a button click.

  7. VAPID Keys: Generate VAPID (Voluntary Application Server Identification) keys. These keys are used to authenticate your server with the push service. You can generate these using a library like web-push in Node.js.

  8. Subscribe to Push Service: Once permission is granted, use the service worker's pushManager.subscribe() method to subscribe to the push service. This will give you a PushSubscription object that contains endpoint and keys you'll use to send push notifications.

  9. Backend Implementation: On the server side, use the PushSubscription object to send push notifications. If you're using Laravel, you can use the "Laravel APN (Apple Push) Notification Channel" package to help with this. Configure the package with your APN credentials and use it to send notifications.

  10. Handle Critical Alerts: For critical alerts, you will need to apply for an entitlement from Apple. This process involves explaining the need for critical alerts and how they will be used. Once approved, you can send critical alerts by setting the content-available and priority fields in your APN payload.

Here's a basic example of a service worker push event listener:

self.addEventListener('push', function(event) {
  const options = {
    body: event.data.text(),
    // Include any other options you want to set
  };

  event.waitUntil(
    self.registration.showNotification('Notification Title', options)
  );
});

And here's how you might send a notification from Laravel using the APN channel:

use Illuminate\Notifications\Notification;
use NotificationChannels\Apn\ApnChannel;
use NotificationChannels\Apn\ApnMessage;

class EmergencyAlert extends Notification
{
    public function via($notifiable)
    {
        return [ApnChannel::class];
    }

    public function toApn($notifiable)
    {
        return ApnMessage::create()
            ->badge(1)
            ->title('Emergency Incident')
            ->body("There's an emergency incident that requires your attention.");
            // Include any other payload data for critical alerts
    }
}

Remember to replace placeholders with actual data and configure your Laravel application with the correct APN settings.

As for tutorials, you might not find one that covers everything end-to-end, especially with Laravel. However, you can piece together the process by looking at documentation for service workers, the web-push library for VAPID, and the Laravel APN Notification Channel package. You may also want to refer to Apple's official documentation on Web Push Notifications.

Keep in mind that this is a high-level overview and each step involves more detailed implementation that you'll need to adapt to your specific use case.

martinbean's avatar

@phayes0289 You’ve asked about this at least once before, because I remember responding.

You need native apps running on the devices to be able to send native push notifications. And your app will need to have an entitlement approved by Apple in order to send critical alerts, otherwise everyone would just use them to spam users.

1 like
phayes0289's avatar

@martinbean I am sure you are correct. I am sorry about the redundancy. But I am surprised to see this topic not being more important to the builders of Laravel applications. I have found some new info on the topic.

In 2022, announced at WWDC their new web notifications / web push for safari. No native app required. There is an easy to find video about it. https://developer.apple.com/wwdc22/10098

Then, in 2023, Apple announced “Apple Push nNtifications” (APN) for web apps. No native app required again. In fact, they say that no, Apple developer account is required either. https://developer.apple.com/wwdc23/10120

As I understand it, I just need to gain permission through a user gesture, such as a mouse click. Once that happens, a data object with information about the device is sent back to the application for use in sending the notifications.

martinbean's avatar

@phayes0289 They’re web push notifications. They’re notifications that can be sent from PWAs (progressive web apps) that appear as an app notification. You’re not going to be able to issue iOS critical push alerts from a web page. Again, any developer could abuse that functionality and just mark their own spammy notifications as “critical”.

Please or to participate in this conversation.