在我的Android应用中,我向用户发送通知,当他们单击该通知时,它会打开MainActivity中的特定片段。通知仅在一半时间内打开该特定片段。当应用仍然在设备的后台时,当用户单击通知时,它将打开到该特定片段;但是当应用不在背景中并且用户从通知中将其打开时,则不会打开该特定片段。
问题与我随通知传递的意图中的字符串多余有关,它为null。
这是在我的MainActivity的OnCreate(...)
String newAddedGamesFragment = getIntent().getStringExtra("FragmentFromNotification");
// NOTIFICATION CLICK
if (newAddedGamesFragment != null) {
if (newAddedGamesFragment.equals("NewAddedReleases")) {
Log.d(TAG, "From notification");
fragmentManager.beginTransaction().hide(mCurrentFragment).show(mNewlyAddedReleasesFragment).commit();
mCurrentFragment = mNewlyAddedReleasesFragment;
initFragmentFeatures(4);
}
} else {
Log.d(TAG, "Not found!!");
}
这是我将意图传达给通知的方式。传递的意图将打开MainActivity并另外定义一个新字符串,名称为FragmentFromNotification
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("FragmentFromNotification", "NewAddedReleases");
PendingIntent notificationIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(body)
.setContentIntent(notificationIntent)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(), notificationBuilder.build());