android - 通知立即取消

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

最佳答案

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

相关文章:

ios - 安排 iOS 本地推送通知

android - 选项卡栏设置作为平板电脑中的叠加层?

ios - @react-native-firebase/messaging : TypeError: (0 , _messaging.default)(...).registerForRemoteNotifications 不是一个函数

android - 从 .gradle 和 .idea 中删除所有内容是否安全?

带有 RemoteViews 的 Android 5+ 自定义通知 XML 布局,为 ImageButton 设置正确的图标色调

ios - 在 ios 中使用 asp.net 推送通知

javascript - 在 Windows 窗体中使用 javascript 的桌面通知

ios - 从服务器删除已发送的推送通知

android - 如何连接eclipse和GTV设备

android - 我应该在字段分隔符等装饰图像的 contentDescription 属性中添加什么?