防止通知自动隐藏

egdjgwm8  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(286)

我用它来显示通知:

public static void showAlarmNotification(Context context, Class<?> cls, String title, String content, int idNotification){
    Intent notificationIntent = new Intent(context, cls);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(cls);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(idNotification, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID).setPriority(NotificationCompat.PRIORITY_MAX);

    Notification notification = builder.setContentTitle(title)
            .setContentText(content)
            .setAutoCancel(false)
            .setOngoing(true)
            .setVibrate(new long[] {1000, 1000, 1000})
            .setSmallIcon(R.drawable.ic_stat_name)
            .setChannelId(NOTIFICATION_CHANNEL_ID)
            .setContentIntent(pendingIntent)
            .build();

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_ALARM, context.getString(R.string.alarms), NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.setSound(null,null);
        notificationChannel.setVibrationPattern(new long[] {1000, 1000, 1000});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }

    notificationManager.notify(idNotification, notification);
}

显示通知效果很好。但我不希望通知自动消失,因为它通常发生,我希望它只上升,如果用户刷了它,我看到应用程序这样做,但我没有找到一种方法来做
更新
此示例显示了一个通知,在用户刷起它之前它不会隐藏
示例(youtube)

zf2sa74q

zf2sa74q1#

这似乎是一个通知与一个设置的类别的类别报警。
另请参见系统范围类别

相关问题