Firebase云消息传递网站未收到测试消息

时间:2019-09-28 13:26:33

标签: javascript firebase firebase-cloud-messaging

我正在尝试使用Firebase云消息传递进行基本概念验证,并且接收消息时遇到痛苦,即使应用程序处于前台,发送消息时也不会发生任何事情,尽管我不这样做,onMessage事件也不会返回任何内容从邮递员或fcm控制台发送所有内容似乎没有任何错误。

我还注意到chrome:// gcm-internals /的状态为“正在连接”

这是我的app.js,但对我来说,这似乎与gcm状态有关,后者应显示“已连接”

var firebase = require("firebase/app");
require('firebase/messaging');

// Your web app's Firebase configuration
var firebaseConfig = {
    apiKey: "AIzaSyCbrs6uvQQ5s6kAp6YfvDzsds_CfMt3-hA",
    authDomain: "test-flarum-fcm.firebaseapp.com",
    databaseURL: "https://test-flarum-fcm.firebaseio.com",
    projectId: "test-flarum-fcm",
    storageBucket: "",
    messagingSenderId: "977636469573",
    appId: "1:977636469573:web:76e2e191f02f8923df6c2c"
};
// Initialize Firebase
var app = firebase.initializeApp(firebaseConfig);

// Retrieve Firebase Messaging object.
var messaging = firebase.messaging();
//console.log(messaging);

console.log('myToken : '+getToken());

Notification.requestPermission().then((permission) => {

    if (permission === 'granted') {
        console.log('Notification permission granted.');

        //getMyToken();
        if(isTokenSentToServer()){
            console.log("token already saved")

        }else{
            // TODO(developer): Retrieve an Instance ID token for use with FCM.
            getMyToken();
        }

    } else {
        console.log('Unable to get permission to notify.');
        setTokenSentToServer(false);
    }
});

// Get Instance ID token. Initially this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
function getMyToken() {
    messaging.getToken().then((currentToken) => {
        if (currentToken) {
            console.log(currentToken);
            //sendTokenToServer(currentToken);//@todo
            //updateUIForPushEnabled(currentToken);
            saveToken(currentToken);
            setTokenSentToServer(true);
        } else {
            // Show permission request.
            console.log('No Instance ID token available. Request permission to generate one.');
            // Show permission UI.
            //updateUIForPushPermissionRequired();//@todo
            //setTokenSentToServer(false); //@todo
            setTokenSentToServer(false);
        }
    }).catch((err) => {
        console.log('An error occurred while retrieving token. ', err);
        //showToken('Error retrieving Instance ID token. ', err);
        setTokenSentToServer(false);
    });
}

// Callback fired if Instance ID token is updated.
messaging.onTokenRefresh(() => {
    messaging.getToken().then((refreshedToken) => {
      console.log('Token refreshed.');
      // Indicate that the new Instance ID token has not yet been sent to the
      // app server.
      setTokenSentToServer(false);
      // Send Instance ID token to app server.
      //sendTokenToServer(refreshedToken);
      // ...
    }).catch((err) => {
      console.log('Unable to retrieve refreshed token ', err);
      //showToken('Unable to retrieve refreshed token ', err);
    });
  });

function setTokenSentToServer(sent) {
    window.localStorage.setItem('sentTokenToServer', sent ? 1 : 0);
}

function isTokenSentToServer(){
    return window.localStorage.getItem('sentTokenToServer') == 1;
}
function saveToken(token){
   myToken = token;
   window.localStorage.setItem('myToken', token);
}

function getToken(){
    return window.localStorage.getItem('myToken');
}

messaging.onMessage((payload) => {
    console.log('Message received. ', payload);
    // ...
});

感谢您的帮助

[update]我解决了gcm状态的问题,但是尽管邮递员的发送结果似乎还可以,但这种方法的工作效果并不好

{
    "multicast_id": 7270949329343339591,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1569712574121124%e609af1cf9fd7ecd"
        }
    ]
}

0 个答案:

没有答案