admin管理员组文章数量:1130349
Trying to implement web push notifications using FCM.
I am able to receive push message with payload on browser using firebase cloud messaging library.
Below is a javascript code snippet.
<script src=".5.1/firebase.js"> </script>
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.onMessage(function(payload){
console.log('onMessage',payload);
});
How to capture events like click,close,show,etc ?
Trying to implement web push notifications using FCM.
I am able to receive push message with payload on browser using firebase cloud messaging library.
Below is a javascript code snippet.
<script src="https://www.gstatic./firebasejs/3.5.1/firebase.js"> </script>
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.onMessage(function(payload){
console.log('onMessage',payload);
});
How to capture events like click,close,show,etc ?
Share Improve this question edited Oct 27, 2016 at 6:45 Suren Srapyan 68.7k14 gold badges125 silver badges117 bronze badges asked Oct 27, 2016 at 6:44 bijalscmbijalscm 4453 gold badges5 silver badges14 bronze badges1 Answer
Reset to default 7Notification Open
You can listen for notification clicks for Data payloads by adding the following to your firebase-messaging-sw.js file:
self.addEventListener('notificationclick', function(event) {
event.notification.close();
// Do something as the result of the notification click
});
Notification Close
You can listen for notification close events with:
self.addEventListener('notificationclose', function(event) {
// Do something as the result of the notification close
});
Note: event.waitUntil()
You should be aware that to ensure you code has time to plete running, you need to pass a promise into event.waitUntil() that resolves when your code is finished, for example:
self.addEventListener('notificationclick', function(event) {
event.notification.close();
const myPromise = new Promise(function(resolve, reject) {
// Do you work here
// Once finished, call resolve() or reject().
resolve();
});
event.waitUntil(myPromise);
});
You'll know when the notification is shown if it's a data payload because you need to call self.registration.showNotification() in your own code, like so:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
You can't detect when a notification is displayed for a "notification" payload as the SDK handles both displaying the notification and behavior when it's clicked.
Code snippets from:
- firebase.google./docs/cloud-messaging/js/
- web-push-book.gauntface.
Trying to implement web push notifications using FCM.
I am able to receive push message with payload on browser using firebase cloud messaging library.
Below is a javascript code snippet.
<script src=".5.1/firebase.js"> </script>
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.onMessage(function(payload){
console.log('onMessage',payload);
});
How to capture events like click,close,show,etc ?
Trying to implement web push notifications using FCM.
I am able to receive push message with payload on browser using firebase cloud messaging library.
Below is a javascript code snippet.
<script src="https://www.gstatic./firebasejs/3.5.1/firebase.js"> </script>
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.onMessage(function(payload){
console.log('onMessage',payload);
});
How to capture events like click,close,show,etc ?
Share Improve this question edited Oct 27, 2016 at 6:45 Suren Srapyan 68.7k14 gold badges125 silver badges117 bronze badges asked Oct 27, 2016 at 6:44 bijalscmbijalscm 4453 gold badges5 silver badges14 bronze badges1 Answer
Reset to default 7Notification Open
You can listen for notification clicks for Data payloads by adding the following to your firebase-messaging-sw.js file:
self.addEventListener('notificationclick', function(event) {
event.notification.close();
// Do something as the result of the notification click
});
Notification Close
You can listen for notification close events with:
self.addEventListener('notificationclose', function(event) {
// Do something as the result of the notification close
});
Note: event.waitUntil()
You should be aware that to ensure you code has time to plete running, you need to pass a promise into event.waitUntil() that resolves when your code is finished, for example:
self.addEventListener('notificationclick', function(event) {
event.notification.close();
const myPromise = new Promise(function(resolve, reject) {
// Do you work here
// Once finished, call resolve() or reject().
resolve();
});
event.waitUntil(myPromise);
});
You'll know when the notification is shown if it's a data payload because you need to call self.registration.showNotification() in your own code, like so:
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});
You can't detect when a notification is displayed for a "notification" payload as the SDK handles both displaying the notification and behavior when it's clicked.
Code snippets from:
- firebase.google./docs/cloud-messaging/js/
- web-push-book.gauntface.
本文标签:
版权声明:本文标题:javascript - How to get push message events like click,close,show with firebase cloud messaging - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1741438807a1869031.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论