【问题标题】:Firebase cloud message/notifications are not receiving. JSFirebase 云消息/通知未收到。 JS
【发布时间】:2018-03-13 11:58:05
【问题描述】:

我正在尝试使用云功能将 FCM 发送到我的 React 应用程序。 云功能正在执行,但客户端未收到通知。

这里是云功能代码。

exports.sendPush = functions.database.ref('/settings2').onWrite(event => {
    let projectStateChanged = false;
    let projectCreated = false;
    let projectData = event.data.val();
    if (!event.data.previous.exists()) {
        projectCreated = true;
    }
    if (!projectCreated && event.data.changed()) {
        projectStateChanged = true;
    }
    let msg = 'A project state was changed';
        if (projectCreated) {
            msg = `The following new project was added to the project: ${projectData.title}`;
        }
    tokens.push("fIGxxxxGtDGxxxx DEVICE Token");
    let payload = {
        notification: {
            title: 'Firebase Notification',
            body: 'This Is Message',
            sound: 'default',
            badge: '1'
        }
    };
    return admin.messaging().sendToDevice(tokens, payload);
});

这里是云函数的日志

这是我客户端的代码:

messaging.onMessage(function(payload) {
   console.log("Message received. ", payload);
      // ...
});

没有任何错误,但控制台中没有任何内容。我必须对 Service Worker 文件做些什么吗?

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript reactjs firebase firebase-cloud-messaging google-cloud-functions


    【解决方案1】:

    我的 firebase-messaging-sw.js 文件存在问题。我已经通过以下代码解决了这个问题。

    importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
    importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');
    
    var config = {
        apiKey: "AIzaSyDtg4aQMQW67Jla1nUzrTTSEhVqpUeqKXI",
        authDomain: "payxxxxxxx.firebaseapp.com",
        databaseURL: "https://pxyxxxxx.firebaseio.com",
        projectId: "pxxoll-pxxxs-fxxx",
        storageBucket: "pxxx-pxxx-fxxxx1.apxxxot.com",
        messagingSenderId: "2xxxx5xxx"
      };
      firebase.initializeApp(config);
    
    
    const messaging = firebase.messaging();
    

    【讨论】:

      【解决方案2】:

      对于可能遇到此帖子并遇到此问题的任何新人,特别是如果您从在线复制和粘贴某些内容,请确保检查 importScripts 版本并确保其版本与您的 package.json 文件中的版本相同。

      示例 目前这个包版本是8.3.1

      所以更新

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

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

      【讨论】:

      • 这是一个合法的解决方案,在刷新同一个该死的页面 4 小时后帮助解决了我的问题。 ++
      猜你喜欢
      • 2017-12-24
      • 1970-01-01
      • 2016-09-17
      • 2021-11-20
      • 2020-07-16
      • 1970-01-01
      • 2020-06-16
      • 1970-01-01
      • 2019-02-06
      相关资源
      最近更新 更多