1

我有一个通过 Firebase Cloud Messaging 接收通知的应用程序,我有一个负责该通知的服务,几天前我开始收到很多关于以下错误的 Firebase (Crashlytics) 报告:

startForeground 的错误通知:java.lang.RuntimeException:服务通知的无效通道:Notification(channel=NotificationServiceId pri=-1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x72 color=0x00000000 vis=SECRET)

显然,这个问题只发生在搭载 Android 8、9 和 10 的华为设备中。我读过一些解决方案,说您应该为 Android 8 及更高版本创建一个新频道,但我的应用程序中已经有了。这个错误不知从何而来,因为我有一段时间没有更改该服务,这是我的代码

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())

我尝试复制错误,向配备 Android 10 的华为 mate 20 pro 设备发送测试通知,一切正常,通知到达正常,应用程序打开良好,我真的不知道这是否是一个真正的错误Firebase 或如何正确复制它。

如果有人知道有关此错误的信息,我将不胜感激!谢谢

4

1 回答 1

0

1.这是一个 Firebase 错误。HMS 提供与 GMS 类似的接口。这里是Push 文档Github。你也可以参考这个答案

如果您想在您的应用中使用 Google Firebase Cloud Messaging 服务,并且希望您的应用在华为手机上运行良好,您可以同时集成 GMS FCM(Firebase Cloud Messaging)和 HMS 推送套件。我建议使用G+H 解决方案

使用 G+H 方法,您可以维护一个代码库,并根据任一代码库的可用性决定是使用 GMS 还是 HMS。

首先,检查GMS和HMS是否可用。

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;
}

代码调用逻辑:

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

或者请参考:如何在设备中检查谷歌移动服务是否启用?

2.您也可以使用这个名为HMS Core Toolkit的IDE插件来帮助您分析GMS在您的代码中的使用位置。HMS Core Toolkit 实现了应用的创建、编码、转换、调试、测试和发布。Convertor 是一个支持 Java 和 Kotlin 项目的代码转换工具。该工具可以帮助您将现有的调用第三方API的Android应用代码快速转换为与HMS Core集成的应用代码。

于 2021-04-28T02:14:10.027 回答