我停止从我的项目中接收推送通知(使用FCM),开始调查,发现我的服务人员甚至无法获得自己的FCM推送令牌,总是收到错误消息:
Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential.
此后,我发现我域的IndexedDB为空。这里有整个结构,但是没有数据,因此服务工作人员无法获得它的简单密钥。当我尝试在服务工作人员中强行插入密钥时,我得到:
Messaging: This method is available in a Window context. (messaging/only-available-in-window).
这是我的服务人员的代码:
importScripts('https://www.gstatic.com/firebasejs/5.4.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/5.4.2/firebase-messaging.js');
var config = {
apiKey: "xxx",
authDomain: "xxx.firebaseapp.com",
databaseURL: "https://xxx.firebaseio.com",
projectId: "xxx",
storageBucket: "xxx.appspot.com",
messagingSenderId: "xxx"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
var token = '';
self.addEventListener('notificationclick', function(event) {
event.notification.close();
event.waitUntil(self.clients.openWindow(event.notification.data.url));
});
messaging.setBackgroundMessageHandler(function(payload) {
var options = payload.data;
options.requireInteraction = true;
options.vibrate = [300, 100, 400];
options.data = {};
options.data.url = options.url.replace('%TOKEN%',encodeURIComponent(token));
return self.registration.showNotification(options.title,options);
});
messaging.getToken().then(function(currentToken){
token = currentToken;
});
那么,当indexeddb为空时,如何在服务工作人员中获取令牌?或者我该如何强制使用无效密钥?