当我的应用程序打开并且收到通知时,我希望能够立即打开关联的活动,而不需要用户点击通知。
这个问题非常相似:在收到 Firebase 通知时打开应用程序 (FCM)
但它在后台打开应用程序,我需要在我的应用程序位于前台时执行此操作。
来自Firebase 文档:
当您的应用程序处于后台时发送通知。在这个
在这种情况下,通知将发送到设备的系统托盘。 A
默认情况下,用户点击通知会打开应用程序启动器。留言
具有通知和数据负载,后台和
前景。在这种情况下,通知将发送至
设备的系统托盘,数据有效负载在 extras 中传递
您的启动器 Activity 的意图。
这是我的实现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);
}
我能够正确收到通知,但只有在点击系统托盘上的通知后活动才会开始。
有没有办法在前台不点击通知的情况下启动活动?
方法onMessageReceived()
从班级MyFirebaseMessagingService
that extends FirebaseMessagingService
在前台时被正确调用,但活动尚未开始。我也尝试过使用标志FLAG_ACTIVITY_NEW_TASK
也没有运气。提前致谢。