Foreground Notification in services not working in android 8.1.0

1

Foreground Notification is working properly in android 8.0 but not working in android 8.1.0

Error:

Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=NOTIFICATION_CHANNEL pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)

services

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PARTIAL_WAKE_LOCK_TAG");


        NotificationHelper helper = new NotificationHelper(this);
        helper.createNotification("Auto Wallpaper","Auto wallpaper is running");
        createNotificationChannel();

        Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL").setSmallIcon
                (R.drawable.ic_launcher_background).setContentTitle("Auto Wallpaper")
                .setContentText("Auto Wallpaper is running").build();

        startForeground(1, notification);
        /////////////////////////////////////////////////////

        return START_NOT_STICKY;
    }
    
    
    
    
     private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Example Service Channel";
            String description = "new description";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

android
notifications
asked on Stack Overflow Apr 3, 2019 by Code11

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0