Notification Duration for HeadsUp

2019-07-31 13:48发布

Is it possible to set the duration of the headsup notification to unlimited? right now its displayed just for 5 seconds. Already tried different things like changing the category. But the duration always is 5 seconds.

Here is my code:

Notification notification =
            notificationBuilder
                    .setCategory(Notification.CATEGORY_CALL)
                    .setContentText("TKSE Werk Duisburg")
                    .setSmallIcon(R.drawable.ic_tk_individual_signet_logo)
                    .setOngoing(true)
                    .setAutoCancel(false)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setContentIntent(contentIntent)
                    .setCustomHeadsUpContentView(viewNotificationHeadsUp)                                                      .setCustomContentView(viewNotificationSmall)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setVibrate(new long[20]).build();

Tried the same things like on this thread: Controlling Android Notification Duration for HeadsUp but it did not help me.

Interesting fact: On my Developer Phone a Samsung S5 mini -> its displayed with no time limit On another Developer Phone a Samsung S7 -> its displayed 5 seconds

2条回答
Summer. ? 凉城
2楼-- · 2019-07-31 13:54

This should work. I have added extra onGoing(true) & category call for notification category.

   NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);

        // Configure the notification channel.
        notificationChannel.setDescription("Channel description");
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }

    // assuming your main activity
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, NOTIFICATION_CHANNEL_ID);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
    notificationBuilder.setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setCategory(Notification.CATEGORY_CALL)
            .setOngoing(true)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("Hearty365")
            .setPriority(Notification.PRIORITY_MAX)
            .setContentTitle("Default notification")
            .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
            .setFullScreenIntent(pendingIntent,true)
            .setContentInfo("Info");

    notificationManager.notify(/*notification id*/1, notificationBuilder.build());

PS. import android.app.Notification; for setting notification category (call)

查看更多
Melony?
3楼-- · 2019-07-31 14:12

Try that solution (the one that you have posted as the link..) but after doing that change dont forget to change the NOTIFICATION_ID sometimes if the notification id is not changed some of the previous attributes of the notifications stays .. so

Change the notification id when you change any attribute of notification builder

查看更多
登录 后发表回答