FCM通知有效载荷 - 确定主题名称时,应用程序没有运行

后端服务器发送FCM通知如下: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写的。

然后,使用以下命令从数据库中检索数据:var valueObject = event.data.val();

而且在notification{..} recieving的通知时,你可以使用valueObject.title这将是主题名称,因此,主题名称将在通知的标题,这是你如何知道它是什么的话题。

欲了解更多信息,请点击https://firebase.google.com/docs/functions/database-events

以上是 FCM通知有效载荷 - 确定主题名称时,应用程序没有运行 的全部内容, 来源链接: utcz.com/qa/261909.html

回到顶部