Reputation: 735
I am new to Android and I have been trying to show a Heads up action push notification just like whatsapp does.
This is my configuration for my notification:
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, ADMIN_CHANNEL)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_foreground))
.setSmallIcon(R.drawable.m_icon)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setTicker(remoteMessage.getNotification().getBody())
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.mblue))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(remoteMessage.getNotification().getBody()))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(likePendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0,notificationBuilder.build());
I have read in some post that the key to acomplish this is to set high priority to the notification but, It is still not working for me.
Upvotes: 3
Views: 2597
Reputation: 1
Just Add This Line In Your Notification Channel Builder:-
channel.setShowBadge(true)
Upvotes: 0
Reputation: 279
For Android Pie (9) on the emulator, I setup my channel with "IMPORTANCE_HIGH", and also set my builder priority to "PRIORITY_HIGH".
But, I still could not get heads-up notifications!
Finally, on the Android 9 emulator, I had to:
Now it works.
Upvotes: 2
Reputation: 735
The issue is that we have to add a new channel configuration whenever the OS is Oroe. So I had to validate if the OS is Oreo and then asign a channel to my notificationManager:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
setupChannels();
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupChannels(){
CharSequence adminChannelName ="Global channel";
String adminChannelDescription = "Notifications sent from the app admin";
NotificationChannel adminChannel;
adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_HIGH);
adminChannel.setDescription(adminChannelDescription);
adminChannel.enableLights(true);
adminChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
adminChannel.setLightColor(Color.RED);
adminChannel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(adminChannel);
}
}
Upvotes: 0