messaging / incorrect-gcm-sender-id firebase PWA

时间:2018-04-14 09:48:15

标签: javascript push-notification firebase-cloud-messaging progressive-web-apps

我做了一个PWA,我现在想要添加通知。我想使用云消息传递(Firebase),我遇到了一些问题。 我按照教程,我在manifest.json中添加了这一行:

"gcm_sender_id": "My sender ID". 

在我的index.html中,我添加了

<script src = "https://www.gstatic.com/firebasejs/4.1.1/firebase-app.js">
</ script>
<script src = "https://www.gstatic.com/firebasejs/4.1.1/firebase-messaging.js"> <script>
    var config = {
        apiKey: "My api",
authDomain: "example.firebaseapp.com",
databaseURL: "https://example.firebaseio.com",
projectId: "example",
storageBucket: "example.appspot.com",
messagingSenderId: "My sender ID"

    };
    firebase.initializeApp (config);
    const messaging = firebase.messaging ();
 messaging
.requestPermission ()
then (function () {
 console.log ("Notification permission granted.");
 return messaging.getToken ();
})
.then (function (token) {
console.log ("token is:" + token);
})
.catch (function (err) {
console.log ("Unable to get permission to notify.", err);
});
</ Script>

有了这个,我收到以下错误:

  

消息:请将您的网络应用清单'gcm_sender_id'值更改为'103953800507'以使用Firebase消息。 (消息/错误-gcm-sender-id)

我搜索了一下github(https://github.com/realtime-framework/WebPushNotifications),他们说要通过控制台中的内容更改SENDER ID

所以我修改了manifest.json,我收到以下错误

  

获取脚本时收到错误的HTTP响应代码(404)。   无法加载资源:net :: ERR_INVALID_RESPONSE /firebase-messaging-sw.js

     

消息:我们无法注册默认服务工作者。“无法注册ServiceWorker:获取脚本时收到错误的HTTP响应代码(404)。(消息传递/失败 - serviceworker-registration)。

我无法找到我必须做的事情,我承认自己有点失落。

2 个答案:

答案 0 :(得分:0)

现在,您必须将另一个文件添加到您的根目录中,名称为“ firebase-messaging-sw.js”,并且可以为空文件。它可能会修复错误。但您需要稍后更新此文件,以便在Firebase消息传递实施的后续步骤中知道。

答案 1 :(得分:0)

对于遇到此问题的任何人,就像我一样,这就是我放入文件中的内容

self.addEventListener('push', function(event) {
  console.log('[Service Worker] Push Received.');
  //console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);

  const title = 'Push Codelab';
  const options = {
    body: 'Yay it works.',
    icon: 'images/icon.png',
    badge: 'images/badge.png'
  };

  event.waitUntil(self.registration.showNotification(title, options));
});



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


  var firebaseConfig = {
    apiKey: "****",
    authDomain: "****",
    projectId: "****",
    storageBucket: "****",
    messagingSenderId: "****",
    appId: "****",
    measurementId: "****"
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);
  //firebase.analytics();

if (firebase.messaging.isSupported()) {
  firebase.messaging();
  console.log('FB Messaging Supported');
}


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

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

用您自己的来自 Firebase 仪表板的数据替换 ****