[英]Unable to send push notification to android device using firebase cloud messaging
I am trying to send a notification to an android device using firebase cloud messaging.我正在尝试使用 Firebase 云消息传递向 Android 设备发送通知。
Below is sendNotification
which is a cloud function I have deployed on firebase:下面是
sendNotification
,这是我在 firebase 上部署的云功能:
const sendNotification = (owner_uid: any, type: any) => {
return new Promise((resolve, reject) => {
admin.firestore().collection('users').doc(owner_uid).get().then((doc) => {
if (doc.exists && doc.data()?.token) {
if (type === 'new_comment') {
console.log('NEW COMMENT');
console.log('TOKEN: ' + doc.data()?.token);
admin.messaging().sendToDevice(doc.data()?.token, {
data: {
title: 'A new comment has been made on your post',
}
}).then((sent) => {
console.log("SENT COUNT " + sent.successCount);
console.log('SENT APPARENTLY')
resolve(sent);
});
}
}
});
});
}
And here is where I'm calling this function:这是我调用这个函数的地方:
export const updateCommentsCount = functions.firestore.document('comments/{commentId}').onCreate(async (event) => {
const data = event.data();
const postId = data?.post;
const doc = await admin.firestore().collection('posts').doc(postId).get();
if (doc.exists) {
let commentsCount = doc.data()?.commentsCount || 0;
commentsCount++;
await admin.firestore().collection('posts').doc(postId).update({
'commentsCount': commentsCount
})
return sendNotification(doc.data()?.owner, 'new_comment');
} else {
return false;
}
})
However, I'm not receiving a notification on the android device.但是,我没有在 android 设备上收到通知。
And here are the cloud function logs when I leave a comment:这是我发表评论时的云功能日志:
Can someone please tell me why is happening, & how it can be resolved?有人可以告诉我为什么会发生,以及如何解决? I can show further code if required.
如果需要,我可以显示更多代码。
I managed to find the solution.我设法找到了解决方案。
In the notification sending method, sendToDevice , I updated the key "data", to "notification" and the notification is now being automatically sent & displayed on the original user's device.在通知发送方法sendToDevice 中,我将键“数据”更新为“通知”,通知现在自动发送并显示在原始用户的设备上。
Here is the updated这是更新的
admin.messaging().sendToDevice(doc.data()?.token, {
notification: {
title: 'A new comment has been made on your post',
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.