gpt4 book ai didi

javascript - Firebase Web 推送通知 token 未获取

转载 作者:行者123 更新时间:2023-12-03 00:00:32 29 4
gpt4 key购买 nike

我是 iOS 开发人员而不是 php,我正在实现 Firebase Web 推送通知以在 Web 中发送和接收。我正在使用 xampp 和 php。为此,我遵循了本教程 https://www.itwonders-web.com/blog/push-notification-using-firebase-demo-tutorial 。在这里,我创建了 index1.html 页面并将其放置在 myphp 文件夹中,然后创建了 firebase-messaging-sw.js 文件并将其放置在同一文件夹中。我收到允许警报,但 token 为空。

请参见下面的屏幕截图,这是我的输出。

enter image description here

实际上我在这里使用的是xampp本地主机,是否必须在实时网站中运行才能获取 token ?

(我的网址是: http://192.168.64.2/myphp/ )

我的代码是

实际上index.html页面名称没有收到警报,这就是为什么我替换了页面名称index1.html,为什么我不知道它没有显示任何文件。

index1.html

<html>
<title>Firebase Messaging Demo</title>
<style>
div {
margin-bottom: 15px;
}
</style>
<body>
<div id="token"></div>
<div id="msg"></div>
<div id="notis"></div>
<div id="err"></div>
<script src="https://www.gstatic.com/firebasejs/5.9.0/firebase.js"></script>
<script>
MsgElem = document.getElementById("msg")
TokenElem = document.getElementById("token")
NotisElem = document.getElementById("notis")
ErrElem = document.getElementById("err")
// Initialize Firebase
// TODO: Replace with your project's customized code snippet
var config = {
apiKey: "AIza********yMSEK3PM",
authDomain: "myf*********ject.firebaseapp.com",
databaseURL: "https://myf*********ject.firebaseio.com",
projectId: "myf******ject",
storageBucket: "myf********ject.appspot.com",
messagingSenderId: "67********7"
};
firebase.initializeApp(config);

const messaging = firebase.messaging();
messaging
.requestPermission()
.then(function () {
MsgElem.innerHTML = "Notification permission granted."
console.log("Notification permission granted.");

// get the token in the form of promise
return messaging.getToken()
})
.then(function(token) {
TokenElem.innerHTML = "token is : " + token
})
.catch(function (err) {
ErrElem.innerHTML = ErrElem.innerHTML + "; " + err
console.log("Unable to get permission to notify.", err);
});

messaging.onMessage(function(payload) {
console.log("Message received. ", payload);
NotisElem.innerHTML = NotisElem.innerHTML + JSON.stringify(payload)
});
</script>

<body>
</body>

firebase-messaging-sw.js

importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');

// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
firebase.initializeApp({
'messagingSenderId': '67******7'
});

// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();

messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/itwonders-web-logo.png'
};

return self.registration.showNotification(notificationTitle,
notificationOptions);
});

如果可能,请发送 Firebase Web 推送通知的详细教程链接。

最佳答案

我也遇到了同样的问题,可以通过将服务工作线程文件移动到 htdocs 文件夹(即根文件夹)来解决此问题

关于javascript - Firebase Web 推送通知 token 未获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55194282/

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