简体   繁体   English

Chrome扩展程序中Firebase Cloud Messaging的getToken()待处理

[英]Firebase Cloud Messaging's getToken() pending in Chrome Extension

I want to use Firebase Cloud Messaging to show push notifications in my extension. 我想使用Firebase Cloud Messaging在扩展中显示推送通知。

I followed the official doc and successfully got the permission, but I am unable to get the Firebase token using messaging.getToken() . 我遵循了官方文档并成功获得了许可,但是我无法使用messaging.getToken()获得Firebase令牌。 The method returns a promise but it is neither resolved nor gives an error. 该方法返回一个promise,但既不解决也不给出错误。

When I call it in console directly, all I get is a Promise object with Pending status. 当我直接在控制台中调用它时,我得到的只是一个状态为PendingPromise对象。

I have searched many questions but no solution applied to mine. 我搜索了许多问题,但没有适用于我的解决方案。

Here is the initialization code I have written in background.js : 这是我在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);
    });

Here is the manifest.json file: 这是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"
}

Output: 输出:

=== have permission ===

Result of messaging.getToken() in background's console: 在后台控制台中的messaging.getToken()结果:

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

How can I get this to work? 我该如何工作?

I followed a thread on Firebase Google Group and was able to get the firebase token. 我在Firebase Google Group上关注了一个线程,并能够获取Firebase令牌。

Solution 1 : Update the google chrome to version 69. 解决方案1 :将谷歌浏览器更新到版本69。

Solution 2 : Add the extension Id (chrome-extension://extensionID) to the allowed notifications list in Chrome Settings. 解决方案2 :将扩展程序ID(chrome-extension:// extensionID)添加到Chrome设置中的允许的通知列表中。

Both the solutions worked for me but I will go with Solution 1. 两种解决方案都对我有用,但我将使用解决方案1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM