Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 如何在Chrome扩展中的service worker中接收FCM推送通知_Firebase_Google Chrome Extension_Push Notification_Notifications_Firebase Cloud Messaging - Fatal编程技术网

Firebase 如何在Chrome扩展中的service worker中接收FCM推送通知

Firebase 如何在Chrome扩展中的service worker中接收FCM推送通知,firebase,google-chrome-extension,push-notification,notifications,firebase-cloud-messaging,Firebase,Google Chrome Extension,Push Notification,Notifications,Firebase Cloud Messaging,我正在从Firebase后端向Chrome扩展发送FCM推送通知。当它处于焦点时,我成功地在我的Chrome扩展中显示它。我想让它显示,即使扩展不在焦点中,但我无法让负责处理通知的服务人员(甚至无法启动) 我按照下面的步骤设置JavaScript Chrome扩展客户端。我已经尝试在我的消息中不包含“通知”负载,并手动注册工作人员(即使教程中没有任何关于这两个方面的内容),但都没有效果 我的服务人员firebase messaging sw.js如下所示: importScripts('http

我正在从Firebase后端向Chrome扩展发送FCM推送通知。当它处于焦点时,我成功地在我的Chrome扩展中显示它。我想让它显示,即使扩展不在焦点中,但我无法让负责处理通知的服务人员(甚至无法启动)

我按照下面的步骤设置JavaScript Chrome扩展客户端。我已经尝试在我的消息中不包含“通知”负载,并手动注册工作人员(即使教程中没有任何关于这两个方面的内容),但都没有效果

我的服务人员
firebase messaging sw.js
如下所示:

importScripts('https://www.gstatic.com/firebasejs/5.8.2/firebase.js');

var config = {
  ...
};
firebase.initializeApp(config);

const messaging = firebase.messaging();

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

  return self.registration.showNotification(notificationTitle,
    notificationOptions);
});
这是我用来发送通知的POST请求(显然具有正确的服务器密钥和用户令牌值):

POST/fcm/send HTTP/1.1
主持人:fcm.googleapis.com
内容类型:application/json
授权:密钥=
缓存控制:没有缓存
{
“数据”:{
“标题”:“Firebase”,
“身体”:“火力基地太棒了”,
“单击操作”:http://localhost:5000/",
“图标”:”http://localhost:5000/firebase-logo.png“
},
“至”:”
}
如何让服务人员接收推送通知并显示它?

提前感谢:)

您可以在服务人员(firebase messaging sw.js)中使用:

您可以在service worker(firebase messaging sw.js)中使用:


你最终找到解决办法了吗?我也有同样的问题。@Jason我就此联系了Firebase团队,他们只说“从Chrome extensions接收FCM通知应该可以使用,因为服务人员在extensions上启用了”。我放弃了它,开始采用另一种方法,所以我没有尝试过,但值得一试。你最终找到了解决方法吗?我也有同样的问题。@Jason我就此联系了Firebase团队,他们只说“从Chrome extensions接收FCM通知应该可以使用,因为服务人员在extensions上启用了”。我放弃了它,开始采用另一种方法,所以我没有尝试过,但值得一试。
POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Content-Type: application/json
Authorization: key=<SERVER_KEY>
Cache-Control: no-cache

{
    "data": {
        "title": "Firebase",
        "body": "Firebase is awesome",
        "click_action": "http://localhost:5000/",
        "icon": "http://localhost:5000/firebase-logo.png"
    },
    "to": "<USER_TOKEN>"
}
importScripts('https://www.gstatic.com/firebasejs/7.2.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.2.2/firebase-messaging.js');

var config = {
  ...
};
firebase.initializeApp(config);

const messaging = firebase.messaging();

this.onpush = event => {
    console.dir(event.data.json());
    // Here you can use event.data to compose Notification
    // @see https://www.w3.org/TR/push-api/#pushmessagedata-interface
};