javascript - Firebase cloud message not sending to device
问题描述
I am trying to allow users to receive push notifications if an action happened ( like, comment, etc). In the log I sometimes get sucesscount: 1 however device does not receive a notification. But most of the time it is faliurecount:1 the second time. Regardless I am not getting the push notification. When I deployed my functions there was no errors.
When I do send myself a test message from Firebase cloud messaging, that however does work correctly and sends a push notification to everyones devices successfully.
Here is observing likes
for example
exports.observeLikes = functions.database.ref('/user-likes/{uid}/{postId}').onCreate((snapshot, event) => {
var uid = event.params.uid;
var postId = event.params.postId;
return admin.database().ref('/users/' + uid).once('value', snapshot => {
var userThatLikedPost = snapshot.val();
return admin.database().ref('/posts/' + postId).once('value', snapshot => {
var post = snapshot.val();
if(uid === post.ownerUid) {
return Promise.resolve();
}
return admin.database().ref('/users/' + post.ownerUid).once('value', snapshot => {
var postOwner = snapshot.val();
var payload = {
notification: {
body: userThatLikedPost.username + ' liked your post ',
sound: 'default'
}
};
admin.messaging().sendToDevice(postOwner.fcmToken, payload)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
return response;
})
.catch((error) => {
console.log('Error sending message:', error);
throw new Error('Error sending message:', error);
});
})
})
})
})
Log
解决方案
So I found the issue. I deleted the app from my device and created a new account ( and it created a new FCM token for the account) and push notifications does work. I did another check and manually changed the FCM token on another device to a new one it registered with , and push notifications are successful. It appears I now need to find a way to update every current test users FCM token because their current devices aren't receiving notifications.
推荐阅读
- python - 为什么在链接列表的第 n 个位置插入节点的代码对我不起作用?
- python - Python on while 返回值
- python - 如何使列中的所有字符串长度相同
- tensorflow - 如何正确使用plot_model?
- aws-sdk - 使用 Trusted Signers 和 Node.js SDK 调试 CloudFront 签名 URL
- postgresql - 当调用略有不同时,postgresql中函数中的“order by”被反转
- python - 如何使用连接连接到 Airflow 中的 InfluxDB?
- python - 在 matplotlib pyplot 中,如何按类别对条形图中的条形进行分组?
- r - R中逐年断点的计算
- html - 如何使用 CSS 为图像设置动画以移出 div 并从另一侧返回到相同位置?