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
Stopped
state. As a result,Generic Notification
is shown. In immediate subsequent Push Messages, service worker transitions toRunning
state and receives Push Messages. - The following code responsible for handling
push
event 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.