我正在尝试使网站现代化以成为 PWA。一部分是推送通知。到目前为止一切顺利,我添加了带有 gcm_sender_id 的 list 并订阅了推送服务。我尝试了自行生成的 vapid-keys 或 google firebase 提供的 key 。片段:
var reg;
navigator.serviceWorker.ready
.then(function(swreg) {
console.log('SW ready');
reg = swreg;
return swreg.pushManager.getSubscription();
})
.then(function(sub) {
console.log('SW ready.then');
if (sub === null) {
var vapidPublicKey = 'BEP1z-pU7KfRqXjQGbB_af7YydA1Hxzb3EWDYyb5q44YEflE8RaT0wISdJJnvQlzBkVuC4CupAqU2wm0SxCjxpk';
// taken^^ from the fcm console under cloud messaging web configuration
var convertedVapidPublicKey = urlBase64ToUint8Array(vapidPublicKey);
console.log(convertedVapidPublicKey);
var retval = reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: convertedVapidPublicKey
});
return retval;
}
return sub;
})
.then(function(newSub) {
console.log('Received PushSubscription: ', JSON.stringify(newSub));
return newSub
})
.then(function(res) {
console.log('SW ready.then.then.then');
if (res.ok) {
displayConfirmNotification();
}
})
.catch(function(err) {
console.log(err);
});
我订阅了:
Received PushSubscription: {"endpoint":"https://fcm.googleapis.com/fcm/send/eU1Mfq-f1So:APA91bEds1D8pio29fNYdMoAQfd_zXcfai7OR_JzppN-Dq5hMhv3_tI9HZHlATBuwBcaPYC6bkmwMXm6IVZ1T83o8r3l8Dyyvxlvy191MjVwc1eKsy8lD1E2sGNaKrZrb5a2qDr9vUi9","expirationTime":null,"keys":{"p256dh":"BLiNeIZqBMTskRwnV5YCtdQFAcbj_-l2fyhOBpcRSOklLy7iv0Ru0XHjJJqOauHeYWk_9rpAjM7lVyVr7_oGHyE","auth":"yXHXHjFu0EiXRXjN8KMEyA"}}
到目前为止一切顺利。据我了解这个过程,浏览器(在本例中是 Android 上的 Chrome)联系谷歌浏览器服务器和订阅文件。使用的 vapid key 使 firebase 能够联系 chrome 服务器,然后通过专有代码触发通知,这因浏览器而异。
现在我尝试实际使用我的订阅:
$ curl "https://fcm.googleapis.com/fcm/send/eU1Mfq-f1So:APA91bEds1D8pio29fNYdMoAQfd_zXcfai7OR_JzppN-Dq5hMhv3_tI9HZHlATBuwBcaPYC6bkmwMXm6IVZ1T83o8r3l8Dyyvxlvy191MjVwc1eKsy8lD1E2sGNaKrZrb5a2qDr9vUi9" --request POST --header "TTL: 60" --header "Content-Length: 0" --header "Authorization: key=[server key from fcm console under cloud messaging server key (first entry)"
the key in the authorization header does not correspond to the sender ID used to subscribe this user. Please ensure you are using the correct sender ID and server Key from the Firebase console.
我更换了几次 key 。没有什么改变。 Google 对这个字符串一无所知。
最佳答案
我有 node.js express、postgres、angular 8 应用程序。
我通过在 manifest.webmanifest 文件(或 manifest.json 我还使用了 firebase 生成的公钥和私钥
您的 gcm_sender_id 是您在 google cloud 中的项目 ID 或 firebase 发件人 ID
关于push-notification - FCM Firebase 推送消息发送者 ID 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57295739/