我尝试了这里发布的几乎所有解决方案和每个标志组合,但都没有起作用。
以下是我遇到问题的用例。
1)当我在应用程序中使用FCM通知时,打开我想要的活动。数据传递到主活动中的onNewIntent。当应用程序处于前台时运行良好。
2)当处于后台模式(按下主页按钮)时,单击通知后,它会启动我的应用程序的新实例,即使我已在清单文件中将android:launchMode =“singleTop”指定为我的主要活动,但我不知道这里正在发生什么。我想要在此处打开的期望活动是RateEventActivity。
这里发生的奇怪的事情是,即使应用程序在后台模式下,通知活动和个人资料活动也可以正常工作,但是RateEventActivity作为我上面描述的所有问题产生了问题。大部分应用程序代码由其他人编写, 我正在处理RateEevent模块,因此我不知道我错过了什么?
在RateEventActivity场景中,当我从后台模式中单击通知时,意图会丢失(在创建时未传递给mainActivty)。
我还尝试了将唯一ID传递给待定意图,但没有成功。我投入了大量时间和精力来解决这个问题,但每次都失败了。
以下是我的FCM代码。
以下是我遇到问题的用例。
1)当我在应用程序中使用FCM通知时,打开我想要的活动。数据传递到主活动中的onNewIntent。当应用程序处于前台时运行良好。
2)当处于后台模式(按下主页按钮)时,单击通知后,它会启动我的应用程序的新实例,即使我已在清单文件中将android:launchMode =“singleTop”指定为我的主要活动,但我不知道这里正在发生什么。我想要在此处打开的期望活动是RateEventActivity。
这里发生的奇怪的事情是,即使应用程序在后台模式下,通知活动和个人资料活动也可以正常工作,但是RateEventActivity作为我上面描述的所有问题产生了问题。大部分应用程序代码由其他人编写, 我正在处理RateEevent模块,因此我不知道我错过了什么?
在RateEventActivity场景中,当我从后台模式中单击通知时,意图会丢失(在创建时未传递给mainActivty)。
我还尝试了将唯一ID传递给待定意图,但没有成功。我投入了大量时间和精力来解决这个问题,但每次都失败了。
以下是我的FCM代码。
private void createNotification(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Log.v("Notification Title", remoteMessage.getNotification().getTitle());
Intent intent = new Intent(this, MainActivity.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (remoteMessage.getData().containsKey("notificationUser")) {
intent.putExtra("notificationUser", remoteMessage.getData().get("notificationUser"));
} else if (remoteMessage.getData().containsKey("notificationId")) {
intent.putExtra("notificationId", remoteMessage.getData().get("notificationId"));
} else if (remoteMessage.getData().containsKey("event")) {
if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("Event Feedback")) {
Log.v("Notification Event", remoteMessage.getData().get("event"));
intent.putExtra("eventId", remoteMessage.getData().get("event"));
}
}
//PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent contentIntent = PendingIntent.getActivity(this, 0 , intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(largeIcon)
.setContentTitle(notification.getTitle())
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notification.getBody()))
.setContentText(notification.getBody())
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
主Activity代码onCreate
方法
if (getIntent().hasExtra("notificationId")) {
Intent intent = new Intent(this, NotificationActivity.class);
intent.putExtra("notificationId", getIntent().getStringExtra("notificationId"));
startActivity(intent);
} else if (getIntent().hasExtra("user")) {
Intent intent = new Intent(this, ProfileActivity.class);
intent.putExtra("userId", getIntent().getStringExtra("user"));
startActivity(intent);
} else if (getIntent().hasExtra("eventId")) {
Intent intent = new Intent(this, RateEventActivity.class);
intent.putExtra("eventId", getIntent().getStringExtra("eventId"));
startActivity(intent);
}
我的清单文件
<activity
android:name=".activities.MainActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/HomeTheme" />
<activity
android:name=".activities.ProfileActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="Profile"
android:screenOrientation="portrait" />
<activity
android:name=".activities.NotificationActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="Notifications"
android:screenOrientation="portrait" />
<activity
android:name=".activities.RateEventActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="Rate Users"
android:screenOrientation="portrait"
/>
Log.v("Notification Event", remoteMessage.getData().get("event"));
? - sohan shetty