Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.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

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
Javascript firebase,无法接收通知(客户端)_Javascript_Firebase_Notifications_Messaging - Fatal编程技术网

Javascript firebase,无法接收通知(客户端)

Javascript firebase,无法接收通知(客户端),javascript,firebase,notifications,messaging,Javascript,Firebase,Notifications,Messaging,我无法从firebase服务器接收推送通知 firebase-messaging-sw.js文件中有代码(用于接收) // [START initialize_firebase_in_sw] // Give the service worker access to Firebase Messaging. // Note that you can only use Firebase Messaging here, other Firebase libraries // are not a

我无法从firebase服务器接收推送通知

firebase-messaging-sw.js文件中有代码(用于接收)

// [START initialize_firebase_in_sw]
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase     libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js');

// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
'messagingSenderId': 'xxxxxxxxx'
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]

// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// [START background_handler]
messaging.setBackgroundMessageHandler(function(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'
};

return self.registration.showNotification(notificationTitle,
notificationOptions);
});
// [END background_handler]

订阅代码工作100%罚款,所以我不会显示它(我需要更多的声誉,以增加超过2个链接)

我使用服务器端代码发送通知:

服务器端也可以正常工作,因为向firebase发送通知时,服务器会返回以下响应:

{“多播id”:7642532958945472490,“成功”:1,“失败”:0,“规范id”:0,“结果”:[{“消息id”:“0:1490541716147780%e609af1cf9fd7ecd”}]

如您所见,通知已成功发送

但我无法接收,我在chrome中看不到通知窗口

所有来源均取自github firebase示例

有什么问题?为什么我不能收到通知?标识符和访问密钥是正确的,javascript中没有错误或警告,通知成功发送的事实证明了这一点

我使用pastebin,因为我不能格式化stackoverflow上的代码部分,对不起


谢谢

首先,我建议您使用类似postman的方法来测试请求。因此,您可以保证问题不是由您的代码引起的

一,。您是否调用此函数来指定firebase消息传递的服务工作者(从您获取注册令牌的页面)

二,。firebase示例仅在服务工作者内部使用apiKey进行初始化。这给我带来了一些问题。相反,我使用的配置也在页面中使用:

var config = {
   apiKey: "xxxaSyDxxxaQMQW67JxxxnUzxxxSEhVxxxeqKXI",
   authDomain: "xxx.firebaseapp.com",
   databaseURL: "https://xxx.firebaseio.com",
   projectId: "xxx-xxx-xxx",
   storageBucket: "xxx-xxx-xxx.apxxxot.com",
   messagingSenderId: "2xxxx5xxx"
};

firebase.initializeApp(config);

请确保您在问题中包含了问题本身。链接需要更多的时间来导航,而且往往会随着时间的推移而腐烂。若要了解如何在堆栈溢出问题中设置代码格式,请参阅“确定”,在下一次我将使用堆栈溢出格式。我已经编辑了该问题,现在,您能帮助我吗?@Giorgi我也有同样的问题。你能修好吗?@Giorgi你修好了吗?我也有同样的问题。
messaging.useServiceWorker(swRegistration);
var config = {
   apiKey: "xxxaSyDxxxaQMQW67JxxxnUzxxxSEhVxxxeqKXI",
   authDomain: "xxx.firebaseapp.com",
   databaseURL: "https://xxx.firebaseio.com",
   projectId: "xxx-xxx-xxx",
   storageBucket: "xxx-xxx-xxx.apxxxot.com",
   messagingSenderId: "2xxxx5xxx"
};

firebase.initializeApp(config);