Reputation: 309
I have a service worker, which is capable of receiving the push event from our server, it's all working so far. The current version of Chrome does not support data payloads within the received notification. Therefore, I need to fetch the info about the notification (title, text, ..) from the server after the push event occurred.
To ensure that the user does not receive the same notification twice, I save the IDs of the 'pending' notifications, which he has not received yet in an array. Everytime the service worker pings the server, the last item from the array is fetched and removed from the array.
The problem is, that if there is any exception thrown during the sending or in the service worker while fetching the notification, the service worker is one notification behind, which means that e.g. the array always contains 2 items if there is only one new pending notification.
How should I handle this?
Upvotes: 0
Views: 269
Reputation: 212
As soon as the push
event has been catched by the ServiceWorker, the notification is considered as sent for GCM ; regardless of the output (i.e. the well fetched notification has been displayed, or an error occurred during the fetching process).
Nevertheless, as you are fetching each notification content from your server, you can handle a 'fetched' flag server-side. In this way, a fetch could return n payloads, and then your ServiceWorker can make n self.registration.showNotification()
in a single push
event.
Finally, if an error occurred while sending notification to GCM, i.e. an Internal Server Error
, you have to retry the request to GCM. However, if your request was a success, but your ServiceWorker never received a push
event, then you have to consider your notification has lost...
Upvotes: 2