androidandroid-notificationsandroid-8.1-oreonotification-channel

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:


Solution

  • 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.