Reputation: 191
I need help with notifications on Android. My server send request (to firebase server) with unique id for new notification. If my device will receive it, new notification is created and notified. If I will send next one request with other unique id, new notification is created. There are two notifications on statusbar. Each notifications has some data in Intent and I want to show it in Activity. But after clicking on first or second or last (if device received 2 or more notifications) it cause start Activity (with Intent) from last received (top of notification stack). I think this problem is in some Flag on Intent or PendingIntent.
@Override
public void onMessageReceived(RemoteMessage respose ) {
JSONObject response_intent=null;
try {
response_intent= new JSONObject( respose.getData().get("intent"));
} catch (JSONException e) {
e.printStackTrace();
}
int unique_id=Integer.valueOf(respose.getData().get("id_original"));
/*After notification click to open Activity ... */
Intent intent = new Intent("eu.energochemica.cat_notifications.DETAIL_SCREEN");
/*... with data from firebase via Intent */
intent.putExtra("fromNotification", true);
intent.putExtra("intentFromNotification", respose.getData().get("intent"));
intent.putExtra("id_original",unique_id);
intent.putExtra("header",respose.getData().get("header"));
intent.putExtra("text",respose.getData().get("text"));
/*HERE? What FLAG to use?* Intent.FLAG_ACTIVITY_NEW_TASK */
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
/*OR HERE? What FLAG to use?*/
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, Config.NOT_CHANNEL_ID)
.setSmallIcon(this.getResources().getIdentifier("cat_logo_white", "drawable", this.getPackageName()))
.setContentTitle(respose.getData().get("header"))
.setContentText(respose.getData().get("text"))
.setAutoCancel(false)
.setChannelId(Config.NOT_CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" +this.getPackageName()+"/"+R.raw.notif))
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(unique_id, notificationBuilder.build());
}
I need (if I received f.e. four notification in row: 1, 2, 3, 4 , where 4 is the last one (newest)) to open Activity with right Intent. (click on notification 1 (oldest) will open Activity with Intent from this notification 1 and will not open Intent from notification 4).
I dont know how and what I have to do with code, FLAG. Can anybody help me?
Upvotes: 5
Views: 466
Reputation: 191
Oh my bad...
I found out problem place ... After reading official description PendingIntent.class
I tried pass unique_id to 2nd parameter [int requestCode] in
PendingIntent.getActivity(this, u_id, intent, ...
instead
PendingIntent.getActivity(this, 0, intent, ...
where constant 0 means "rewriting" same PendingIntent around (in my opinion) => 0 was as ID for PendingIntent.
Upvotes: 6