且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当应用程序处于前台时,Angular 8 Firebase推送通知不起作用

更新时间:2023-02-27 08:52:44

您需要手动触发它.

收到消息后,您只需对其进行记录,然后将消息转到下一个消息:

When you receive you only logging it, and let the message go to the next one:

this.angularFireMessaging.messages.subscribe(
(payload) => {
    console.log("new message received. ", payload);
    this.currentMessage.next(payload);})

您需要触发服务人员的通知:

You need to trigger the notification from service worker:

this.angularFireMessaging.messages.subscribe(
(payload) => {
    console.log("new message received. ", payload);
    const NotificationOptions = {
            body: payload.notification.body,
            data: payload.data,
            icon: payload.notification.icon
          }
          navigator.serviceWorker.getRegistration('/firebase-cloud-messaging-push-scope').then(registration => {
            registration.showNotification(payload.notification.title, NotificationOptions);
          });
    this.currentMessage.next(payload);})

然后添加firebase-messaging-sw.js:

Then add in firebase-messaging-sw.js:

self.addEventListener('notificationclick', function(event) {
    event.notification.close();
    event.waitUntil(self.clients.openWindow(event.notification.data.url));
});