Chrome扩展程序中Firebase Cloud Messaging的getToken()待处理 [英] Firebase Cloud Messaging's getToken() pending in Chrome Extension
问题描述
我想使用Firebase Cloud Messaging在我的扩展程序中显示推送通知.
I want to use Firebase Cloud Messaging to show push notifications in my extension.
我关注了官方文档,并成功获得了许可,但我无法使用messaging.getToken()
获取Firebase令牌.该方法返回一个promise,但是它既没有解决也没有给出错误.
I followed the official doc and successfully got the permission, but I am unable to get the Firebase token using messaging.getToken()
. The method returns a promise but it is neither resolved nor gives an error.
当我直接在控制台中调用它时,我得到的只是状态为Pending
的Promise
对象.
When I call it in console directly, all I get is a Promise
object with Pending
status.
我搜索了很多问题,但没有适用于我的解决方案.
I have searched many questions but no solution applied to mine.
这是我在background.js
中编写的初始化代码:
Here is the initialization code I have written in background.js
:
var config = {
apiKey: "BWgeK.................MKfP",
authDomain: "****-*****-******.firebaseapp.com",
databaseURL: "https://****-*****-******.firebaseio.com",
projectId: "****-*****-******",
storageBucket: "****-*****-******.appspot.com",
messagingSenderId: "************"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.usePublicVapidKey("******");
messaging.requestPermission()
.then(function() {
//It is printing
console.log("=== have permission ===");
return messaging.getToken();
})
.then(function(currentToken) {
//It is not printing
console.log("== f_token ==", currentToken);
})
.catch(function(err) {
console.log("==== error ====", err);
});
这是manifest.json
文件:
{
"manifest_version": 2,
"name": "Chrome Plugin",
"description": "Chrome Plugin",
"version": "1.0.0.1",
"icons": {
"128": "images/small-logo.png"
},
"browser_action": {
"default_icon": "images/small-logo.png",
"default_popup": "index.html"
},
"background": {
"scripts": ["lib/jquery-3.2.1.min.js","lib/firebase.js", "lib/firebase-app.js","lib/firebase-auth.js", "lib/firebase-messaging.js", "firebase-messaging-sw.js", "src/background.js"],
"persistent": true
},
"content_scripts": [
{
"matches": ["https://google.com/*"],
"js": ["lib/jquery-3.2.1.min.js", "src/content.js"],
"css": ["css/dialog.css"],
"run_at": "document_end"
}],
"permissions": ["identity", "tabs", "storage", "notifications", "webRequest", "webRequestBlocking", "<all_urls>", "unlimitedStorage"],
"content_security_policy": "script-src 'self' https://www.gstatic.com/ https://*.firebaseio.com https://www.googleapis.com; object-src 'self'",
"web_accessible_resources": [
"css/dialog.css", "css/popup.css", "images/small-logo.png",
"lib/jquery-3.2.1.min.js", "lib/firebase.js", "lib/firebase-messaging.js", "lib/firebase-app.js", "lib/firebase-auth.js",
"src/main.js", "src/content.js", "firebase-messaging-sw.js"],
"oauth2": {
"client_id": "*******.apps.googleusercontent.com",
"scopes": ["*******"]
},
"gcm_sender_id": "103953800507"
}
输出:
=== have permission ===
在后台控制台中messaging.getToken()
的结果:
Result of messaging.getToken()
in background's console:
Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
如何使它正常工作?
推荐答案
我在 Firebase Google Group ,并能够获取Firebase令牌.
I followed a thread on Firebase Google Group and was able to get the firebase token.
解决方案1 :将google chrome更新到版本69.
Solution 1: Update the google chrome to version 69.
解决方案2 :将扩展程序ID(chrome-extension://extensionID)添加到Chrome设置中的允许的通知列表中.
Solution 2: Add the extension Id (chrome-extension://extensionID) to the allowed notifications list in Chrome Settings.
这两种解决方案都对我有用,但是我将使用解决方案1.
Both the solutions worked for me but I will go with Solution 1.
这篇关于Chrome扩展程序中Firebase Cloud Messaging的getToken()待处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!