通知会立即取消

时间:2015-06-29 13:31:05

标签: android notifications push-notification

我需要在我的应用中创建通知。我正在构建它:

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());放入另一个方法时,会显示通知,直到该方法结束。

1 个答案:

答案 0 :(得分:1)

尝试在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

相关问题