Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification Duration for HeadsUp

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

like image 231
Serdar Nur Avatar asked Feb 19 '18 08:02

Serdar Nur


People also ask

What are heads-up notification?

Beginning with Android 5.0, notifications can briefly appear in a floating window called a heads-up notification. This behavior is normally for important notifications that the user should know about immediately, and it appears only if the device is unlocked.

How do you count notifications?

Notification Counter basically counts the notifications that you got through an application and shows on the top of your application icon so that you get to know you get new messages or any new update without opening your application or specific feature like the message button in Instagram.


1 Answers

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)

Update Android 10

You need to add priority along with category for on going notification.

val fullScreenIntent = Intent(this, CallActivity::class.java)
val fullScreenPendingIntent = PendingIntent.getActivity(this, 0,
    fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT)

val notificationBuilder =
        NotificationCompat.Builder(this, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Incoming call")
    .setContentText("(919) 555-1234")
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setCategory(NotificationCompat.CATEGORY_CALL)

    // Use a full-screen intent only for the highest-priority alerts where you
    // have an associated activity that you would like to launch after the user
    // interacts with the notification. Also, if your app targets Android 10
    // or higher, you need to request the USE_FULL_SCREEN_INTENT permission in
    // order for the platform to invoke this notification.
    .setFullScreenIntent(fullScreenPendingIntent, true)

val incomingCallNotification = notificationBuilder.build()

Source

like image 155
Aks4125 Avatar answered Sep 24 '22 12:09

Aks4125