簡體   English   中英

無法使用 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 中,我將鍵“數據”更新為“通知”,通知現在自動發送並顯示在原始用戶的設備上。

這是更新的

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM