You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 13集成FCM后通知不显示问题排查求助

升级到Android 13(SDK 33)后FCM通知无法接收问题

将App的compileSdk和targetSdk升级至33,已实现POST_NOTIFICATIONS权限申请逻辑,权限弹窗正常弹出且用户已点击允许,但发送FCM推送时App仍无法接收通知。仅修改了权限请求相关代码,未改动FirebaseMessagingService类,此前App的targetSdk版本为30。

相关代码

MyFirebaseMessagingService

class MyFirebaseMessagingService : FirebaseMessagingService() {
var intent: Intent? = null
var badgecount: Int = 0

override fun onMessageReceived(remoteMessage: RemoteMessage) {

    if (remoteMessage.data.isNotEmpty()) {
        try {
            val json = JSONObject(remoteMessage.data.toString())
            handleDataMessage(json)
        } catch (e: Exception) {
            Log.e("FIREBASEEXCEPTION:", e.message.toString())

        }
    }

    if (remoteMessage.notification != null) {
        sendNotification(remoteMessage.notification!!.body)
        println("Message Notification Body:" + remoteMessage.notification!!.body)

    }
    super.onMessageReceived(remoteMessage)
}
private fun handleDataMessage(json: JSONObject) {
    try {
        val data = json.getJSONObject("body")
        val badge = data.getString("badge")
        val message = data.getString("message")
        Log.e("FIREBASE_MSG:", message)
        badgecount = badge.toInt()
        sendNotification(message)
    } catch (e: JSONException) {
        Log.e("JSONEXCEPTION:", e.message.toString())
    } catch (e: java.lang.Exception) {
    }
}
@SuppressLint("ObsoleteSdkInt")
private fun sendNotification(messageBody: String?) {
    ShortcutBadger.applyCount(this, badgecount)
    intent = Intent(this, HomeActivity::class.java)
    intent!!.action = System.currentTimeMillis().toString()
    intent!!.putExtra("Notification_Recieved", 1)
    val notId = NotificationID.getID()
    val stackBuilder = TaskStackBuilder.create(this)
    stackBuilder.addNextIntentWithParentStack(intent!!)
    val pendingIntent = stackBuilder.getPendingIntent(notId, PendingIntent.FLAG_UPDATE_CURRENT)
    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder =
        NotificationCompat.Builder(this)
            .setContentTitle(resources.getString(R.string.app_name))
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
    val notificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel_id = getString(R.string.app_name) + "_01"
        val name: CharSequence = getString(R.string.app_name)
        val importance = NotificationManager.IMPORTANCE_HIGH
        val mChannel = NotificationChannel(channel_id, name, importance)
        notificationBuilder.setChannelId(mChannel.id)
        mChannel.setShowBadge(true)
        mChannel.canShowBadge()
        mChannel.enableLights(true)
        mChannel.lightColor = resources.getColor(R.color.split_bg)
        mChannel.enableVibration(true)
        mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500)
        notificationManager.createNotificationChannel(mChannel)
    }


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        notificationBuilder.setSmallIcon(R.drawable.notify_small)
        notificationBuilder.color = resources.getColor(R.color.split_bg)
    } else {
        notificationBuilder.setSmallIcon(R.drawable.nas_large)
        notificationBuilder.color = resources.getColor(R.color.split_bg)
    }

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

}
}

MyFirebaseInstanceIDService

class MyFirebaseInstanceIDService : FirebaseInstanceIdService() {
    var mContext: Context = this
    override fun onTokenRefresh() {

        //val refreshedToken = FirebaseInstanceId.getInstance().token

        val refreshedToken = FirebaseInstanceId.getInstance().token.toString()

        Log.e("FIREBASETOKEN", refreshedToken)
        sendRegistrationToServer(refreshedToken)
        super.onTokenRefresh()
    }

    private fun sendRegistrationToServer(refreshedToken: String) {
        if (refreshedToken != null) {
            PreferenceManager.setFcmID(mContext, refreshedToken)
        }

    }
}

清单配置

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<service
            android:name=".fcm.MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".fcm.MyFirebaseInstanceIDService"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

内容的提问来源于stack exchange,提问作者Sanju Sabu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 00:35:31