How to open the same activity after clicking differences notifications

I will spend a lot of time to solve my problem. I wrote a messenger client in android. My application receives an income message and prepares a notification. the notification panel displays each income message in the notification item. when you click on the notification item, it will open the conversation activity to list all messages from the very beginning until now. all dose is perfect, but when I click another item in the notification bar, nothing happens! (he should reload the data for another conversation). This is my notification code:

private void showNotification(String message, Class activity, Message messageObject) { //Get the Notification Service NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); CharSequence text = message;//getText(R.string.service_started); Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis()); notification.flags |= Notification.FLAG_AUTO_CANCEL; Intent callbackIntent = new Intent(context, activity); if(messageObject != null) { callbackIntent.putExtra("conversation", MessageManager.getProvider().getConversation(messageObject.getConversationId())); } //callbackIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); int myUniqueValue = new Random().nextInt(); PendingIntent contentIntent = PendingIntent.getActivity(context, myUniqueValue, callbackIntent, PendingIntent.FLAG_ONE_SHOT); notification.setLatestEventInfo(context, messageObject.getFrom(), text, contentIntent); notificationManager.notify(messageObject.getFrom(), myUniqueValue, notification); } 

This is the code block for calling the showNotification function.

 showNotification(message.getBody(), ConversationActivity.class, messageObject); 
+4
source share
2 answers

Despite your efforts to provide a unique value, those PendingIntent all treated equally by the system, so as soon as you click one, the others will be inert.

You need to add something other than callbackIntent ; I suggest inventing a data URI that includes a conversation identifier or something else that is guaranteed for different notifications (see setData ).

Finally, I would advise you to try to collapse several notifications into one icon - you do not want to spam the user. See the Notifications section of the Android Design Guide in the “Lay out your notifications” section.

0
source

I changed my code and it works very well

 private void showNotification(Context context, CharSequence contentTitle, CharSequence contentText, CharSequence notificationContent, Class activity, Conversation conversation) { //Get the Notification Service NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.ic_launcher, notificationContent, System.currentTimeMillis()); notification.flags |= Notification.FLAG_AUTO_CANCEL; Intent callbackIntent = new Intent(context, activity); if(conversation != null) { callbackIntent.putExtra("conversation", conversation); } callbackIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); int myUniqueValue = new Random().nextInt(); PendingIntent contentIntent = PendingIntent.getActivity(context, myUniqueValue, callbackIntent, PendingIntent.FLAG_ONE_SHOT); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); notificationManager.notify(myUniqueValue, notification); } 
-1
source

All Articles