无法使用 firebase 云消息传递向 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,我将密钥 "data" 更新为 "notification",通知现在正在自动发送并显示在原始用户的设备。
这是更新后的
admin.messaging().sendToDevice(doc.data()?.token, {
notification: {
title: 'A new comment has been made on your post',
}
我正在尝试使用 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,我将密钥 "data" 更新为 "notification",通知现在正在自动发送并显示在原始用户的设备。
这是更新后的
admin.messaging().sendToDevice(doc.data()?.token, {
notification: {
title: 'A new comment has been made on your post',
}