i have tried almost every solution posted here and combination of every flag but it is not working.
Following are use cases in which i am having problems.
1) When i am in application FCM notification opens my desired activity.
Data is passed to onNewIntent
in main activity. It works fine when application is foreground.
2) When in background mode(presssing home button) after clicking on notification it launches new instance of my application even though i have specified android:launchMode="singleTop"
in manifest file to my main activity but i don't know what is happening here. Desired Activity i want to open here is RateEventActivity
the strange thing happening here is that NotificationActivity
and ProfileActivity
are working perfectly even when application is in background mode but RateEventActivity
is producing all the problem as i described above. Most of application code is written by someone else i am working on RateEevent
module so i don't know what i am missing here?
Intents are lost somewhere (not passed to mainActivty
on create) when i am clicking notification from background mode in RateEventActivity
scenario.
i have also tried passing unique id to my pending intent but no luck. I have put lot of time and effort to solve this but failing every time
here is my FCM code
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());
}
Main Activity Code onCreate
Method
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);
}
My Manifest File
<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"
/>