startForeground fail after upgrade my App to API 29

0

After upgrading my API App to version 29, startForeground is failing!

I updated the AndroidManifest.xml adding this:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

But when starting the emulator and clicking the button on my app that starts the service it just closes.

I did a debug and found this problem:

My Notification construct:

    private Notification createNotification() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mService);
        builder.setContentTitle(mService.getString(R.string.app_name));
        builder.setContentText(mCustomContentText);
        builder.setSmallIcon(R.drawable.ic_stat_notify);
        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        builder.setOngoing(true);

        if (mActionsShown) {
            // Add notification triggers
            Intent muteIntent = new Intent(BROADCAST_MUTE);
            Intent deafenIntent = new Intent(BROADCAST_DEAFEN);
            Intent overlayIntent = new Intent(BROADCAST_OVERLAY);

            builder.addAction(R.drawable.ic_action_microphone, mService.getString(R.string.mute), PendingIntent.getBroadcast(mService, 1, muteIntent, PendingIntent.FLAG_CANCEL_CURRENT));
            builder.addAction(R.drawable.ic_action_audio, mService.getString(R.string.deafen), PendingIntent.getBroadcast(mService, 1, deafenIntent, PendingIntent.FLAG_CANCEL_CURRENT));
            builder.addAction(R.drawable.ic_action_channels, mService.getString(R.string.overlay), PendingIntent.getBroadcast(mService, 2, overlayIntent, PendingIntent.FLAG_CANCEL_CURRENT));
        }

        Intent channelListIntent = new Intent(mService, PlumbleActivity.class);
        channelListIntent.putExtra(PlumbleActivity.EXTRA_DRAWER_FRAGMENT, DrawerAdapter.ITEM_SERVER);
        // FLAG_CANCEL_CURRENT ensures that the extra always gets sent.
        PendingIntent pendingIntent = PendingIntent.getActivity(mService, 0, channelListIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(pendingIntent);

        Notification notification = builder.build();
        mService.startForeground(NOTIFICATION_ID, notification);
        return notification;
    }

Error Message:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.morlunk.ptt, PID: 3202
    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 defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
android
service
android-notifications
background-service
asked on Stack Overflow Feb 20, 2020 by Slowaways • edited Feb 20, 2020 by Aditya Arora

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0