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

喜你入骨 提交于 2020-01-25 09:26:05

问题


I want to use Firebase Cloud Messaging to show push notifications in my extension.

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.

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.

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);
    });

Here is the manifest.json file:

{
    "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:

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

How can I get this to work?


回答1:


I followed a thread on Firebase Google Group and was able to get the firebase token.

Solution 1: Update the google chrome to version 69.

Solution 2: Add the extension Id (chrome-extension://extensionID) to the allowed notifications list in Chrome Settings.

Both the solutions worked for me but I will go with Solution 1.



来源:https://stackoverflow.com/questions/52817689/firebase-cloud-messagings-gettoken-pending-in-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!