gpt4 book ai didi

javascript - Firebase 消息传递返回成功,但 webapp 未收到消息

转载 作者:行者123 更新时间:2023-11-29 23:31:20 29 4
gpt4 key购买 nike

我正在使用 Firebase 云消息传递 API 并将消息发送到网络应用程序。一切似乎都工作正常,发送消息返回成功,但从未调用应该接收消息的 onMessage()。

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

firebase 的响应看起来不错:

{'multicast_ids': [5162553035531497690], '成功': 1, '失败': 0, 'canonical_ids': 0, '结果': [{'message_id': 'https://updates. push.services.mozilla.com/m/...'}], 'topic_message_id': 无}

但是,永远不会调用 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)

最佳答案

我遇到了同样的问题。在我使用自发布的 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);
});

关于javascript - Firebase 消息传递返回成功,但 webapp 未收到消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244235/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com