gpt4 book ai didi

javascript - FCM : firebase getToken options( ServiceWorkerRegistration, vapidKey)

转载 作者:行者123 更新时间:2023-12-04 02:31:13 28 4
gpt4 key购买 nike

这就是 firebase 文档描述获取 token 功能的方式:

getToken ( options ? : { serviceWorkerRegistration ?: ServiceWorkerRegistration ; vapidKey ?: string } ) : Promise < string >

Optional options: { serviceWorkerRegistration?: ServiceWorkerRegistration; vapidKey?: string }

Optional serviceWorkerRegistration?: ServiceWorkerRegistrationThe service worker registration for receiving push messaging. If the registration is not provided explicitly, you need to have a firebase-messaging-sw.js at your root location. See Retrieve the current registration token for more details.

我只是想弄清楚如何在我的代码中使用 service worker 选项。我是否像这样将文件位置放在 getToken('/file-location') 内?或者我是否导入一个注册我的自定义 Firebase 服务 worker 的函数?我可能只是愚蠢,但这些文档是准系统。

最佳答案

如果您的 (web) 服务器根目录中有 firebase-messaging-sw.js,您只需调用 .getToken(),您的 service worker 就会自动加载和使用。

如果您的 firebase-messaging-sw.js 位于 /my-other-folder,那么您需要这样调用它:

const swRegistration = await navigator.serviceWorker.register('/my-other-folder/firebase-messaging-sw.js');
const token = await fcm.getToken({
serviceWorkerRegistration: swRegistration,
});

关于javascript - FCM : firebase getToken options( ServiceWorkerRegistration, vapidKey),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64126505/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com