5

Been trying to find a way to create a persistent heads up notification, exactly like receiving a phone call or hangouts call. Are these restricted to Google/system apps?

I've tried:

  • Updating the notification before the heads up notification is removed.
  • Setting the ongoing flag to true.
  • Setting auto cancel to false

Can't seem to find this being done anywhere.

RyPope
  • 2,645
  • 27
  • 51

3 Answers3

5

It worked for me:

  • set ongoing flag to true
  • set full screen intent
  • set priority to PRIORITY_HIGH
  • set category to CATEGORY_CALL
Community
  • 1
  • 1
Vladimir
  • 116
  • 2
  • 4
  • I must've missed the setCategory for the builder. Thanks! https://developer.android.com/reference/android/app/Notification.Builder.html#setCategory(java.lang.String) – RyPope Oct 05 '16 at 20:42
0

Heads-Up Notifications (API level 21) :

Examples of conditions that may trigger heads-up notifications include:

  • The user's activity is in fullscreen mode (the app uses fullScreenIntent), or
  • The notification has high priority and uses ringtones or vibrations

Second case was enough for me to show heads-up notification. Here is a sample code:

Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("Title")
        .setContentText("Message")
        .setSmallIcon(R.drawable.ic_notification)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.your_sound))
//        .setOngoing(true)
        .build();

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(42, notification);

Running this sample code every second (depends on your_sound.mp3 duration...) did the trick.

Note, you may want to play silent.mp3 after the first time.

frouo
  • 5,087
  • 3
  • 26
  • 29
0

Just, add setOngoing to true. .setOngoing(true);, that`s how you make the notification (heads up), display longer, also no notification is now unswipeable, in the notification panel.

Marcello B.
  • 4,177
  • 11
  • 45
  • 65