0

I have typed the following function to get notifications in android.

But I get an error which says Failed to post notification on channel "null". I know that I'm missing some piece of code which seems to be inserted for devices running on some newer versions.

Can I get the exact code to be inserted?

void create_notification(String string){
        Intent intent = new Intent(this, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(intent);
    PendingIntent pendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
            );
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setAutoCancel(true)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(pendingIntent)
            .setContentText(string)
            .build();
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, notification);
}
Shudy
  • 7,806
  • 19
  • 63
  • 98
  • have you check whether you are using deprecated syntax or not and what is your android SDK level of your test device – Viroj Fernando Jul 14 '20 at 11:36
  • Please refer to this [stackoverflow question](https://stackoverflow.com/questions/45711925/failed-to-post-notification-on-channel-null-target-api-is-26). I think you need to set channel id using `setChannelId(id)` or using NotificationChannel `new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);` – AhmerMH Jul 14 '20 at 11:40
  • Hi @ShivaGanesh, when you have some crash, consider to add the logcat, adding it to your question will make easy find the error. In this case is "quite obvious", but for next ones add it ;) – Shudy Jul 14 '20 at 11:45

2 Answers2

0

The documentation (first result on google for the search terms "android notification channel") gives the following example:

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);
    }
}

And then on your notification you need to specify the channel ID:

Notification notification = new Notification.Builder(this, CHANNEL_ID)
   ...

Have you tried this?

aax
  • 394
  • 5
  • 10
  • What does R.string.channel_name and R.string.channel_description imply here? – Shiva Ganesh Jul 14 '20 at 11:44
  • R.string.XXXXXX are the text you want to add. Usually you put all your text in strings files. This way you can have multiple languages --> https://developer.android.com/training/basics/supporting-devices/languages – Shudy Jul 14 '20 at 11:49
  • See https://stackoverflow.com/questions/51685489/purpose-of-notificationchannel-name-and-description – aax Jul 14 '20 at 11:49
0

In your builder, you can add the channelID, for example:

Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(getString(R.string.app_name))
            .setAutoCancel(true)
            .setChannelId("base_channel") //<-- set the name you feel is proper to your project
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setContentIntent(pendingIntent)
            .setContentText(string)
            .build();
Shudy
  • 7,806
  • 19
  • 63
  • 98
  • @ShivaGanesh if this answer helped you, feel free to "upvote" and "accept" the answer. This will help next SO users, to find answers faster. – Shudy Jul 14 '20 at 12:02