如何在点击差异通知后打开同一活动

问题描述:

我花了很多时间来解决我的问题。我已经在android中编写了一个messenger客户端。我的申请收到收入消息并提出通知。在通知栏中,我将每个收入消息显示在通知项目中。当点击通知项目时,它将打开对话活动以列出从开始到现在的所有消息。一切都很完美,但是当我点击通知栏中的其他项目时,什么也没有发生! (它必须为另一个对话重新加载数据)。这是我的代码,以提高一个通知:如何在点击差异通知后打开同一活动

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); 
    } 

这是代码块调用showNotification功能

showNotification(message.getBody(), ConversationActivity.class, messageObject); 

尽管你的努力提供独特的价值,那些PendingIntent s的全部视为相同的该系统,所以一旦你点击一个,其余的都是惰性的。

你需要添加一些区别callbackIntent;我建议发明一个数据URI,其中包含会话ID或其他保证每个通知不同的其他内容(请参阅setData)。

最后,我建议您尝试将多个通知合并为一个图标 - 您不希望发送该用户的垃圾邮件。请参阅“堆叠您的通知”下的Notifications section in the Android Design guide

我改变了我的代码,它工作得很好

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); 
    }