Android通知,当有多个通知时,仅加载最新的意图

时间:2014-03-25 04:26:22

标签: android android-intent android-notifications android-pendingintent

我的应用程序有4层/堆栈,类似于

  

首页>> DetailPage>> MapPage>> ChatPage

我设法在收到不同的聊天消息时创建通知(请注意,每个会话都有唯一的ID,例如 ChatId

对于不同的对话,我将使用 ChatId 作为pendingIntent

中的 requestCode 创建通知

我面临的问题是,每当收到超过1条通知时,首先会说 ChatId1 ,然后是 ChatId2 。然后,当我点击 ChatId1 的通知时,它会启动最新的 ChatId2 意图,使在 ChatPage 中加载的对话成为对话 ChatId2

问题是,我怎样才能这样做,当我点击 ChatId1 的通知时,它会加载 ChatId1 的消息,并在点击时ChatId2 ,它会加载 ChatId2 的消息。

以下是我创建通知的代码片段

public void createLocalNotification(String meetingIdLong, String countryCode, String phoneNumber, String contactName, String chatMessage) {

            // Prepare intent which is triggered if the
            // notification is selected
            Intent intent = new Intent(this, ChatLog.class);
            long L = Long.parseLong(meetingIdLong);
            int a = Integer.parseInt(meetingIdLong);
            intent.putExtra("meetingId", L);

            Intent intent1 = new Intent(this, HomePage.class);

            Intent intent2= new Intent(this, MeetingDetailPage.class);
            intent2.putExtra("meetingId", L);
            Intent intent3 = new Intent(this, MapPage.class);
            intent3.putExtra("meetingId", L);

            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
            stackBuilder.addParentStack(ChatLog.class);
            stackBuilder.addNextIntent(intent1);
            stackBuilder.addNextIntent(intent2);
            stackBuilder.addNextIntent(intent3);
            stackBuilder.addNextIntent(intent);

            PendingIntent pIntent = stackBuilder.getPendingIntent(a, PendingIntent.FLAG_UPDATE_CURRENT);

            String notificationSound = _defPrefs.getString("pref_notification_tone", "");

            NotificationCompat.Builder noti = new NotificationCompat.Builder(this)
            .setContentTitle(contactName)
            .setContentText(chatMessage)
            .setSmallIcon(R.drawable.noti_smallicon)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.noti_smallicon))
            .setSound(Uri.parse(notificationSound))
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setNumber(++numberMsg)
            .setAutoCancel(true)
            .setContentIntent(pIntent);

            NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
            String[] events = new String[6];
            events [numberMsg - 1] = new String(chatMessage);

            // Sets a title for the Inbox style big view
            inboxStyle.setBigContentTitle(contactName);
            inboxStyle.setSummaryText(numberMsg + " new message(s)");

            for (int i=0; i < events.length; i++) {

                inboxStyle.addLine(events[i]);
            }
            noti.setStyle(inboxStyle);

            String isChatOpen, isChatOpenId;

            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
            isChatOpen = preferences.getString("isChatOpen","");
            isChatOpenId = preferences.getString("isChatOpenId","0");

            if (!isChatOpenId.equals(meetingIdLong))
            {
                NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(a, noti.build());
            }

         }

1 个答案:

答案 0 :(得分:1)

使用

intent.setAction(Long.toString(System.currentTimeMillis()));

为每个你想要独特的意图。它只是一个虚拟动作,但它有所帮助。