Firebase消息传递返回成功,但webapp未接收消息

时间:2017-11-12 00:22:32

标签: javascript firebase firebase-cloud-messaging

我使用Firebase云消息传递API并向webapp发送消息。一切似乎工作正常,发送消息返回成功,但是应该接收消息的onMessage()永远不会被调用。

chrome://gcm-internals中的Chrome内部的GCM连接已连接。 我也试过Firefox,同样的事情。

来自firebase的回复看起来不错:

{'multicast_ids': [5162553035531497690], 'success': 1, 'failure': 0, 'canonical_ids': 0, 'results': [{'message_id': 'https://updates.push.services.mozilla.com/m/...'}], 'topic_message_id': None}

然而,webapp中的onMessage从未被调用过,我也不知道为什么,因为我没有得到错误,一切似乎都很好。

这是我的JS代码:

<link rel="manifest" href="/cryptoalert/manifest.json">

<script src="https://www.gstatic.com/firebasejs/4.6.2/firebase.js"></script>
<script>
  // Initialize Firebase
  var config = {
    apiKey: "...",
    projectId: "...",
    messagingSenderId: "..."
  };
  firebase.initializeApp(config);

  const messaging = firebase.messaging();

  messaging.requestPermission()
    .then(function() {
      console.log('Notification permission granted.');
      // TODO(developer): Retrieve an Instance ID token for use with FCM.
      // ...
    })
    .catch(function(err) {
      console.log('Unable to get permission to notify.', err);
    });


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

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

    });
</script>

这是服务器代码:

from pyfcm import FCMNotification

push_service = FCMNotification(api_key="...")


# Send to multiple devices by passing a list of ids.
#Get registration ID from
registration_ids = ['from console.log(currentToken)']
message_title = "Uber update"
message_body = "Hope you're having fun this weekend, don't forget to check today's news"
result = push_service.notify_multiple_devices(registration_ids=registration_ids, message_title=message_title, message_body=message_body)

print(result)

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。在我从具有自发SSL的本地主机发布到具有可信SSL的真实站点之后,它得到了解决。请检查以下步骤:

  • firebase-messaging-sw.js,manifest.json在root中可用

  • 有一个真正的SSL(不是自信的)

  • firebase-messaging-sw.js代码如:

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

    importScripts(&#39; https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js&#39);

    firebase.initializeApp({     messagingSenderId:&#34; 830 *********&#34; });

    //检索Firebase Messaging的实例,以便它可以处理后台 //消息 const messaging = firebase.messaging();

    messaging.setBackgroundMessageHandler(function(payload){     console.log(&#39; [firebase-messaging-sw.js]收到背景消息&#39;,payload);     //在此处自定义通知     var notificationTitle =&#39;背景消息标题&#39 ;;     var notificationOptions = {         body:&#39;背景信息正文。&#39;,         icon:&#39; /firebase-logo.png'     };

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

    });

相关问题