I updated my project api target to 27 and all notifications were disabled.
What's the difference between notification in api 26 and 27?
Notification notif = new NotificationCompat.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText(message)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.setSound(alarmSound)
.setAutoCancel(true).build();
notif.ledARGB = 0xFFff0000;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
NotificationManager notificationCompatManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationCompatManager.notify(0, notif);
As stated in the Android developers website:
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all.
So You can use this method to show notification in both -27 and +27 apis :
Java :
void showNotification(String title, String message) {
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID",
"YOUR_CHANNEL_NAME",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("YOUR_NOTIFICATION_CHANNEL_DESCRIPTION");
mNotificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "YOUR_CHANNEL_ID")
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle(title) // title for notification
.setContentText(message)// message for notification
.setAutoCancel(true); // clear notification after click
Intent intent = new Intent(getApplicationContext(), ACTIVITY_NAME.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pi);
mNotificationManager.notify(0, mBuilder.build());
}
Kotlin :
fun showNotification(title: String, message: String) {
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val channel = NotificationChannel("YOUR_CHANNEL_ID",
"YOUR_CHANNEL_NAME",
NotificationManager.IMPORTANCE_DEFAULT)
channel.description = "YOUR_NOTIFICATION_CHANNEL_DESCRIPTION"
mNotificationManager.createNotificationChannel(channel)
}
val mBuilder = NotificationCompat.Builder(applicationContext, "YOUR_CHANNEL_ID")
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle(title) // title for notification
.setContentText(message)// message for notification
.setAutoCancel(true) // clear notification after click
val intent = Intent(applicationContext, ACTIVITY_NAME::class.java)
val pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
mBuilder.setContentIntent(pi)
mNotificationManager.notify(0, mBuilder.build())
}
Note: If you want to show heads-up notification, you can set the importance of your channel and your notification as HIGH, AND remove your app and install it again.
User contributions licensed under CC BY-SA 3.0