Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show heads up notification in Oreo or higher

Tags:

android

I think heads up notification disable in android 8.0 but android's built-in message have this feature even on running android 8.0 . I know how to show heads up notification lower than 8.0 device by setting vibration to notification. But vibration on notification running Oreo is deprecated. So i go and search in internet and someone says enabling setVibrationPattern to notification channel will works. But saddly this not works .

So here is my code .

Notification  builder = new NotificationCompat.Builder(MainActivity.this)
                            .setAutoCancel(true)
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.drawable.ic_android_black_24dp)
                            .setChannelId(id)
                            .setPriority(Notification.PRIORITY_HIGH)
                            .setColor(setActionColor(counter))
                            .setColorized(true)
                            .setContentIntent(pendingIntent)
                            .build();

//Notification channel code.

NotificationChannel chan2 = new NotificationChannel(SECONDARY_CHANNEL,
                            getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH);
                    chan2.setLightColor(Color.BLUE);
                    chan2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                    manager.createNotificationChannel(chan2);
                    manager.notify(notificationID, builder);
like image 359
Rk215 Tech Avatar asked Dec 25 '17 15:12

Rk215 Tech


People also ask

How do I get notifications on my Android Oreo?

Notification channels allow you to create a user-customizable channel for each type of notification you want to display. So, if the android version is greater or equals to 8.0, we have to create the NotificationChannel object and set it to createNotificationChannel(…) setter property of NotificationManager.

How do I see pop up notifications?

If you want the notifications to turn on the screen, go to Settings> Notifications> More notification settings and turn on the switch for Notifications turn on screen. - open WhatsApp, tap in the upper right corner, go to Settings > Notifications > Popup notification and select Always show popup.


4 Answers

Late Answer :- But i think its worth sharing. I was doing the same thing as above but Heads-up notifications won't shows in Oreo.

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = 
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = 
        new NotificationChannel("ch_nr", "CHART", NotificationManager.IMPORTANCE_HIGH);
    notificationChannel.setVibrationPattern(new long[]{300, 300, 300});

    if (defaultSoundUri != null) {
        AudioAttributes att = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        notificationChannel.setSound(defaultSoundUri, att);
    }

    notificationManager.createNotificationChannel(notificationChannel);
    notificationBuilder = 
        new NotificationCompat.Builder(context, notificationChannel.getId());
}

Essential code is above . The problem was i was overriding build each time on device and the importance did not get change to HIGH . I did not understand this until i came across this document .

Importance levels have the following restrictions:

The importance level you assign will be the channel’s default. Users can change a channel’s importance level in Android Settings. Once you choose an importance level, you're limited in how you can change it: you may only lower the importance, and only if the user hasn't explicitly changed it.

Conclusion:- Completely uninstall previous build or Clear Data(not sure about this) before upgrading the importance for notification channel.

like image 52
ADM Avatar answered Oct 17 '22 16:10

ADM


You can do this way.. this will work on every android version

private NotificationManager notifManager;
public void createNotification(String aMessage) {
    final int NOTIFY_ID = 1002;

    // There are hardcoding only for show it's just strings
    String name = "my_package_channel";
    String id = "my_package_channel_1"; // The user-visible name of the channel.
    String description = "my_package_first_channel"; // The user-visible description of the channel.

    Intent intent;
    PendingIntent pendingIntent;
    NotificationCompat.Builder builder;

    if (notifManager == null) {
        notifManager =
                (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = notifManager.getNotificationChannel(id);
        if (mChannel == null) {
            mChannel = new NotificationChannel(id, name, importance);
            mChannel.setDescription(description);
            mChannel.enableVibration(true);
            mChannel.setLightColor(Color.GREEN);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notifManager.createNotificationChannel(mChannel);
        }
        builder = new NotificationCompat.Builder(this, id);

        intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        builder.setContentTitle(aMessage)  // required
                .setSmallIcon(android.R.drawable.ic_popup_reminder) // required
                .setContentText(this.getString(R.string.app_name))  // required
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setTicker(aMessage)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    } else {

        builder = new NotificationCompat.Builder(this);

        intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        builder.setContentTitle(aMessage)                           // required
                .setSmallIcon(android.R.drawable.ic_popup_reminder) // required
                .setContentText(this.getString(R.string.app_name))  // required
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setTicker(aMessage)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setPriority(Notification.PRIORITY_HIGH);
    } // else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    Notification notification = builder.build();
    notifManager.notify(NOTIFY_ID, notification);
}
like image 29
Muhaiminur Rahman Avatar answered Oct 17 '22 14:10

Muhaiminur Rahman


You also need to set the App notification setting in "Settings" to Urgent. A Heads up notification will not show up unless the User sets the App to urgent.

like image 41
user8555999 Avatar answered Oct 17 '22 15:10

user8555999


Heads up notifications works if you create Notification Channel and do something like this.

NotificationChannel chan2 = new NotificationChannel(SECONDARY_CHANNEL,
                getString(R.string.noti_channel_second), NotificationManager.IMPORTANCE_HIGH);
        chan2.setLightColor(Color.BLUE);
        chan2.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(chan2);
like image 20
lib4 Avatar answered Oct 17 '22 15:10

lib4