首页 > 解决方案 > Firebase FCM onMessage 触发但因未定义 showNotification 而失败

问题描述

我对firebase FCM有这个问题,试图让它在带有https的普通php网站上工作。

我设法注册服务工作者,订阅并获取令牌。

然后我有另一个隐身chrome窗口将消息投射到这个特定的令牌。

选角成功

Array
(
    [multicast_id] => 6443372871511421810
    [success] => 1
    [failure] => 0
    [canonical_ids] => 0
    [results] => Array
        (
            [0] => Array
                (
                    [message_id] => 0:1619275101769867%e609af1cf9fd7ecd
                )
        )
)

然后我参考我的正常 chrome 窗口,messaging.onMessage 成功触发。但未能完全完成,停在 showNotification..

已经尝试了几个小时,清除缓存,从通知中删除站点以重新订阅,从应用程序选项卡中删除服务工作者......分别在普通浏览器上运行获取通知并在隐身窗口发送通知,仍然无法正常工作。

messaging.onMessage(function(payload) {

    console.log('[handle incoming]', payload);

    const notificationTitle = 'ON MESSAGE TITLE';
    const notificationOptions = {
        body: 'Data Message body',
        icon: 'https://c.disquscdn.com/uploads/users/34896/2802/avatar92.jpg',
        image: 'https://c.disquscdn.com/uploads/users/34896/2802/avatar92.jpg'
    };

    return self.registration.showNotification(notificationTitle, notificationOptions);  //FAIL here
});

firebase.js:79 Uncaught (in promise) TypeError: Cannot read property 'showNotification' of undefined
    at rt.onMessageCallback (firebase.js:79)
    at rt.<anonymous> (window-controller.ts:74)
    at tslib.es6.js:102
    at Object.next (tslib.es6.js:83)
    at tslib.es6.js:76
    at new Promise (<anonymous>)
    at d (tslib.es6.js:72)
    at rt.messageEventListener (window-controller.ts:59)
    at ServiceWorkerContainer.<anonymous> (window-controller.ts:51)

[handle incoming] 有效载荷成功打印如下

有效载荷是这样的

{来自:“355031145915”,优先级:“正常”,通知:{…},collapse_key:“do_not_collapse”} collapse_key:“do_not_collapse”来自:“355031145915”通知:{标题:“新文章”,正文:“Firebase Cloud使用 JavaScript 的 Web 消息传递”,图标:“https://c.disquscdn.com/uploads/users/34896/2802/avatar92.jpg”,click_action:“http://localhost/test”} 优先级:“正常” 原型:对象

我使用的版本是

importScripts("https://www.gstatic.com/firebasejs/8.4.2/firebase-app.js"); importScripts("https://www.gstatic.com/firebasejs/8.4.2/firebase-messaging.js");

我泵入的配置是

  var firebaseConfig = {
    apiKey: "xxx",
    authDomain: "xxx",
    projectId: "xxx",
    storageBucket: "xxx",
    messagingSenderId: "xxx",
    appId: "xxx"
  };
  firebase.initializeApp(firebaseConfig);

有什么办法可以解决这个 showNotification 未定义问题?

标签: javascriptphpfirebasefirebase-cloud-messaging

解决方案


推荐阅读