【问题标题】:Sending & Receiving Push Notifications across major browsers & mobile devices跨主要浏览器和移动设备发送和接收推送通知
【发布时间】:2017-07-24 21:54:33
【问题描述】:

我注册了一个 Google Cloud Services 帐户,创建了一个新的 Firebase 项目,并下载了我的服务凭据 JSON 文件。

按照本指南: https://firebase.google.com/docs/cloud-messaging/js/client

我将此添加到我的网络客户端 html 中(为了获取客户端注册令牌并接受推送通知):

<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js">
</script>
<script>
firebase.initializeApp({
  apiKey: "_APIKEY_",
  authDomain: "_ID_.firebaseapp.com",
  databaseURL: "https://_ID_.firebaseio.com",
  projectId: "_ID_",
  storageBucket: "_ID_.appspot.com",
  messagingSenderId: "_SENDERID_"
});
/* Is the API Key supposed to be public-facing? */

const messaging = firebase.messaging();
messaging.requestPermission().then(function() {
  console.log('Notification permission granted.');

  messaging.getToken().then(function(currentToken) {
    if (currentToken) {
      console.log(currentToken)
    } else {
      console.log('No Instance ID token available. Request permission to generate one.');
    }
  }).catch(function(err) {
    console.log('An error occurred while retrieving token. ', err);
  });
}).catch(function(err) {
  console.log('Unable to get permission to notify.', err);
});

messaging.onTokenRefresh(function() {
  messaging.getToken().then(function(refreshedToken) {
    console.log(refreshedToken)
  }).catch(function(err) {
    console.log('Unable to retrieve refreshed token ', err);
  });
});
</script>

我抓取了客户端生成的注册令牌并添加了一个 service worker (firebase-messaging-sw.js):

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

firebase.initializeApp({
  'messagingSenderId': '_ID_'
});

const messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(function(payload) {
  console.log('[firebase-messaging-sw.js] Received background message ', payload);
  const notificationTitle = 'Background Message Title';
  const notificationOptions = {
    body: 'Background Message body.',
    icon: '_ICON_'
  };

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

然后,为 Firebase 管理员下载 npm 模块并使用客户端令牌设置基本推送通知测试模板:

  var admin = require("firebase-admin");
  var serviceKey = require("./_KEY_.json");

  admin.initializeApp({
    credential: admin.credential.cert(serviceKey),
    databaseURL: "https://_PROJECTID_.firebaseio.com"
  });

  var registrationToken = "_TOKEN_";

  var notification = {
    data: {
      msg:"Hello!"
    }
  };

  admin.messaging().sendToDevice(registrationToken, notification)
    .then(function(response) {
      console.log("Successfully sent message:", response);
    })
    .catch(function(error) {
      console.log("Error sending message:", error);
    });

现在,node.js 告诉我它"Successfully sent message",但我在 Chrome 网络客户端中没有收到任何内容。在 Safari 中,我看到错误:This browser doesn't support the API's required to use the firebase SDK.

对我来说,通过主要浏览器和移动设备上的推送通知简单地向单个客户发送消息的最简单方法是什么?

【问题讨论】:

标签: push-notification service-worker


【解决方案1】:

Safari 尚不支持 FCM。 See here

现在为什么你没有收到消息,首先你必须了解实际发生了什么。

客户端正在浏览器中生成令牌。(这就是 generateToken 方法的作用)并且 Firebase 管理员正在运行 fcm 服务器。

但 Firebase 服务器不知道您的客户端。您的客户端必须订阅您要从中接收通知的管理服务器。

要订阅服务器,您必须使用您的服务器密钥发出发布请求。 see this

【讨论】:

  • 如果我想兼容 Safari、Chrome、Firefox 等,我可以用什么来代替 FCM?
  • Socket.io 在这种情况下是最好的选择
猜你喜欢
  • 2023-03-22
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
相关资源
最近更新 更多