Android O reporting notification not posted to channel - but it is

35,382

Solution 1

I think I have learned a couple things that all add up to an answer:

  1. I was using an emulator device, with an image that did not include the Play Store.
  2. The version of Google Play Services on the image was not the latest, so I should have been getting a notification telling me I needed to upgrade. Since that notification didn't get applied to a channel, it didn't appear.
  3. If I set logcat in Android Studio to "No Filters" instead of "Show only selected application", then I found the logs that pointed out that the notification in question was the Play Services "update needed" notification.

So, I changed to a image with the Play Store included, and it showed the notification properly (maybe the channel for that notification was to be set by the Play Store?), let me update to the latest Google Play Services, and I haven't seen that warning since.

So, long story short (too late) - with Android O, if you are using Google Play Services & testing on the emulator, choose an image with the Play Store included, or ignore the toast (good luck on that one!).

Solution 2

I had the same problem, and resolved it by using the constructor

new Notification.Builder(Context context, String channelId), instead of the one which is deprecated onAPI levels >=26 (Android O) : new NotificationCompat.Builder(Context context)

The following code won't work if your notificationBuilder is built using the deprecated constructor :

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationBuilder.setChannelId(NOTIFICATION_CHANNEL);}

Solution 3

First create the notification channel:

 public static final String NOTIFICATION_CHANNEL_ID = "4565";
//Notification Channel
        CharSequence channelName = NOTIFICATION_CHANNEL_NAME;
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});


NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);

then use the channel id in the constructor:

final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                .setDefaults(Notification.DEFAULT_ALL)
                .setSmallIcon(R.drawable.ic_timers)
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setSound(null)
                .setChannelId(NOTIFICATION_CHANNEL_ID)
                .setContent(contentView)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setLargeIcon(picture)
                .setTicker(sTimer)
                .setContentIntent(pendingIntent)
                .setAutoCancel(false);

Solution 4

You gotta create a channel before.

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) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            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 = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
}

public void notifyThis(String title, String message) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.green_circle)
                .setContentTitle(title)
                .setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

        // notificationId is a unique int for each notification that you must define
        notificationManager.notify(0, mBuilder.build());
}

Finally you call this method:

createNotificationChannel();
notifyThis("My notification", "Hello World!");

Solution 5

create a notification using following code :

    Notification notification = new Notification.Builder(MainActivity.this)
              .setContentTitle("New Message")
                        .setContentText("You've received new messages.")
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setChannelId(channelId)
                        .build();

not using :

Notification notification = new NotificationCompat.Builder(MainActivity.this)
                    .setContentTitle("Some Message")
                    .setContentText("You've received new messages!")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setChannel(channelId)
                    .build();
Share:
35,382
jkane001
Author by

jkane001

Updated on July 09, 2022

Comments

  • jkane001
    jkane001 almost 2 years

    Couple Android O notification questions:

    1) I have created a Notification Channel (see below), am calling the builder with .setChannelId() (passing in the name of the channel I created, "wakey"; and yet, when I run the app, I get a message that I've failed to post a notification to channel "null". What might be causing this?

    2) I suspect the answer to #1 can be found in the "log" that it says to check, but I've checked logcat & don't see anything about notifications or channels. Where is the log that it says to look in?

    Here's the code I'm using to create the channel:

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence name = context.getString(R.string.app_name);
    String description = "yadda yadda"
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    
    NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, name, importance);
    channel.setDescription(description);
    
    notificationManager.createNotificationChannel(channel);
    

    Here's the code to generate the notification:

    Notification.Builder notificationBuilder;
    
    Intent notificationIntent = new Intent(context, BulbActivity.class);
    notificationIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // Fix for https://code.google.com/p/android/issues/detail?id=53313
    
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    
    Intent serviceIntent = new Intent(context, RemoteViewToggleService.class);
    serviceIntent.putExtra(WakeyService.KEY_REQUEST_SOURCE, WakeyService.REQUEST_SOURCE_NOTIFICATION);
    
    PendingIntent actionPendingIntent = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    _toggleAction = new Notification.Action(R.drawable.ic_power_settings_new_black_24dp, context.getString(R.string.toggle_wakey), actionPendingIntent);
    
    notificationBuilder= new Notification.Builder(context)
        .setContentTitle(context.getString(R.string.app_name))
        .setContentIntent(contentIntent)
        .addAction(_toggleAction);
    
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        notificationBuilder.setChannelId(NOTIFICATION_CHANNEL);
    }
    
    notificationBuilder.setSmallIcon(icon);
    notificationBuilder.setContentText(contentText);
    _toggleAction.title = actionText;
    
    int priority = getNotificationPriority(context);
    notificationBuilder.setPriority(priority);
    notificationBuilder.setOngoing(true);
    
    Notification notification = notificationBuilder.build();
    notificationManager.notify(NOTIFICATION_ID, notification);
    

    And here's the warning I'm getting: enter image description here

  • Chris Li
    Chris Li almost 7 years
    Try below code to set channel: new Notification.Builder(getApplicationContext(),FOLLOWERS_CHANN‌​EL) .setContentTitle(title) .setContentText(body) .setSmallIcon(getSmallIcon());
  • jkane001
    jkane001 about 6 years
    The original question states that I created a notification channel. That said, I resolved the issue and explained that all in the accepted answer.
  • jkane001
    jkane001 about 6 years
    The original question states that I created a notification channel. That said, I resolved the issue and explained that all in the accepted answer.
  • jkane001
    jkane001 almost 6 years
    This simply suggests avoiding the problem by not going to the latest SDK. That's not an acceptable solution.
  • IgorGanapolsky
    IgorGanapolsky over 5 years
    Why is setChannelId(NOTIFICATION_CHANNEL_ID) needed?
  • Cililing
    Cililing over 5 years
    I had about 1k crashes because of this deprectated constructor... Thanks!