当我的应用程序处于打开状态并收到通知时,我希望能够立即打开关联的活动,而无需用户点击通知。在前台收到的关于Firebase通知的打开活动
这个问题是非常相似:Open app on firebase notification received (FCM)
但它打开应用程序时,它在后台,我需要做的是,当我的应用程序是在前台。
通知时,您的应用程序在后台交付。在这个 的情况下,通知被传递到设备的系统托盘。 A 默认情况下,用户点击通知将打开应用程序启动器。带有通知和数据有效负载的消息 ,前台均为背景和 。在这种情况下,通知将传送到 设备的系统托盘,并且数据有效负载将以您的启动程序活动意图的附加组件 传送。
这是我的onMessageReceived
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage);
}
}
/**
* Create and show a simple notification containing the received FCM message.
*
* @param remoteMessage FCM message message received.
*/
private void sendNotification(RemoteMessage remoteMessage) {
Intent intent = new Intent(this, MyActivity.class);
Map<String, String> hmap ;
hmap = remoteMessage.getData();
hmap.get("data_info");
intent.putExtra("data_info", hmap.get("data_info"));
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
}
我能够正确地得到通知,但一旦我点击系统托盘通知的活动只有开始implmentation。
有没有办法在不使用前台通话的情况下启动活动?
该方法onMessageReceived()
类MyFirebaseMessagingService
extends FirebaseMessagingService
在前台正在调用,但活动没有开始。我也试过与国旗FLAG_ACTIVITY_NEW_TASK
也没有运气。提前致谢。
ForegroundActivity代码应该放在Myactivity.class(应该打开的活动)还是在不同的活动中? – bankode
在不同的活动。在通知到达的那一刻,用户应该使用它。即:用户打开应用程序并检查一些内容,您可以拥有一个MainActivity类并在那里声明观察者,只要介意用户离开该活动时广播通知将不会被捕获。 – cdiazmo
即使应用程序被终止,此代码是否也能正常工作? –