admin管理员组文章数量:1387334
I have been trying to show custom notifications when the user gets an FCM background notification but I am getting two notifications each time a user gets a new notification one is the default Firebase SDK and the other is a custom notification from my service worker code.
Is there a way to disable the default notification or hide it as soon as it appeared so that the user can only get the custom notification?
I have tried all the previous solutions but none of them are working as many functions such as SetBackgroundHandler are deprecated. FCM Push notifications arrive twice if the browser is in background
see my code below -
self.addEventListener('push', function(event) {
// console.log('Received a push message', event);
var body,title,tag,icon;
messaging.onBackgroundMessage((payload)=>{
title = payload.notification.title;
body = payload.data.order_id;
icon = "/images/logo.png';
tag = 'simple-push-demo-notification-tag';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
// icon: icon,
tag: tag,
})
);
});
});
I have been trying to show custom notifications when the user gets an FCM background notification but I am getting two notifications each time a user gets a new notification one is the default Firebase SDK and the other is a custom notification from my service worker code.
Is there a way to disable the default notification or hide it as soon as it appeared so that the user can only get the custom notification?
I have tried all the previous solutions but none of them are working as many functions such as SetBackgroundHandler are deprecated. FCM Push notifications arrive twice if the browser is in background
see my code below -
self.addEventListener('push', function(event) {
// console.log('Received a push message', event);
var body,title,tag,icon;
messaging.onBackgroundMessage((payload)=>{
title = payload.notification.title;
body = payload.data.order_id;
icon = "/images/logo.png';
tag = 'simple-push-demo-notification-tag';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
// icon: icon,
tag: tag,
})
);
});
});
Share
Improve this question
asked Apr 6, 2021 at 6:37
Siddhant GuptaSiddhant Gupta
611 silver badge2 bronze badges
2
- did you find a solution? – Pumuckelo Commented Aug 9, 2021 at 8:06
- did you find a solution? – Tabarek Ghassan Commented Jan 16, 2022 at 12:44
1 Answer
Reset to default 7This question puzzled me for 2 days. I read all the informations about firebase messaging and service worker, finally I found a solution now, so I'm so excited to write it down.
You can skip to the end if you just want the conclusion.
By using chrome's console tool, run
getEventListeners(self).push[0]
on firebase-messaging-sw.js, this method can let me know the list of event handlers of 'push' event, which should be the mechanism used by firbase messaging. And I can get the function be run by running
getEventListeners(self).push[0].listener()
. Because there was en error, I jumped to the firebase-messaging.js->sw-controller.ts-> onPush(event: PushEvent), and found out that if the message from firebase contains notification property, this event handler created by firebase will show the notification.
So, the simple way to stop the default notification popup is, don't send message with notification property. (If you test message sending on firebase project console, the message will contain the notification property, so you'd better test sending by another way.)
Then you can do your customized handling by onBackgroundMessage() by now.
本文标签:
版权声明:本文标题:javascript - Is there a way to intercept and disable the default background FCM notification and show custom notification in the 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744567171a2613120.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论