我在我的应用程序中使用这样的本地通知。
showNotification(this, "Title1", "Message One", 1);
showNotification(this, "Title2", "Message Two", 2);
showNotification(this, "Title3", "Message Three", 3);
showNotification(this, "Title4", "Message Four", 4);
public static void showNotification(Context con, String title,
String message, int id) {
NotificationManager manager = (NotificationManager) con
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.ic_noti_logo,title, System.currentTimeMillis());
Intent notificationIntent = new Intent(con,Result.class);
notificationIntent.putExtra("Message", message);
notificationIntent.putExtra("NotiId", id);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(con, 0,
notificationIntent, PendingIntent.FLAG_ONE_SHOT);
note.setLatestEventInfo(con, title, message, pi);
note.defaults |= Notification.DEFAULT_ALL;
note.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(id, note);
}
在 Resut.java 中
message = getIntent().getStringExtra("Message");
notiId = getIntent().getIntExtra("NotiId", 0);
showAlert(message,notiId);
private void showAlert(String msg, int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(msg).setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// finish();
cancelNotification(Result.this,id);
}
});
AlertDialog alert = builder.create();
alert.show();
}
public static void cancelNotification(Context con, int id) {
NotificationManager manager = (NotificationManager) con
.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(id);
}
我的问题是,我在通知栏中收到 4 条通知消息,当我单击其中任何一条时,我正在重定向到Result
Activity,但它仅在我第二次单击时出现,但没有任何效果。请帮我。