Reputation: 542
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);
Upvotes: 12
Views: 15581
Reputation: 2056
In some phones, there is a status bar and notification setting where if the top preview of notification is set to no preview, then it won't allow any app to show heads up, setting it to banner will solve your problem if you have already set up your channel and notification properly.
I faced the same issue with the Android 9 OS, Vivo y1902 phone, which was resolved after making the above changes.
Upvotes: 0
Reputation: 21053
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.
Upvotes: 27
Reputation: 544
You are setting the wrong channel ID.
setChannelId(String channelId) specifies the channel the notification should be delivered on.
And here in your example you are setting some id
to your notification builder:
setChannelId(id)
and then creating a new secondary channel that you are not specifying: chan2
Here is my working example:
private static final String CHANNEL_ID = "CHANNEL_NO_1";
public void sendNotification(Context context) {
// Create your notification channel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "New riddle";
String description = "Riddle of the day";
int importance = NotificationManager.IMPORTANCE_HIGH;
// IMPORTANT: CHANNEL_ID
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 = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
// Get an instance of NotificationManager
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle("Riddle of the day:")
//IMPORTANT: CHANNEL_ID
.setChannelId(CHANNEL_ID)
// It won't show "Heads Up" unless it plays a sound
if (Build.VERSION.SDK_INT >= 21) mBuilder.setVibrate(new long[0]);
// Gets an instance of the NotificationManager service//
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(001, mBuilder.build());
}
One more thing, make sure to REINSTALL your app after making these changes.
Upvotes: 0
Reputation: 3152
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);
}
Upvotes: 13
Reputation: 41
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.
Upvotes: 4
Reputation: 3607
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);
Upvotes: 2