0

What could be the possible other causes of a

Fatal Exception: android.app.RemoteServiceException
Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification

besides not having a channel set? It seems to happen only on Android 8 and 9

My stacktrace shows that channel has a value:

invalid channel for service notification: Notification(channel=com.myapp.notifications pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x52 color=0x00000000 category=service number=0 vis=PRIVATE semFlags=0x0 semPriority=0 semMissedCount=0)

so it seems that the channel has been created correctly.

My background service is set with the usual

    public static final String NOTIFICATION_CHANNEL_ID = "com.myapp.notifications";
    public static final String SERVICE_CHANNEL_ID = "com.myapp.services";
    public static final int NOTIFICATION_ID = 100;

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForeground(2, buildNotification(getApplicationContext()));
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    Notification buildNotification(Context context) {
        String channelId = SERVICE_CHANNEL_ID;
        setupNotificationChannel(context, channelId);

        return NotificationCompat.Builder(context, channelId)
                .setOngoing(true)
                .setSmallIcon(R.drawable.app_icon)
                .setCategory(Notification.CATEGORY_SERVICE)
                .setAutoCancel(true)
                .setChannelId(channelId)
                .build();
    }

    @RequiresApi(Build.VERSION_CODES.O)
    void setupNotificationChannel(Context context, String channelId) {
        NotificationManager notificationManager = getNotificationManager(context);

        if (notificationManager.getNotificationChannel(channelId) == null) {
            NotificationChannel channel = NotificationChannel(channelId, getChannelName(channelId), getChannelImportance())
            channel.setDescription(getChannelDescription(channelId))
            notificationManager.createNotificationChannel(channel)
        }
    }

I also display some push notifications in a similar way:

    public void showNotification(Context context) {
        NotificationManager notificationManager = getNotificationManager(context);

        String channelId = SHRNotificationService.NOTIFICATION_CHANNEL_ID;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            setupNotificationChannel(context, channelId);
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
                .setSmallIcon(android.R.drawable.stat_sys_download)
                .setContentTitle(getNotificationTitle())
                .setColor(ContextCompat.getColor(context, R.color.BLUE))
                .setChannelId(channelId)
                .setPriority(getNotificationPriority(channelId))
                .setAutoCancel(false)
                .setOngoing(true)
                .setOnlyAlertOnce(true);

        notificationManager.notify(NOTIFICATION_ID, builder.build());
    }

I have the

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

in the Manifest.

What is also unclear is that the stacktrace refers to a notification from the service category, with com.myapp.notifications as the channel Id, but none of the background services or notifications meet both these conditions.

Ricardo A.
  • 685
  • 2
  • 8
  • 35
J. Doe
  • 85
  • 12
  • seams there is a workaround here: https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1 – David Jun 04 '19 at 14:30
  • I don't think you need to use getApplicationContext() if you're a in a Service, then *you are a Context* too. – Martin Marconcini Jun 04 '19 at 14:42
  • @David what workaround are you referring to ? The answers in the other question all just mention to create a channel – J. Doe Jun 05 '19 at 22:35

1 Answers1

0

It's been a while, but I think this is how it you want to make it look:

     fun sendToForegroundWithNotification() {
        val CHANNEL_ID = "YourChannelId"

        @Suppress("DEPRECATION") //for the NotificationBuilder < API26 ctor
        val notificationBuilder: Notification.Builder
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // O > need a channel, create one
            notificationManager.createNotificationChannel(
                    NotificationChannel(
                            CHANNEL_ID,
                            "Title",
                            NotificationManager.IMPORTANCE_DEFAULT
                    )
            )
            notificationBuilder = Notification.Builder(this, CHANNEL_ID)
        } else notificationBuilder = Notification.Builder(this)


        val notification = notificationBuilder
                .setContentTitle(getText(R.string.app_name))
                .setContentText(getText(R.string.your_content))
                .setSmallIcon(R.drawable.some_icon)
                .etc(whatever you need)
                .build()


        // since you're in your Service, you can call startFg directly:
        startForeground(666, notification) // ;-)

     }

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144
  • I don't see what is different between this and my code, besides you building the Notification without a channel ID for < O – J. Doe Jun 05 '19 at 22:34
  • I'm not sure what else to tell you; if there's another issue, it may be somewhere else, could you show more code or a sample project reproducing the issue? I created a sample project with a service and startForeground like that works (on 8.1.0 and 9.0 and API 23 on an old device (where channel is skipped). Anything else you can think of that is _different_ in your project? – Martin Marconcini Jun 06 '19 at 12:08
  • I have not been able to reproduce the issue internally, it works most of the time but there are some occurrences of this crash on my Crashlytics reports. It seems to happen more or less once per user only – J. Doe Jun 07 '19 at 11:12
  • I see this from the Pre-launch reports yet don't see it in person. Interesting thing. So far haven't found a reason why either. – CmosBattery Oct 15 '19 at 17:30
  • did you find a solution for this issue? – Roman Pozdnyakov Sep 08 '20 at 11:10