noob-Sci-Bot
noob-Sci-Bot

Reputation: 231

NotificationCompat.Builder() not accepting Channel Id as argument

I know this question has been asked several times before. But none of the solutions worked for me. That's why I would like to ask the question again. The following line only accepts NotificationCompat.Builder(context) :

 NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, ADMIN_CHANNEL_ID)  // Getting error

I have fulfilled:

Upvotes: 2

Views: 5890

Answers (4)

swati kapoor
swati kapoor

Reputation: 131

Try updating your gradle dependencies. I was also not getting the the setChannelId method or the arguments. But as it came in the updated version of the support library. You will find it. Try using

implementation 'com.android.support:support-v4:28.0.0'

Upvotes: 0

Solution:

On Android 8.0 (Oreo) you must have something called as a NotificationChannel So try the below implementation:

Step1: Create Notification Channel

private void createNotificationChannel() {

    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);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

Finally: Then your Notification:

 NotificationCompat.Builder mBuilder =   new NotificationCompat.Builder(activity)
                .setSmallIcon(R.drawable.ic_launcher_background) // notification icon
                .setContentTitle("Notification!") // title for notification
                .setContentText("Hello word") // message for notification
                .setAutoCancel(true); // clear notification after click
Intent intent = new Intent(activity, RecorderActivity.class);
PendingIntent pi = PendingIntent.getActivity(activity,0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mBuilder.setContentIntent(pi);
notificationManager.notify(1, mBuilder.build());

Please compare this with Yours and see if it works

Try this, Hope it helps.

Upvotes: 2

Ramesh Yankati
Ramesh Yankati

Reputation: 1217

First create notification channel and notification passing channel ID.

  NotificationManager notificationManager = (NotificationManager)context
                     .getSystemService(Context.NOTIFICATION_SERVICE);
  buildNotificationChannel(manager,String <Channel_ID>,String description);

Create Notification channel.

 public void buildNotificationChannel(NotificationManager manager, String channelID, 
       String description) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             if (manager.getNotificationChannel(channelID) == null) {
               NotificationChannel channel = new NotificationChannel(channelID, 
               description,NotificationManager.IMPORTANCE_LOW);
               channel.setDescription(description);
               manager.createNotificationChannel(channel);
           }
      }
  }

Create Notification passing argument channel ID

     Notification notification = new 
     NotificationCompat.Builder(context,<Channel_ID>)
        .setSmallIcon(R.drawable.app_icon)
        .setContentTitle("Title goes here.")
        .setPriority(Notification.PRIORITY_HIGH)
        .setContentIntent(pendingIntent)
        .setAutoCancel(false)
        .setOngoing(true)
        .setVisibility(Notification.VISIBILITY_PUBLIC)
        .setContentText("Content Text").build();
        notificationManager.notify(ID,notification);

Upvotes: -1

Sifat Ullah Chowdhury
Sifat Ullah Chowdhury

Reputation: 81

As far as I know you can add notification channel only for versions greater than or equal 26, the versions below android 8.0 doesn't support notification channel. My suggestion would be using following codes:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
           { 
             NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, ADMIN_CHANNEL_ID) 
           }

    else NotificationCompat.Builder(context)

Upvotes: 0

Related Questions