41

I get Toast on Android 8.1 API 27:

Developer warning for package "my_package_name"
Failed to post notification on ...

Logcat includes next strings:

Notification: Use of stream types is deprecated for operations other than volume control

W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

E/NotificationService: No Channel found for pkg=my_package_name

The full information in the Toast and in Logcat can help in the localization this problem.

Andy Sander
  • 1,888
  • 1
  • 13
  • 16
  • Post the code please – Samuel Robert Oct 28 '17 at 15:07
  • 1
    Possible duplicate of [Notifications fail to display in Android Oreo (API 26)](https://stackoverflow.com/questions/45395669/notifications-fail-to-display-in-android-oreo-api-26) – Sky Kelsey Dec 14 '17 at 23:00
  • Related post - [NotificationCompat.Builder doesn't accept 2nd argument](https://stackoverflow.com/q/50765964/465053) – RBT Sep 03 '18 at 04:46

4 Answers4

98

If you get this error should be paid attention to 2 items and them order:

  1. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
  2. builder = new NotificationCompat.Builder(context, id);

Also NotificationManager notifManager and NotificationChannel mChannel are created only once.

There are required setters for Notification:

  • builder.setContentTitle() // required
  • .setSmallIcon() // required
  • .setContentText() // required

See example:

private NotificationManager notifManager;
public void createNotification(String aMessage, Context context) {
    final int NOTIFY_ID = 0; // ID of notification
    String id = context.getString(R.string.default_notification_channel_id); // default_channel_id
    String title = context.getString(R.string.default_notification_channel_title); // Default Channel
    Intent intent;
    PendingIntent pendingIntent;
    NotificationCompat.Builder builder;
    if (notifManager == null) {
        notifManager = (NotificationManager)context.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, title, importance);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            notifManager.createNotificationChannel(mChannel);
        }
        builder = new NotificationCompat.Builder(context, id);
        intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        builder.setContentTitle(aMessage)                            // required
               .setSmallIcon(android.R.drawable.ic_popup_reminder)   // required
               .setContentText(context.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(context, id);
        intent = new Intent(context, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        builder.setContentTitle(aMessage)                            // required
               .setSmallIcon(android.R.drawable.ic_popup_reminder)   // required
               .setContentText(context.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);
    }
    Notification notification = builder.build();
    notifManager.notify(NOTIFY_ID, notification);
}
Andy Sander
  • 1,888
  • 1
  • 13
  • 16
  • 1
    * I have set channel id, but notification still not shown; * Finally i found my problem is had not invoke "setContentText()" method * It was really help me that you mentioned "required setters"! – Jeffery Ma Mar 06 '18 at 12:19
  • Worked. Thanks a lot :) – Yesha Oct 09 '18 at 12:29
  • After many hours of trial and error, stumbled on this and voila! Thanks. – Maurice Aug 21 '19 at 21:05
  • 1
    Wonderfull tuto!! but to get it works I change NotificationComppat.Builder to Notificaction.Builder inside the if( SDK version) – San Juan Oct 19 '19 at 18:06
16

Andy's answer is working however I wanted to avoid to deprecated Builder and follow the FireBase Quickstart Project. I just added code before notify from manager.

String channelId = "default_channel_id";
String channelDescription = "Default Channel";
// Since android Oreo notification channel is needed.
//Check if notification channel exists and if not create one
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId);
    if (notificationChannel == null) {
        int importance = NotificationManager.IMPORTANCE_HIGH; //Set the importance level
        notificationChannel = new NotificationChannel(channelId, channelDescription, importance);
        notificationChannel.setLightColor(Color.GREEN); //Set if it is necesssary
        notificationChannel.enableVibration(true); //Set if it is necesssary
        notificationManager.createNotificationChannel(notificationChannel);
    }
}

//notificationManager.notify as usual

Edit: They removed the channel exist check from example I am not sure why.

Community
  • 1
  • 1
engincancan
  • 2,442
  • 2
  • 28
  • 43
10

I have set channel id, but notification still not shown.

Finally i found my problem is had not invoke "setContentText()" method.

It was really help me that @Andy Sander mentioned "required setters"!

here are required setters for Notification on Android 8 Oreo API 26 and later:

builder.setContentTitle() // required
.setSmallIcon() // required
.setContentText() // required
.setChannelId(id) // required for deprecated in API level >= 26 constructor .Builder(this)
Jeffery Ma
  • 3,051
  • 1
  • 23
  • 26
  • 1
    documentation isn't specific enough at times. – filthy_wizard Mar 06 '18 at 21:02
  • Hello Allen,Hope you are doing great please have a look at my question https://stackoverflow.com/questions/50748028/android-not-getting-notification-in-oreo-8-1-when-app-is-not-in-recent-task-but?noredirect=1#comment88503635_50748028 – Ankita Singh Jun 09 '18 at 14:36
4

Also don't forget to bind your channel_id to your notification builder. After binding it, my problem gone.

notificationBuilder.setChannelId(channelId)

or

NotificationCompat.Builder(Context context, String channelId)
asozcan
  • 1,370
  • 1
  • 17
  • 24