【发布时间】:2023-03-17 16:52:01
【问题描述】:
我几乎尝试了此处发布的所有解决方案以及每个标志的组合,但它不起作用。
以下是我遇到问题的用例。
1) 当我在应用程序中时,FCM 通知会打开我想要的活动。
数据在主要活动中传递给onNewIntent。当应用程序在前台时,它可以正常工作。
2) 在后台模式下(按主页按钮)单击通知后,它会启动我的应用程序的新实例,即使我已在清单文件中为我的主要活动指定 android:launchMode="singleTop" 但我不知道这里发生了什么.我想在此处打开的所需活动是 RateEventActivity
这里发生的奇怪事情是 NotificationActivity 和 ProfileActivity 即使在应用程序处于后台模式时也能正常工作,但 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());
}
主要活动代码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"));`??
-
是的,我正在从推送通知中获取数据..
标签: android android-intent firebase-cloud-messaging