【发布时间】:2018-04-08 17:15:31
【问题描述】:
在客户端我请求许可并将令牌发送到服务器
messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
return messaging.getToken();
})
.then((token) => {
if (token) {
const body = JSON.stringify({
token,
stopId: 'BKK_F02550',
routeId: 'BKK_0205',
});
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
};
console.log(token, 'token');
fetch('/api/test', {method: 'post', body, headers});
} else {
console.log('No Instance ID token available. Request permission to generate one.');
}
})
.catch(function(err) {
console.log('Unable to get permission to notify.', err);
});
在服务器端,我将请求发送到 FCM:
app.post('/api/test', (req, res) => {
const body = {
"to": req.body.token,
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
const headers = {
Authorization: `key=${serverKey}`,
'Content-Type': 'application/json'
}
fetch('https://fcm.googleapis.com/fcm/send', body, headers)
.then(data => res.status(200).json(data))
.catch(error => console.log('failure', error))
})
我收到成功响应,但在客户端没有收到通知。我觉得响应对象的要点是:{size: 0, timeout: 0}。
【问题讨论】:
-
从服务器端,
req.body.token的值是多少?可以记录吗? -
是的,我已经这样做了,而且两边的令牌值相同
标签: javascript firebase push-notification firebase-cloud-messaging progressive-web-apps