【问题标题】:Firebase Cloud Messaging's getToken() pending in Chrome ExtensionFirebase 云消息传递的 getToken() 在 Chrome 扩展中挂起
【发布时间】:2019-03-19 22:30:22
【问题描述】:

我想使用 Firebase 云消息传递在我的扩展程序中显示推送通知。

我关注了official doc 并成功获得了许可,但我无法使用messaging.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"
}

输出:

=== have permission ===

messaging.getToken() 在后台控制台中的结果:

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

我怎样才能让它工作?

【问题讨论】:

    标签: javascript firebase google-chrome-extension promise firebase-cloud-messaging


    【解决方案1】:

    我关注了Firebase Google Group 上的一个帖子,并获得了 firebase 令牌。

    解决方案 1:将 google chrome 更新到版本 69。

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

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

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多