【发布时间】:2014-03-25 04:26:22
【问题描述】:
我有一个有 4 层/堆栈的应用程序,例如,
首页 >> 详情页 >> 地图页 >> 聊天页
每当我收到不同的聊天消息时,我都会创建通知(请注意,每个对话都有其唯一的 ID,例如 ChatId)
对于不同的对话,我将使用 ChatId 作为 requestCode 在 pendingIntent
中创建一个通知我面临的问题是,每当我收到超过 1 条通知时,都说首先是 ChatId1,然后是 ChatId2。然后当我点击 ChatId1 的通知时,它会启动最新的 Intent 即 ChatId2,使 ChatPage 中加载的对话为对话ChatId2。
问题是,我怎样才能做到,当我点击 ChatId1 的通知时,它会加载 ChatId1 的消息,当点击 ChatId2,加载ChatId2的消息。
下面是我创建通知的代码的 sn-p
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());
}
}
【问题讨论】:
-
为每个通知传递的 id 必须是唯一的,以便启动多个通知意图,否则最新的会覆盖其他的
-
@Rat-a-tat-a-tatRatatouille 是的,我传递的chatId 是唯一的,我能够收到多个通知,只是当我点击chatid 1 的通知时,最新的最终调用了来自聊天 id 2 的意图。
-
也许您可以使用 id 本身来区分它?比如传递唯一的id,然后在activity中根据收到的id显示相关数据
标签: android android-intent android-notifications android-pendingintent