gpt4 book ai didi

Android:后台传递数据中的FCM推送通知

转载 作者:搜寻专家 更新时间:2023-11-01 09:33:16 26 4
gpt4 key购买 nike

我在将背景数据传递到屏幕时遇到困难。在后台,应用程序正在正确调用屏幕,但未获取通知中“数据”(来自数据对象的 ID)中此屏幕所需的数据。在前台我正确地得到了“数据”。

通知json

{
"to" : "akshih890uhnkjh389jfkn3...",
"priority" : "normal",
"time_to_live" : 0,
"data" : {
"type" : "my_type",
"id" : "my_id"
},
"notification" : {
"body" : "Test body",
"title" : "Test title",
"click_action" : ".MyActivity"
}
}


public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
String clickAction = remoteMessage.getNotification().getClickAction();

Map<String, String> data = remoteMessage.getData();
Gson gson = new Gson();
MyObject myObject = new MyObject(data.get("id"), data.get("type"), remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody());

Intent intent = new Intent(myObject.getClickAction());
intent.putExtra("id", myObject.getId());

PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

sendFCMNotification(title, body, contentIntent);

}

我的 Activity

 if(getIntent() != null) {

Bundle extras = getIntent().getExtras();
code = extras.getString("id");

}

最佳答案

int id = Integer.valueOf(remoteMessage.getData().get("id"));

因为你使用了 remoteMessage.getData(),你得到了整个 data 对象,而不是一个里面有 data json 的对象,你得到包含您的字段的实际 data json。

String data = remoteMessage.getData();
Gson gson = new Gson();
MyObject myObject = gson.fromJson(data, MyObject.class);

关于Android:后台传递数据中的FCM推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45460211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com