Man*_*han 17 javascript push-notification firebase-cloud-messaging angular
所以我需要在我们的网络应用中实现网络推送通知,我已经在文档的帮助下在我的应用程序中成功设置了firebase云消息,我可以要求用户允许通知权限并获取令牌ID如果他接受了许可.
问题是当我尝试发送通知来测试生成的令牌时,我收到了一条消息发送的响应,但是我无法在客户端收到它.
我的index.html
<head>
<script src="https://www.gstatic.com/firebasejs/4.4.0/firebase.js"></script>
<link rel="manifest" href="./firebase.json">
</head>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./firebase-messaging-sw.js').then(function(registration) {
console.log('Firebase Worker Registered');
}).catch(function(err) {
console.log('Service Worker registration failed: ', err);
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
我已成功注册服务工作文件
我的App.component.ts
var config = {
apiKey: "somekey",
authDomain: "someproject-2.firebaseapp.com",
databaseURL: "https://someproject-2.firebaseio.com",
projectId: "someproject-2",
storageBucket: "",
messagingSenderId: "someid"
};
firebase.initializeApp(config);
const messaging = firebase.messaging();
messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
return messaging.getToken()
})
.then(function(result) {
console.log("The token is: ", result);
})
.catch(function(err) {
console.log('Unable to get permission to notify.', err);
});
messaging.onMessage(function(payload) {
console.log("Message received. ", payload);
});
Run Code Online (Sandbox Code Playgroud)
我正在获取权限对话框询问权限并获取带有此代码的令牌
我用来发送消息的卷曲代码
curl -X POST -H "Authorization: key=somekey" -H "Content-Type: application/json" -d '{
"notification": {
"title": "Portugal vs. Denmark",
"body": "5 to 1",
"icon": "firebase-logo.png",
"click_action": "http://localhost:8081"
},
"to": "sometoken"
}' "https://fcm.googleapis.com/fcm/send"
Run Code Online (Sandbox Code Playgroud)
我能够使用此代码成功发送通知
我无法弄清楚我哪里出错了,也许是因为它在localhost上?也许onMessage方法运行不正常?任何帮助都非常感谢!
好吧,我在stackoverflow上没有得到任何答案,所以我必须自己做,然后才能工作!
问题出在服务工作者文件中,我显然没有定义消息传递变量。
如果有人遇到此问题,只需确保您的服务工作者文件看起来像这样。
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js');
firebase.initializeApp({
'messagingSenderId': '182145913801'
});
const messaging = firebase.messaging();
Run Code Online (Sandbox Code Playgroud)
小智 6
我有一个类似的问题,我去了firebase上的云消息传递并获得了我的服务器密钥并将其添加到curl请求中.
curl --header "Authorization: key=CloudMessagingServerKey" --header "Content-Type: application/json" -d '{
"notification": {
"title": "FCM Message",
"body": "This is an FCM Message",
},
"to": "token from messaging.getToken()"
}' "https://fcm.googleapis.com/fcm/send"
Run Code Online (Sandbox Code Playgroud)
我 的firebase-messaging-sw.js 看起来像下面的代码
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-
app.js');
importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-
messaging.js');
firebase.initializeApp({
apiKey: 'yourapikey',
authDomain: 'xxxx.firebaseapp.com',
databaseURL: 'https://xxxx.firebaseio.com',
projectId: 'xxxx',
storageBucket: 'xxxx.appspot.com',
messagingSenderId: 'your message sender Id',
});
const messaging = firebase.messaging();
Run Code Online (Sandbox Code Playgroud)
这对我有用.我的错误是我使用了错误的serverKey而我在firebase-messaging-sw.js中没有使用importScripts
| 归档时间: |
|
| 查看次数: |
9123 次 |
| 最近记录: |