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云消息传递&x27;s getToken()在Chrome扩展中挂起_Javascript_Firebase_Google Chrome Extension_Promise_Firebase Cloud Messaging - Fatal编程技术网

Javascript Firebase云消息传递&x27;s getToken()在Chrome扩展中挂起

Javascript Firebase云消息传递&x27;s getToken()在Chrome扩展中挂起,javascript,firebase,google-chrome-extension,promise,firebase-cloud-messaging,Javascript,Firebase,Google Chrome Extension,Promise,Firebase Cloud Messaging,我想使用Firebase云消息在我的扩展中显示推送通知 我遵循并成功获得了权限,但我无法使用messageing.getToken()获取Firebase令牌。该方法返回一个承诺,但既没有解析,也没有给出错误 当我直接在控制台中调用它时,我得到的只是一个状态为Pending的Promise对象 我搜索了很多问题,但没有找到适合我的答案 下面是我在background.js中编写的初始化代码: var config = { apiKey: "BWgeK.................MK

我想使用Firebase云消息在我的扩展中显示推送通知

我遵循并成功获得了权限,但我无法使用
messageing.getToken()
获取Firebase令牌。该方法返回一个承诺,但既没有解析,也没有给出错误

当我直接在控制台中调用它时,我得到的只是一个状态为
Pending
Promise
对象

我搜索了很多问题,但没有找到适合我的答案

下面是我在
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"
}
后台控制台中消息传递的结果。getToken():

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
我如何才能让它工作?

我遵循了一个线程,并能够获得firebase令牌

解决方案1:将google chrome更新至69版

解决方案2:添加扩展Id(chrome)-extension://extensionID)到Chrome设置中的允许通知列表

这两种解决方案对我都有效,但我会选择解决方案1

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}