Background service crashes application on API 28 but works on API 23

2

I have this app that starts service in the background to monitor the proximity sensor. It all works great at targetSdkVersion 23 but not on 28. Service:

public class ForegroundService (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
            Log.i(LOG_TAG, "Received Start Foreground Intent ");
            Intent notificationIntent = new Intent(this, MainActivity.class);
            notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);

            Intent previousIntent = new Intent(this, ForegroundService.class);
            previousIntent.setAction(Constants.ACTION.PREV_ACTION);
            PendingIntent ppreviousIntent = PendingIntent.getService(this, 0,
                    previousIntent, 0);

            Intent playIntent = new Intent(this, ForegroundService.class);
            playIntent.setAction(Constants.ACTION.PLAY_ACTION);
            PendingIntent pplayIntent = PendingIntent.getService(this, 0,
                    playIntent, 0);


            Intent 

when the app crashes i also get the logcat:

020-01-03 03:24:00.466
26636-26636/com.personal.ramakrishnanak.autoscreenoff
E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.personal.ramakrishnanak.autoscreenoff, PID: 26636
        android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for
service notification: Notification(channel=null pri=0 contentView=null
vibrate=null sound=null tick defaults=0x0 flags=0x42 color=0x00000000
actions=1 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2067)
            at android.os.Handler.dispatchMessage(Handler.java:107)
            at android.os.Looper.loop(Looper.java:237)
            at android.app.ActivityThread.main(ActivityThread.java:7762)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
java
android
android-9.0-pie
asked on Stack Overflow Jan 3, 2020 by Bruno Alex • edited Jan 3, 2020 by Bruno Alex

3 Answers

4

From API 28, if you want to set Foreground service, you have to add FOREGROUND_SERVICE permission.

This is the official document:

Apps that target Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission. This is a normal permission, so the system automatically grants it to the requesting app. If an app that targets Android 9 or higher attempts to create a foreground service without requesting FOREGROUND_SERVICE, the system throws a SecurityException.

the other problem may be the notification. from API 26+, you have to add the notification channel.

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) {
        String CHANNEL_ID = "some_channel_id";
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        // add the NotificationChannel
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

answered on Stack Overflow Jan 3, 2020 by Lenoarod • edited Jan 3, 2020 by Edric
2

When you target Android 8.0 (API level 26), you must implement one or more notification channels.

You must have code like this to create a channel before you use

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Create the NotificationChannel
    String name = "channel_id";
    String descriptionText = "channel_description";
    int importance = NotificationManager.IMPORTANCE_DEFAULT;
    NotificationChannel channel = new NotificationChannel("channel_id", name, importance);
    channel.setDescription(descriptionText);
    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.createNotificationChannel(channel);
}

Normally you put the code in your customized Application class.

Then you notification builder should goes like

Notification notification = new Notification.Builder(this,"channel_id")
answered on Stack Overflow Jan 3, 2020 by fangzhzh • edited Jan 3, 2020 by fangzhzh
1

It seems you are not using a NotificationChannel for your Notification. This is required from Android 8 up.

See the documentation herr: https://developer.android.com/training/notify-user/channels

answered on Stack Overflow Jan 3, 2020 by Ridcully

User contributions licensed under CC BY-SA 3.0