Start Foreground Service Issue In Android Pie

-2

In Android Pie I had StartForeground Service running when the service starts running the issue I had got is as below

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=120 pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
android
service
asked on Stack Overflow Jan 20, 2019 by Dina Gar

1 Answer

0

You have to initialize notification channels before you can create a notification since Android O.
You can read the official documentation here
Here is an example of creating a channel inside you Application object:

public class YourApp extends Application {

@Override
public void onCreate() {
    super.onCreate();

    createNotificationChannel();
}


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 = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(Constants.NOTIFICATIONS_CHANNEL, 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);
    }
}
}
answered on Stack Overflow Jan 20, 2019 by Mark Kazakov

User contributions licensed under CC BY-SA 3.0