我在我的某些应用程序中的 android 设备上显示通知。每次触发通知时,bundle 都会保存该值,然后在单击通知时会打开一个带有 bundle 中 id 的新活动。
现在我遇到了一个问题,当一个通知带有 id :A 并且同时如果用户收到带有 id 的通知:B。现在它将导致捆绑值 - B。现在如果单击任何通知,那么该页面将使用新的通知值打开。这是 id 更新的主要问题。
我要求在通知点击右侧页面应始终打开
发送通知
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
// TODO Auto-generated method stub
Message message = (Message) packet;
senderName = packet.getFrom();
// new code
mMessageItem = new MessageItemDataClass();
mMessageItem.isSendMessage = false;
mMessageItem.messageText = message.getBody();
int alphaPOS = senderName.indexOf("@");
String subSenderName = senderName.substring(0, alphaPOS);
refinedID = refineFromjId(packet.getFrom());
Random rand = new Random();
int randNotificationVal = rand.nextInt(1000);
String notificationID = ""+randNotificationVal;
while(idMap.containsValue(notificationID)){
randNotificationVal = rand.nextInt(1000);
notificationID = ""+randNotificationVal;
}
if(!idMap.containsKey(refinedID)){
saveNotificationID(refinedID, notificationID);
}
if (UserChatActivity.checkPresence == true) {
if (packet.getFrom().equalsIgnoreCase(
refineFromjId(UserChatActivity.frienduserID)
+ "/Smack")) {
UserChatActivity.messages.add(mMessageItem);
} else {
notificationforChat(
subSenderName + ": " + message.getBody(),
packet.getFrom().toString(), Integer.parseInt(idMap.get(refinedID))
);
}
} else {
notificationforChat(
subSenderName + ": " + message.getBody(), packet
.getFrom().toString(), Integer.parseInt(idMap.get(refinedID))
);
}
public void notificationforChat(CharSequence message, String toJid,
int notificationID) {
int notificationCount = 1;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//notification.number = notificationCount++;
Context context = getApplicationContext();
CharSequence contentTitle = "Chat";
CharSequence contentText = message;
Intent notificationIntentforChat = new Intent(this,
UserChatActivity.class);
notificationIntentforChat.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntentforChat.putExtra("userNameVal", toJid);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntentforChat, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_ALL;
mNotificationManager.notify(notificationID, notification);
}
检查捆绑包的 UserChatActivity
try {
Bundle bundle = getIntent().getExtras();
if (bundle.getString("userNameVal") != null) {
frienduserID = bundle.getString("userNameVal");
int index_of_Alpha = frienduserID.indexOf("@");
String subID = frienduserID.substring(0, index_of_Alpha);
mtvChatTitle.setText(subID);
//System.out.println("TRY == "+frienduserID);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
//System.out.println(""+frienduserID);
}
谢谢