javascript - FCM JS 客户端 - 提供的 applicationServerKey 无效

标签 javascript firebase firebase-cloud-messaging web-push

我正在尝试使用 Firebase Cloud Messaging 来使用 WebPush,并按照他们的文档设置 JavaScript 客户端。我的代码位于所有设置下方,但是当我调用 getToken 时,我收到一条错误消息:

无法在“PushManager”上执行“订阅”:提供的 applicationServerKey 无效。

我已经四次检查了我的 VAPID key ,并且它使用的是 Firebase 控制台 > 项目设置 > 云消息 > Web 推送认证中的公钥。

我已经尝试过使用和不使用点击事件,但没有运气。

我已经尝试过不同的域,但没有成功。

我已确认已授予通知权限。

// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getMessaging, getToken, onMessage } from "firebase/messaging";

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
    apiKey: "AIzaSyDAIduxCnC5T4-QoDEw1vRjnI1_1K0odeg",
    authDomain: "hudsonvillewaterpolo.firebaseapp.com",
    projectId: "hudsonvillewaterpolo",
    storageBucket: "hudsonvillewaterpolo.appspot.com",
    messagingSenderId: "351470653163",
    appId: "1:351470653163:web:1572d4340e67fe6cf9b8ae",
    measurementId: "G-Z86EW7BNVM"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const messaging = getMessaging(app);

function doTheThing() {

    getToken(messaging, { vapidKey: "VAPID KEY HERE" }).then(currentToken => {
        if (currentToken) {
            // send the token to your server and update the UI if necessary
            console.log(currentToken)
        } else {
            console.log('No registration token available, request permission to generate one')
        }
    }).catch(err => {
        console.error('Error occurred while retrieving token', err);
    });
}


onMessage(messaging, (payload) => {
    console.log('Message received. ', payload);
    // ...
});

document.addEventListener('DOMContentLoaded', () => {
    document.querySelector('.site-name').addEventListener('click', doTheThing)
})

最佳答案

enter image description here

天哪,键在显示中被 chop 了! 🤬

我最终检查了该元素并将其从源中提取出来。

这真是浪费时间。

关于javascript - FCM JS 客户端 - 提供的 applicationServerKey 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77241230/

相关文章:

javascript - 我应该如何在 Backbone.js 中构造类继承?

firebase - 如何使用 Firebase 存储聊天的用户和群组

ios - Firebase .onCall Cloud Function 未从 Swift Xcode 项目中调用

firebase - 如何在 flutter 中检索 firebase 数据

android - 为 Room 数据库使用 Android 服务上下文

iOS - 在前台接收 FCM 推送消息

javascript - 无法使用 AJAX 将 PHP 变量获取到 JavaScript 文件

javascript - 如何使用 $(this) 将 'top' css 值设置为变量

firebase - 我可以在没有通知权限的情况下使用 firebase 云消息传递吗? (Javascript)

javascript - 想要从 jquery 列表中删除取消选中的复选框值