【问题标题】:Firebase Web Push background message is never displayedFirebase Web Push 背景消息永远不会显示
【发布时间】:2018-12-03 10:18:34
【问题描述】:

我在我们的网络应用程序中使用 Firebase 网络推送通知。

当应用程序未激活或未打开时,从 FCM 发送的消息永远不会显示。没有产生错误。

前端代码:

<script src="https://www.gstatic.com/firebasejs/5.6.0/firebase.js"></script>
    <script>
        MsgElem = document.getElementById("msg")
        TokenElem = document.getElementById("token")
        NotisElem = document.getElementById("notis")
        ErrElem = document.getElementById("err")
        // Initialize Firebase
        // TODO: Replace with your project's customized code snippet
        var config = {
            apiKey: "<apikey>",
            authDomain: "<app>.firebaseapp.com",
            databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
            storageBucket: "<app>.appspot.com",
            messagingSenderId: "<id>",
        };
        firebase.initializeApp(config);

        const messaging = firebase.messaging();
        messaging
            .requestPermission()
            .then(function () {
                MsgElem.innerHTML = "Notification permission granted." 
                console.log("Notification permission granted.");

                // get the token in the form of promise
                return messaging.getToken()
            })
            .then(function(token) {
                TokenElem.innerHTML = "token is : " + token
            })
            .catch(function (err) {
                ErrElem.innerHTML =  ErrElem.innerHTML + "; " + err
                console.log("Unable to get permission to notify.", err);
            });

        messaging.onMessage(function(payload) {
            console.log("Message received. ", payload);
            NotisElem.innerHTML = NotisElem.innerHTML + JSON.stringify(payload) 
        });
    </script>

firebase-messaging-sw.js

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

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

    firebase.initializeApp ({'messagingSenderId': 'id'});

    const messaging = firebase.messaging();

    messaging.setBackgroundMessageHandler (function(payload) {

    console.log('[firebase-messaging-sw.js] Received background message ', payload);

     const data = payload.data;
     const notificationTitle = data.title;
     const notificationOptions = {
body: data.body
     };

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

FCM 请求:

 {
      "to":"ids",
      "data":{
      "body":"clicktoaction",
      "title":"Simplext Notification"
      }
    }

无论应用程序是在前台还是在后台,都会调用事件 messing.onMessage(function(payload)。

我尝试将 FCM 请求从“数据”更改为“通知”。它仍然是一样的。

我正在使用 Chrome v 70。

【问题讨论】:

    标签: javascript firebase firebase-cloud-messaging


    【解决方案1】:

    我终于发现firebase客户端应用程序应该在https下才能让serviceworker工作。

    【讨论】:

      猜你喜欢
      • 2011-09-11
      • 2019-02-22
      • 2021-03-31
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-10
      • 2015-07-24
      相关资源
      最近更新 更多