0

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

shashank chandak
  • 544
  • 5
  • 13
Kumar VL
  • 45
  • 4
  • 1
    *`How to do this in API level 22`* **NOT POSSIBLE** Read about **[NotificationChannel](https://developer.android.com/reference/android/app/NotificationChannel)** and `NotificationChannel` added in **`API level 26`** **[Create and Manage Notification Channels](https://developer.android.com/training/notify-user/channels)** – AskNilesh Sep 05 '18 at 12:42

2 Answers2

0

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

cd1
  • 15,908
  • 12
  • 46
  • 47
0

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