我在某些应用程序的Android设备上显示通知。每次触发通知时,捆绑包都会保存该值,然后在点击通知时打开一个新活动,并在捆绑包中显示该ID。
现在我遇到一个问题,当一个通知带有id:A并同时如果用户获得带有id的通知:B。现在它将导致bundle值为 - 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);
}
由于
答案 0 :(得分:1)
我找到了解决方案,
下载此库并添加您的项目
https://github.com/JakeWharton/NotificationCompat2/downloads
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.notification.R.id;
import com.jakewharton.notificationcompat2.NotificationCompat2;
public class MainActivity extends Activity {
Button btn;
int i = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Click button 3-4 times and there will be natification and
// click one of them
notificationforChat("Message" + i,"sender" + i,i);
//notificationforChat("sender" + i, "Message" + i, i);
i++;
}
});
}
public void notificationforChat(String message, String sender,int notificationID) {
Bundle bundle = new Bundle();
bundle.putString("key", message);
NotificationManager notifManager = (NotificationManager) this
.getSystemService(this.NOTIFICATION_SERVICE);
int uniqueInteger = notificationID;// create a unique Integer
int icon = R.drawable.ic_launcher;
NotificationCompat2.Builder mNotification = new NotificationCompat2.Builder(
this).setSmallIcon(icon).setContentTitle(sender)
.setContentIntent(getPendingIntent(bundle, uniqueInteger));
mNotification.setDefaults(Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
mNotification.setAutoCancel(true);
notifManager.notify(uniqueInteger, mNotification.build());
}
private PendingIntent getPendingIntent(Bundle bundle, int rc) {
Intent notificationIntent = new Intent(MainActivity.this, UserChatActivity.class);
notificationIntent.putExtras(bundle);
return PendingIntent.getActivity(this, rc, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
然后在userChatActivity上,
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class UserChatActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent intent = getIntent();
String key = intent.getStringExtra("key");
Log.v("notifica",key);
}
}
答案 1 :(得分:0)
似乎最新的一个总会被打开,这不是你想要的吗?
在您描述的情况下,您是否希望有2个单独的通知?一个用于:A和一个用于:B?