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.