首页 > 解决方案 > 防止通知被自动隐藏

问题描述

我正在使用它来显示通知:

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)

标签: javaandroid

解决方案


解决方案是更改setContentIntentsetFullScreenIntent这是源


推荐阅读