后端服务器发送FCM通知,如下所示:
{
"to": "/topics/599_7041",
"priority": "high",
"data": {
"id_message": "45"
}
"click_action": "SHOW"
"notification" : {
"body" : "Lorem ipsum",
"title" : "Sample"
}
}
我的应用程序订阅了多个主题。当应用程序未运行时,Firebase会显示通知并允许我在点击通知消息后运行应用程序,提供单击操作并将数据(在本例中为message_id
)传递给已启动的应用程序实例。
目前很好。但是,此外,我需要确定从哪个订阅主题通知收到。从通知中启动新的应用程序实例时,我可以以某种方式确定它吗?或者我需要将主题名称添加到数据(message_id
旁边)?
我知道我可以确定当我的应用程序运行时提交的to
,这样:
@Override
public void onMessageReceived(@NonNull final RemoteMessage remoteMessage)
{
//....
String to = remoteMessage.getTo();
}
但是,如果我的应用程序没有运行,那么onMessageReceived
将不会被调用。
你可以这样做:
exports.pushNotification =
functions.database.ref('/messages/{pushId}').onWrite( event => {
console.log('Push notification event triggered');
var valueObject = event.data.val();
console.log("it is"+ valueObject.title);
//Create a notification
const payload = {
notification: {
title:valueObject.title,
body: valueObject.message,
sound: "default"
},
};
这样您就可以使用数据库触发器onWrite
来检查是否已在pushid
下写入任何消息。
然后使用以下命令从数据库中检索数据:qazxsw poi。
在var valueObject = event.data.val();
中,您可以使用notification{..}
作为主题名称,因此在收到通知时,主题名称将出现在通知的标题中,这就是您如何知道它是什么主题。
有关更多信息,请查看valueObject.title