Does not show notification on Android 9.0 and works great on 8 and below. I had trouble with old code and I read that is needed to pass Channel on new android version, I got this code from internet but still is not working on Android 9.0. I don't know why. Any help will be appreciated.
As I read through internet everything looks good. But notification does not show.
private void showSmallNotification( int icon, String title, String message, String timeStamp, PendingIntent resultPendingIntent){
String CHANNEL_ID = Constants.ChannelID;
String CHANNEL_NAME = "Notification";
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);;
NotificationCompat.Builder notification;
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
if (Build.VERSION.SDK_INT >= 26) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setLightColor(Color.BLUE);
channel.enableLights(true);
channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.notification),
new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build());
channel.canShowBadge();
notificationManager.createNotificationChannel(channel);
notification = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
} else {
notification = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);
}
notification
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.BLUE, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(inboxStyle)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), icon))
.setContentText(message)
.build();
notificationManager.notify(CHANNEL_ID, 1, notification.build());
}
Okay this code now works for me, and also teseted until android 12 First I create Channel
// Create Channel For Notifications (if does not exist)
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) {
NotificationManager mNotificationManager = getSystemService(NotificationManager.class);
NotificationChannel existingChannel = mNotificationManager.getNotificationChannel(Constants.notification_channel_ID);
// If channel does not exist then create.
if (existingChannel == null) {
CharSequence name = getResources().getString(R.string.app_name);
String description = getResources().getString(R.string.notification_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel( Constants.notification_channel_ID , name , importance) ;
notificationChannel.setDescription(description);
notificationChannel.enableLights(true) ;
notificationChannel.setLightColor(Color.BLUE) ;
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
Maybe is not needed to check if channel exists but I check. Then when I need to show notification I call this method:
private void notification(){
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE) : PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, Constants.notification_channel_ID)
.setChannelId(Constants.notification_channel_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(this.getResources().getString(R.string.app_name))
.setContentText(this.getResources().getString(R.string.description))
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(Constants.NOTIFICATION_ID, builder.build());
}