We have implemented an SDK to ease the process of sending and receiving Push Notification. However, recently we are facing an issue that results in receiving Generic Notification(i.e., This site has been updated in the background) message whenever we send a push message.
After a period of inspection and trial-and-error, we came to the rough conclusion that:
- Whenever we send a Push Notification, the first time around, service worker won't wake up and hang in the
Stoppedstate. As a result,Generic Notificationis shown. In immediate subsequent Push Messages, service worker transitions toRunningstate and receives Push Messages. - The following code responsible for handling
pushevent is not invoked the first time sending Push Message.
@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. Using a wrapper function containing event.waitUntil(), we are awaiting for the promises inside to resolve.
This is the intended behavior. Your service worker is not handling the first
pushevent because your page is still an uncontrolled client for it. Your service worker is not yet in theactivestate.From The Service Worker Lifecycle (a highly recommended read):
To handle the first
pushevent you can either:location.reload()in your service worker registration scriptclients.claim()in your service workerOption 2 overrides the default behavior of the service worker and let it take control of uncontrolled clients (your sw immediately claims the clients).
See also: