We have introduced an SDK to streamline the process of sending and receiving Push Notifications. However, a recent issue has emerged where we receive a Generic Notification
message (e.g., indicating that the site has been updated in the background) whenever a push message is sent.
After extensive investigation and trial-and-error, our initial finding suggests that:
- Upon sending a Push Notification for the first time, the service worker fails to activate immediately and remains in a
Stopped
state. This results in the display of aGeneric Notification
. Subsequent push messages prompt the service worker to transition to aRunning
state and successfully receive the notifications. - The Piece of code responsible for managing the
push
event does not execute when the first Push Message is sent.
@serviceWorkerEvent('push')
public static async onPushEvent(context: Context, event: PushEvent) {
context.api.pushDelivered({
// Sending Analytics
}).catch(e => log.error('e'));
const notificationOptions: NotificationOptions = {
// Notification Options
};
const notifTitle = notificationData.title || '';
await (await context.serviceWorker.registration).showNotification(notifTitle, notificationOptions);
await context.workerMessenger.broadcast(WORKER_MESSAGE_TYPES.PUSH_RECEIVED, notificationData);
}
P.S. By utilizing a wrapper function incorporating event.waitUntil()
, we ensure that the promises inside are resolved before proceeding.