应用运行android时未收到Cloud Firestore通知

时间:2019-04-21 14:37:58

标签: android firebase google-cloud-firestore firebase-cloud-messaging

我具有Cloud Firestore功能,可以将通知发送给用户。

[ANDROID]

它工作得很好,但是只有当应用程序处于后台(暂停或终止)时,我才会收到通知。

这是我的函数的简化版本:

    'use strict'


const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


exports.sendNotification = functions.database.ref('/Notifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) =>
{
    const receiver_user_id = context.params.receiver_user_id;
    const notification_id = context.params.notification_id;


console.log('We have a notification to send to :' , receiver_user_id);


if (!data.after.val()) 
{
    console.log('A notification has been deleted :' , notification_id);
    return null;
}

const DeviceToken = admin.database().ref(`/Users/${receiver_user_id}/device_token`).once('value');

return DeviceToken.then(result => 
{
    const token_id = result.val();

    const payload = 
    {
        notification:
        {
            title: "New Chat Request",
            body: `You have a new chat Request`,
            icon: "default"
        }
    };

    return admin.messaging().sendToDevice(token_id, payload)
    .then(response => 
        {
            console.log('This was a notification feature.');
        });
});
});

我没有任何特定的Java代码,因为我现在只需要显示通知即可。 有没有人有一个简单的方法可以解决这个问题?

1 个答案:

答案 0 :(得分:0)

FCM通知在您的应用程序后台运行时由系统处理,并显示在Android的系统托盘中。如果您想在应用处于活动状态时处理通知,则必须通过overriding onMessageReceived编写相应的代码。

另请参阅: