【问题标题】:Firebase messaging returns success, but webapp not receiving messageFirebase 消息返回成功,但 webapp 未收到消息
【发布时间】:2018-04-24 22:46:46
【问题描述】:

我正在使用 Firebase 云消息传递 API 并向网络应用发送消息。一切似乎都正常,发送消息返回成功,但应该接收消息的 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)

【问题讨论】:

  • 嗯,什么也没做,但一天后运行它,一切正常?
  • 嗨,您是否设法弄清楚它为什么在第二天起作用?我自己也在为同样的问题苦苦挣扎,但在 Android 上

标签: javascript firebase firebase-cloud-messaging


【解决方案1】:

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

  • firebase-messaging-sw.js、manifest.json 位于根目录下且可用

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

  • firebase-messaging-sw.js 的代码如:
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js');

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

firebase.initializeApp({
  messagingSenderId: "830*********"
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();


messaging.setBackgroundMessageHandler(function (payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  // Customize notification here
  var notificationTitle = 'Background Message Title';
  var notificationOptions = {
    body: 'Background Message body.',
    icon: '/firebase-logo.png'
  };

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 2013-08-28
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    相关资源
    最近更新 更多