FCM Firebase推送消息发送者 ID 不匹配

3

我正在尝试将一个网站现代化以成为PWA。其中一部分是推送通知。到目前为止,一切都很顺利,我添加了带有gcm_sender_id的清单,并订阅了推送服务。我尝试使用自动生成的vapid-keys或谷歌Firebase提供的vapid-keys。代码片段:

   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"}}

目前为止一切顺利。据我所理解的流程,浏览器(在此情况下是安卓上的Chrome)与谷歌Chrome服务器联系并存储了订阅信息。使用的vapid密钥使得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.

我已经几次更换了钥匙,但情况并没有改变。谷歌上关于这个字符串的信息也很少。

似乎使用curl触发推送通知无效。我现在使用一个PHP库来实现,已经放弃了FCM。 - MPW
1个回答

0

我有一个使用node.js express、postgres和angular 8的应用程序。

我通过在manifest.webmanifest文件(或manifest.json)中添加"gcm_sender_id"来使其工作。 我还使用了Firebase生成的公钥和私钥。

你的gcm_sender_id是你在Google Cloud或Firebase中的项目ID或发送者ID。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接