我在使用 FCM click_action启动后台活动时遇到问题。
我的宣言:
<activity
android:name="com.myoro.MainActivity"
android:theme="@style/AppTheme.NoActionBar"/>
<activity
android:name="com.myoro.ActivityA"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.myoro.A" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.myoro.ActivityB"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.myoro.B" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
我的消息设置:
data: {{"type" : "A","id" : "a123","click_action": "com.myoro.A"}}
.JAVA:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
if (remoteMessage.getData().containsKey("click_action")) {
Intent intent = new Intent(remoteMessage.getData().get("click_action"));
intent.putExtra("id", remoteMessage.getData().get("id"));
startActivity(intent);
return;
}
if (remoteMessage.getData().containsKey("data")) {
String data = remoteMessage.getData().get("data");
Gson gson = new Gson();
MyObj myObj = gson.fromJson(data, MyObj.class);
if(myObj.getType().equals("A")) {
Intent intent = new Intent(context, ActivityA.class);
intent.putExtra("id", myObj.getId());
startActivity(intent);
} else {
Intent intent = new Intent(context, ActivityB.class);
intent.putExtra("id", myObj.getId());
startActivity(intent);
}
}
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
}
如果我尝试发送此 json 模型并且它总是打开活动主。如何操作通知点击在四地和后台打开活动A和活动B,需要传递id?...
click_action
是应在notification
消息负载中使用的参数,而不是data
。
尝试将有效负载编辑为如下所示的内容:
}
notification:{
"click_action": "com.myoro.A"
},
data: {
"type" : "A",
"id" : "a123"
}
}
有关参考,请参阅 FCM HTTP 参数: https://firebase.google.com/docs/cloud-messaging/http-server-ref