我在某些應用程序的Android設備上顯示通知。每次觸發通知時,捆綁包都會保存該值,然後點擊通知時,會打開一個新的活動,並顯示捆綁包中的ID。單擊Android通知
現在我遇到了一個問題,即當一個通知帶有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);
}
感謝
感謝您的回覆。但是,如何獲取通知點擊通知的ID。 ??它如何識別ArrayList中的id? –
創建一個類來保存數據,我編輯我的答案 – Talha
它將如何得到哪個通知被點擊??? i。假設有兩個通知。一個來自用戶A,另一個來自用戶B.如何獲得點擊哪一個? –