简体   繁体   English

无法使用 Firebase 云消息传递向 Android 设备发送推送通知

[英]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.

相关问题 我们如何使用Firebase云消息传递(fcm)在Android中发送推送通知来发送图像和图标 - How we can send image and icon by using Firebase cloud messaging(fcm) for push notification in android Firebase云消息传递Android推送通知不起作用 - firebase cloud messaging Android push notification not working 使用云消息传递Firebase推送通知 - Push notification using cloud messaging firebase 使用Firebase和Google Cloud Messaging推送通知 - Push Notification using Firebase and Google Cloud Messaging 使用 FCM(Firebase 云消息传递)通过 C# 将推送发送到 Android - Send push to Android by C# using FCM (Firebase Cloud Messaging) 如何从 ADB 向设备发送 FCM(firebase 云消息传递)推送通知 - How to send FCM (firebase cloud messaging) push notification from ADB to device firebase 云消息推送通知 - firebase cloud messaging push notification Firebase 云消息推送通知无法在前台工作,仅在后台工作,在 Android - Firebase Cloud Messaging push notification not working in foreground, only background, on Android Android phonegap使用php使用Google Cloud Messaging发送推送通知 - Android phonegap send push notification using Google Cloud Messaging using php 如何使用Firebase Cloud Messaging在Adobe Phonegap中执行推送通知 - How to perform Push Notification in adobe Phonegap using Firebase Cloud Messaging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM