Bad notification for startForeground error only in Huawei devices

1

I have an app that receives notifications trough Firebase Cloud Messaging, I have a Service that is in charge of that and a few days ago I started getting a lot of Firebase (Crashlytics) report of the following error:

Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=NotificationServiceId pri=-1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x72 color=0x00000000 vis=SECRET)

Apparently this is a problem that only happens in Huawei devices with Android 8, 9 and 10. I've read some solutions that said you should create a new channel for Android 8 and above but I already had that in my app. This error appeared from nowhere since I haven't change that Service in a while, here is my code

val pendingIntent = PendingIntent.getActivity(
            this, (System.currentTimeMillis()).toInt(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT)

        val channelId = getString(com.kubo.leal.R.string.notification_channel_id)
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

        val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(com.kubo.leal.R.drawable.logo_leal)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setStyle(NotificationCompat.BigTextStyle().bigText(body))

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_HIGH
            )
            notificationManager.createNotificationChannel(channel)
            notificationBuilder.setChannelId(channelId)
        }

        notificationManager.notify(0, notificationBuilder.build())

I tryed to replicate the error sending a test notification to a Huawei mate 20 pro device with Android 10 and everything worked as expected, the notification arrived fine and the app opened well so, I don't really know if this is a real bug from Firebase or how to replicate it correctly.

I would really appreciate if someone has information about this error! Thanks

android
push-notification
huawei-push-notification
asked on Stack Overflow Apr 27, 2021 by Bryanalmo

1 Answer

0

1.This is a Firebase error. HMS provides interfaces similar to those of the GMS. Here are Push documentation and Github.You can refer to this answer as well.

If you want to use the Google Firebase Cloud Messaging service in your app, and also want your app to run fine on Huawei phones, you may can integrate both GMS FCM(Firebase Cloud Messaging ) and HMS push Kit. I suggest that use G+H solution.

Using the G+H approach, you are able to maintain one codebase and decide whether to use GMS or HMS based on the availability of either one.

First, check whether the GMS and HMS are available.

public boolean  isGMS(){
    return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this) == com.google.android.gms.common.ConnectionResult.SUCCESS;
}
public boolean  isHMS(){
    return HuaweiApiAvailability.getInstance().isHuaweiMobileServicesAvailable(this) == com.huawei.hms.api.ConnectionResult.SUCCESS;
}

The logic of the code invoking:

If ( isGMS() ) {
//GMS code
}else if ( isHMS() ){
//HMS code
}

Or please kindly refer to this: How to check Google Mobile Services enable in device or not?

2.You can also use this IDE plug-in called HMS Core Toolkit to help you analyze where GMS is used in your code. HMS Core Toolkit implements app creation, coding, conversion, debugging, test, and release. The Convertor is a code conversion tool supporting Java and Kotlin projects. This tool can help you quickly convert the existing Android app code for calling third-party APIs into the app code integrated with the HMS Core.

answered on Stack Overflow Apr 28, 2021 by shirley

User contributions licensed under CC BY-SA 3.0