Search code examples
javascriptiosfirebase-cloud-messaging

User doesnt get PWA Notifications on iOS using FCM after a couple attempts


I'm using Firebase Cloud Messaging to send Daily notifications to iOS users who have a PWA app installed. I've found that for a given token, I can only successfully send 2 notifications. Beyond that the call will return a success (verified in the logs) but the user never gets the notification. Havent tested it on android yet to confirm if this is an iOS specific issue.

        const message = {
          notification: {
            title: "Practice Reminder",
            body: "Time for your daily fretboard practice!",
          },
          token: data.token,
        };
        getMessaging().send(message)
        .then((response) => {
          // Response is a message ID string.
          console.log('Successfully sent message:', response);
        })
        .catch((error) => {
          console.log('Error sending message:', error);
        });

Solution

  • As of the 2nd of June, the solution suggested here fixes the problem. i.e.: replace the onBackgroundMessage with

    self.addEventListener('push', function(event) {
        console.log('[Service Worker] Push Received.');
        const payload = event.data.json();  // Assuming the payload is sent as JSON
        const notificationTitle = payload.notification.title;
        const notificationOptions = {
            body: payload.notification.body,
            icon: payload.notification.icon,
            image: payload.notification.image,
            badge: payload.notification.badge,
        };
        event.waitUntil(
            self.registration.showNotification(notificationTitle, notificationOptions)
        );
    });