我有一个云 Firestore 函数,可以向用户发送通知。
[安卓]
它运行良好,但只有在应用程序处于后台(暂停或死亡(时,我才会收到通知。
这是我函数的简化版本:
'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 代码,因为我现在只需要显示通知。有没有人想以简单的方式解决这个问题?
FCM 通知由系统在您的应用后台运行时处理,并显示在 Android 上的系统托盘中。如果要在应用处于活动状态时处理通知,则必须通过覆盖onMessageReceived
来为此编写代码。
另请参阅:
- 当应用处于前台时如何处理火力基地通知