Android push header/heads-up notification doesn't work

294 Views Asked by At

I am currently trying to get a header notification on a Nexus S (API 29), but with no success. I've seen some threads with various solutions, but none of them worked. The code is as follows:

Context applicationContext = getApplicationContext();    
NotificationCompat.Builder builder = new NotificationCompat.Builder(applicationContext, CHANNEL_ID)
                                .setDefaults(NotificationCompat.DEFAULT_ALL)
                                .setDefaults(NotificationCompat.DEFAULT_SOUND)
                                .setSmallIcon(R.drawable.ic_account_circle)
                                .setContentTitle(notificationTitle)
                                .setContentText(notificationTextBody)
                                .setPriority(NotificationCompat.PRIORITY_HIGH)
                                .setLights(Color.RED, 3000, 3000);

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(applicationContext);



int notificationId = new Random().nextInt();
//launches the notification on the device
notificationManager.notify(notificationId, builder.build());

I have tried setting sound on, vibrations on, to set the priority as high or max. I have even used the method setFullScreenIntent, separately and all at once, but nothing accomplished, what I seek.

1

There are 1 best solutions below

0
On

on Android 8+ you have to create notification channel, like in DOC

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

note that channel also have importance. if you want a heads-up notification you have to set high priority for this channel, DOC

Example conditions that might trigger heads-up notifications include the following:

The notification channel has high importance on devices running Android 8.0 (API level 26) and higher.