Kumar VL
Kumar VL

Reputation: 45

Android: Oreo Notification Channel Not Working in API level 22

I am doing push notification in android. The below code block is not working in API level 22.

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new 
        NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }

How to do this in API level 22

Upvotes: 0

Views: 264

Answers (2)

user13146129
user13146129

Reputation:

You can try following source code

public void showNotification()
{
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"channelID")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("Notification")
            .setContentText("Hello! This is a notification.")
            .setAutoCancel(true);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int notificationId = 1;
    createChannel(notificationManager);
    notificationManager.notify(notificationId, notificationBuilder.build());
}

public void createChannel(NotificationManager notificationManager){
    if (Build.VERSION.SDK_INT < 26) {
        return;
    }
    NotificationChannel channel = new NotificationChannel("channelID","name", NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("Hello! This is a notification.");
    notificationManager.createNotificationChannel(channel);
}

This code works in every android version. Here's the git repo

Upvotes: 0

cd1
cd1

Reputation: 16534

There is no NotificationChannel in API 22. That feature is only available in API >=26.

Upvotes: 0

Related Questions