简体   繁体   English

FCM通知负载 - 在应用未运行时确定主题名称

[英]FCM notification payload - determine topic name when app is not running

Backend server sends FCM notification like this: 后端服务器发送FCM通知,如下所示:

{
  "to": "/topics/599_7041",
  "priority": "high",
  "data": {
    "id_message": "45" 
  }
  "click_action": "SHOW" 
  "notification" : {
      "body" : "Lorem ipsum",
      "title" : "Sample" 
    }
}

My application subscribes multiple topics. 我的应用程序订阅了多个主题。 When application is not running, Firebase displays notification and allows me to run app after tap on notification message, with provided click action and pass data (in this case, message_id ) to launched app instance. 当应用程序未运行时,Firebase会显示通知并允许我在点击通知消息后运行应用程序,并提供单击操作并将数据(在本例中为message_id )传递给已启动的应用程序实例。

Good so far. 目前很好。 However, in addition, I need to determine from which subscribed topic notification is received. 但是,此外,我需要确定从哪个订阅主题通知收到。 Can I determine it somehow when launching new app instance from notification, or I need to add topic name to data (next to message_id )? 我可以在从通知启动新的应用程序实例时以某种方式确定它,或者我需要向数据添加主题名称(在message_id旁边)?

I know I can determine to filed when my app is running, this way: 我知道我能确定to申请时,我的应用程序正在运行,这种方式:

@Override
public void onMessageReceived(@NonNull final RemoteMessage remoteMessage)
{
 //....
 String to = remoteMessage.getTo();
}

But, what if my app is not running, so onMessageReceived won't be called. 但是,如果我的应用程序没有运行,那么onMessageReceived将不会被调用。

you can do this: 你可以这样做:

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"
},
};

This way you are using the database trigger onWrite , that checks if any messages have been written under the pushid . 这样您就可以使用数据库触发器onWrite来检查是否已在pushid下写入任何消息。

Then you retreive the data from the database using: var valueObject = event.data.val(); 然后使用以下命令从数据库中检索数据: var valueObject = event.data.val(); .

And in the notification{..} you can use valueObject.title which will be the topic name, thus when recieving a notification, the topic name will be in the title of the notification and thats how you can know what topic it is. notification{..}您可以使用valueObject.title作为主题名称,因此在收到通知时,主题名称将出现在通知的标题中,这就是您如何知道它是什么主题。

For more info check https://firebase.google.com/docs/functions/database-events 有关详情,请访问https://firebase.google.com/docs/functions/database-events

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM