Currenly we use for browser push notifications following approach:
- Request is sent to FCM server with
dataproperty inside the payload (withoutnotificationproperty), which omits SDK to display the popup message - Inside the client application we use
onMessagecallback to show popup in foreground state andsetBackgroundHandlerinside service worker to show popup when application is in background state.
There were problems with duplicate popups (in case you have multiple background tabs/windows of application), but we managed to fix it using indexation of messages by ID and randomized timeout to show the message (the first timeout that fired indexes message and shows popup, subsequent timeouts see that message was shown already, so popup is not shown).
So, all is OK now with this approach. Almost all...
We've noticed that such notifications/popups are shown only in case there's at least one window/tab opened with our application (does not matter background or foreground). But when we use SDK approach to send push notification (providing notification property inside the request payload), the popup appears even when our application is fully closed, which is really NICE THING!
So, we tried to combine 2 approaches in order to display popups even when application is closed, but we have one problem. The problem arise when you have several windows/tabs of application opened, where one is in foreground and other(s) is/are in background. In this case, popup appears twice, where one is created inside onMessage callback (foreground) and another is created by SDK. We thought that we can use only SDK method, but in this case popup is not shown when application in foreground state (https://firebase.google.com/docs/cloud-messaging/js/receive).
Then we had idea to skip displaying popup inside onMessage callback, in case there's more than one tab/window of our application opened inside the browser, but it seems that it is not possible to find out such information using existing Web API, which is kind of strange thing. There are some workarounds to make some counters of opened tabs/windows using localStorage or IndexedDB, but they are not safe in case browser crash.
Does anyone had such problem? What are your solutions to this?
Aucun commentaire:
Enregistrer un commentaire