几秒钟后,Android通知在棉花糖和棒棒糖上消失

时间:2018-07-12 11:05:09

标签: android firebase push-notification android-notifications android-6.0-marshmallow

Android Firebase通知会在几秒钟后在棉花糖和棒棒糖版本上自动消失。如何保持此版本的通知,直到用户点击它或手动清除它?

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_id);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

1 个答案:

答案 0 :(得分:0)

也许是问题所在(很难提供提供的信息)

notificationManager.notify(0 /* ID of notification */

如果您始终将通知ID设置为0,则每个新通知都会覆盖旧通知see specs.

  

如果您的应用程序已经发布了具有相同ID的通知,但尚未取消,则该通知将被更新的信息替换。