![](/img/trans.png)
[英]How we can send image and icon by using Firebase cloud messaging(fcm) for push notification in android
[英]Unable to send push notification to android device using firebase cloud messaging
我正在尝试使用 Firebase 云消息传递向 Android 设备发送通知。
下面是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);
});
}
}
});
});
}
这是我调用这个函数的地方:
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;
}
})
但是,我没有在 android 设备上收到通知。
这是我发表评论时的云功能日志:
有人可以告诉我为什么会发生,以及如何解决? 如果需要,我可以显示更多代码。
我设法找到了解决方案。
在通知发送方法sendToDevice 中,我将键“数据”更新为“通知”,通知现在自动发送并显示在原始用户的设备上。
这是更新的
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.