我需要在我的应用程序中创建通知。我正在这样构建它:
protected void onCreate(Bundle savedInstanceState) {
[...]
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo_ico)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
我的问题是我的通知在显示半秒后立即被取消。我该如何解决?
更新:
这可能与我来自框架的其他通知有关吗?可以有多个通知,不是吗?
更新 2:
当我将 notificationManager.notify(1, mBuilder.build());
放在另一个方法中时,通知会一直显示到该方法结束。
最佳答案
尝试在 mBuilder 上设置标志:
mBuilder.setFlags(Notification.FLAG_NO_CLEAR);
是的,可以有多个通知。要显示第二个通知,请使用另一个 ID。例如notificationManager.notify(2, mBuilder.build());
http://developer.android.com/reference/android/app/Notification.html#FLAG_NO_CLEAR
关于android - 通知立即取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31117050/