TypeError:messaging.getToken 不是函数

Posted

技术标签:

【中文标题】TypeError:messaging.getToken 不是函数【英文标题】:TypeError: messaging.getToken is not a function 【发布时间】:2021-09-15 08:59:45 【问题描述】:

所以我正在开发一个 Firebase 云消息传递网络应用程序并收到错误:

Error: TypeError: messaging.getToken is not a function

我尝试了很多来解决它,也在谷歌上搜索过,但没有运气。 如果有人可以帮助我,我将不胜感激。

相同的代码在官方谷歌示例中运行良好。 以下是我收到此错误的行:

            return messaging.getToken();

下面是整个代码:

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clouding</title>
    <script type="module">
        // Import the functions you need from the SDKs you need
        import  initializeApp  from "https://www.gstatic.com/firebasejs/9.0.2/firebase-app.js";
        import  getAnalytics  from "https://www.gstatic.com/firebasejs/9.0.2/firebase-analytics.js";
        import  getMessaging  from "https://www.gstatic.com/firebasejs/9.0.2/firebase-messaging.js";
        // TODO: Add SDKs for Firebase products that you want to use
        // https://firebase.google.com/docs/web/setup#available-libraries

        // Your web app's Firebase configuration
        // For Firebase JS SDK v7.20.0 and later, measurementId is optional
        const firebaseConfig = 
            apiKey: "AIzaSyDOVN1qOGzsD6AI-2jA05LpfQIJ5aoCuD4",
            authDomain: "clouding-aa740.firebaseapp.com",
            projectId: "clouding-aa740",
            storageBucket: "clouding-aa740.appspot.com",
            messagingSenderId: "1059589787087",
            appId: "1:1059589787087:web:cdd636057d2a921ccdd6ba",
            measurementId: "G-CD2BEK39LZ"
        ;
        window.addEventListener("click", (e) => 
            switch (e.target.id) 
                case "btnPermissions":
                    initFirebaseMessagingRegistration();
                    break;
            
        );
        // Initialize Firebase
        const app = initializeApp(firebaseConfig);
        //        const analytics = getAnalytics(app);
        const messaging = getMessaging(app);


        function initFirebaseMessagingRegistration() 

            Notification.requestPermission()
                .then(function () 
                    messageElement.innerhtml = "Got notification permission";
                    console.log("Got notification permission");
                    return messaging.getToken();
                )
                .then(function (token) 
                    // print the token on the HTML page
                    tokenElement.innerHTML = "Token is " + token;
                )
                .catch(function (err) 
                    errorElement.innerHTML = "Error: " + err;
                    console.log("Didn't get notification permission", err);
                );

            // Don't forget your vapidKey here
            getToken(messaging,  vapidKey: "publicVapidKey" )
                .then((t) => 
                    tokenElement.innerHTML = "Token is " + r;
                )
                .catch(function (err) 
                    errorElement.innerHTML = "Error: " + err;
                    console.log("Didn't get notification permission", err);
                );

            onMessage(messaging, (payload) => 
                console.log("Message received. ", JSON.stringify(payload));
                notificationElement.innerHTML =
                    notificationElement.innerHTML + " " + payload.data.notification;
            );
            messaging.onTokenRefresh(function () 
                messaging.getToken()
                    .then(function (refreshedToken) 
                        console.log('Token refreshed.');
                        tokenElement.innerHTML = "Token is " + refreshedToken;
                    ).catch(function (err) 
                        errorElement.innerHTML = "Error: " + err;
                        console.log('Unable to retrieve refreshed token ', err);
                    );
            );
            console.log("clicked");
        

    </script>

</head>

<body>
    <main>
        <h1>Welcome to Clouding</h1>
        <div id="token" style="color:lightblue"></div>
        <div id="message" style="color:lightblue"></div>
        <div id="notification" style="color:green"></div>
        <div id="error" style="color:red"></div>
        <script>
            messageElement = document.getElementById("message")
            tokenElement = document.getElementById("token")
            notificationElement = document.getElementById("notification")
            errorElement = document.getElementById("error")
        </script>
        <button id="btnPermissions">Enable Firebase Messaging</button>

    </main>

</body>

</html>

【问题讨论】:

【参考方案1】:

知道了, 我还必须导入 getToken 方法。 下面是我应该写的新的 sn-p:

import  getMessaging, getToken  from "https://www.gstatic.com/firebasejs/9.0.2/firebase-messaging.js";

代替:

import  getMessaging  from "https://www.gstatic.com/firebasejs/9.0.2/firebase-messaging.js";

【讨论】:

还是同样的错误【参考方案2】:

https://***.com/a/70046707/5681567 查看给出的链接。 react native 也出现了同样的问题。 试试这种东西

messaging ().getToken()

【讨论】:

以上是关于TypeError:messaging.getToken 不是函数的主要内容,如果未能解决你的问题,请参考以下文章

反应本机获取多标记[未处理的承诺拒绝:TypeError:TypeError:未定义不是对象(评估'this.state.markers.map

Django TypeError - TypeError: issubclass() arg 1 必须是一个类

pyspark:TypeError:'float'对象不可迭代

Python 3.8 TypeError: can't concat str to bytes - TypeError: a bytes-like object is required, not 's

TypeError: key 必须是一个字符串,一个缓冲区或一个对象在 typeError 与 GCP 文件存在

TypeError: jQueryxxxxxx 不是函数