Solutions to Fail to post notification on channel "null"

Keywords: SDK Android Mobile less

There are a few solutions to this problem on the beginner's website. Here are some of the code that you can really use after you understand and test it.

To add a click event to the notification before the sdk version is 25 or 25, simply pass in a Pending Intent through setContentIntent() and the notification click event code is as follows

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
PendingIntentpendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this)
                                .setContentTitle("This is content title")
                                .setContentText("This is content text")
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .build();
manager.notify(1,notification);

But for many novice simulators or debugging tools like mine, they are the latest version of the sdk 26 platform.
So if you use the above code, you'll jump out of this error.

The answer was finally found in an update to Android O.
Portal: https://www.ithome.com/html/android/298943.htm

Look back at the error prompt again

Failed to post notification on channel "null"

Now we know what the problem is.

This means that after Android O, a class called Notification Channel was introduced. When the sdk version is 26, we can set up click events without adding this thing.

As far as I understand, Notification Channel's function is to refine the notification settings before the notification settings can be completed in Notification.Builder(Context,int) after the introduction of Notification Channel, the setting of the priority of the vibration sound prompt lamp can be set in Notification Channel.

However, after personal testing, Android O feels more user-oriented in notification. Even if you set the importance in the code, the effect of the actual prompt is that the user judges the importance of the notification set by the user on the mobile phone, so the importance of the code settings can be directly omitted from the personal feeling of the developer.

After joining Notification Channel
The code is as follows

String id ="channel_1";//id of channel
String description = "123";//Description information of channel
int importance = NotificationManager.IMPORTANCE_LOW;//The Importance of channel
NotificationChannel channel = new NotificationChannel(id, "123", importance);//Generating channel
//Adding attributes to channel
//channel.enableVibration(true); vibration
//channel.enableLights(true); prompt light
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);//Add channel
Notification notification = new Notification.Builder(MainActivity.this,id)
                                    //Note that there is an additional parameter id, which refers to the ID of the configured Notification Channel.
                                    //You can try it on your own and then configure the generation above this line of code after you run it once.
                                    //Code comment can also run successfully if the parameter id is changed directly to "channel_1"
                                    //But we can't change it to something else like "channel_2".
                                    .setCategory(Notification.CATEGORY_MESSAGE)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .setContentTitle("This is a content title")
                                    .setContentText("This is a content text")
                                    .setContentIntent(pendingIntent)
                                    .setAutoCancel(true)
                                    .build();
manager.notify(1,notification);

But it's not good for the project because we need to consider a compatible version, so we need to add a version judgment or a requireApi for Android O.
However, my personal suggestion is to add a version judgment because you can add another piece of code to be compatible with the platform before 25.

Here is the final code

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
 if(Build.VERSION.SDK_INT >= 26) 
 {
               //When sdk version is larger than26
   String id = "channel_1";
   String description = "143";
   int importance = NotificationManager.IMPORTANCE_LOW;
   NotificationChannel channel = new NotificationChannel(id, description, importance);
//                     channel.enableLights(true);
//                     channel.enableVibration(true);//
   manager.createNotificationChannel(channel);
   Notification notification = new Notification.Builder(MainActivity.this, id)
                                    .setCategory(Notification.CATEGORY_MESSAGE)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .setContentTitle("This is a content title")
                                    .setContentText("This is a content text")
                                    .setContentIntent(pendingIntent)
                                    .setAutoCancel(true)
                                    .build();
   manager.notify(1, notification);
   }
   else
   {
            //When sdk version is less than26
    Notification notification = new NotificationCompat.Builder(MainActivity.this)
                                    .setContentTitle("This is content title")
                                    .setContentText("This is content text")
                                    .setContentIntent(pendingIntent)
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .build();
    manager.notify(1,notification);
   }

Posted by krelian on Thu, 13 Dec 2018 13:48:09 -0800