在 fcm 中创建通知时未创建通知通道

Notification channel not being created while creating notification in fcm

每当我通过 fcm 发送通知时。

它说它将使用清单或 android 应用程序中的默认值,我还在清单中提到我的频道是默认值。 只是说频道不是应用创建的。

日志显示

W/FirebaseMessaging: Notification Channel requested (message_notification) has not been created by the app. Manifest configuration, or default, value will be used.

这是我的 onMessageReceived 函数。

override fun onMessageReceived(p0: RemoteMessage) {
        super.onMessageReceived(p0)
        sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+":/ /"+applicationContext.packageName+"/"+R.raw.alert)
        Log.d("notofiction Nul", sound.toString())

        if(p0.notification!=null){

            val title = p0.data["title"]
            val message = p0.data["body"]
            Log.d("notottg",title+message)
            buildNotificationChannel()
 if(buildNotificationChannel()){
                generateNotification(title!!,message!!)
            }

        }
        else{
            val title = p0.data["title"]
            val message = p0.data["body"]
            Log.d("notottg",title+message)
            buildNotificationChannel()

            if(buildNotificationChannel()){
                generateNotification(title!!,message!!)
            }

        }


    }

这是我的 createNotificationChannel 函数

 private fun buildNotificationChannel():Boolean {


        /*var audioAttributes = AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ALARM)
            .build()
        sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+applicationContext.packageName+"/"+R.raw.alert)
        Log.d("notisound",sound.toString())*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            val CHANNEL_ID =  "merchant_notification"
            val CHANNEL_NAME = "Merchant Notification"
            var notificationChannel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH).apply {
                vibrationPattern=longArrayOf(400, 400, 400, 400, 500, 400, 400, 400, 400)
                enableVibration(true)
            }
            notificationManager= getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)





            //notificationChannel.setSound(sound,audioAttributes)

        }
        return true

    }

这是我的 generateNotification 函数

fun generateNotification(title: String, message: String) {

        val intent = Intent(this@CustomMessagingService, MainActActivity::class.java)
        var pendingIntent = PendingIntent.getActivity(this@CustomMessagingService, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        var builder: NotificationCompat.Builder =
            NotificationCompat.Builder(this, "merchant_notification")
        var audioAttributes = AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ALARM)
            .build()
        sound=Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+"://"+applicationContext.packageName+"/"+R.raw.alert)
        Log.d("notisound",sound.toString())

        builder.setAutoCancel(true)
            .setSmallIcon(R.mipmap.ic_launcher_round)
            .setContentTitle(title)
            .setContentText(message)
            .setContentIntent(pendingIntent)
            .setSound(sound)

        notification = builder.build()
        Log.d("notot", notification.toString() + "   " + sound.toString())
        notificationManager.notify(1, notification)



        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            var nb: Notification.Builder = Notification.Builder(this)
            nb.setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true)
                .setSound(sound, audioAttributes)
                .setContentTitle(title)
                .setContentText(message)
                .setContentIntent(pendingIntent)
                .build()
            notification = nb.notification
            notification.flags = Notification.FLAG_AUTO_CANCEL
            notificationManager.notify(0, notification)
        }


    }

我的清单代码

  <service android:name=".classes.CustomMessagingService"
            android:exported="false"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"
                    />

            </intent-filter>
        </service>
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="merchant_notification" />

generateNotification 方法的示例代码。

private fun generateNotification(title: String, messageBody: String) {
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT)

        val channelId = "125"
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
        val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle(title)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT)
            notificationManager.createNotificationChannel(channel)
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
    }