No notification channel found when already created

0

I am getting No notification channel found in the error in Oreo when I already have created it and its working when app is in foreground, this is my code, when I log NC in foreground it is not null and gives me data but when the app is in background I get error-

E/NotificationService: No Channel found for pkg=..*, channelId=null, id=0, tag=GCM-Notification:141257693, opPkg=.., callingUid=10530, userId=0, incomingUserId=0, notificationUid=10530, notification=Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)

NotificationCompat.Builder notificationBuilder ;
    NotificationManager notificationManager;

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

        NotificationChannel nc = notificationManager.getNotificationChannel(getString(R.string.default_notification_channel_id));

        Log.e(TAG, "sendNotification: "+nc );

        notificationManager = getSystemService(NotificationManager.class);

        notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), getString(R.string.default_notification_channel_id));

        notificationBuilder.setChannelId(getString(R.string.default_notification_channel_id));
        if (nc == null) {
            nc = new NotificationChannel(getString(R.string.default_notification_channel_id), getString(R.string.default_notification_channel_name), NotificationManager.IMPORTANCE_HIGH);

            nc.setDescription("Badge Notifications");
            nc.enableLights(true);
            nc.setShowBadge(true);
            nc.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

            notificationManager.createNotificationChannel(nc);

        }
    } else {
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationBuilder = new NotificationCompat.Builder(getApplicationContext());
    }

    notificationBuilder.setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setColor(Utils.getPrimaryColor())
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    notificationManager.notify(0, notificationBuilder.build());
android
firebase
push-notification
android-notifications
android-8.0-oreo
asked on Stack Overflow Sep 27, 2019 by swati kapoor

1 Answer

0

I solved the problem, the fcm dependencies I had was of 10.2.1. It was said to include notification channel you must need it.( I update this to recent 20.0.0 but as the project was old, many of the functions were not found that were implemented in the project. So I then finally upgraded that yo 11.4.0 and it worked. This was the dependency that has all methods of the code plus latest functions to make the channel work at the same time.

answered on Stack Overflow Sep 28, 2019 by swati kapoor

User contributions licensed under CC BY-SA 3.0