Android 12升级SDK至31后推送通知点击无响应问题求助
解决Android 12+(SDK31)通知点击无法打开Activity的问题
针对你升级SDK到31后通知点击无响应的问题,可按以下步骤排查修复:
1. 确保目标Activity的Manifest配置正确
Android 12+要求可从外部启动的Activity必须显式设置android:exported="true",同时确认Intent Filter配置正确:
<activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
2. 检查Notification Channel是否已创建
SDK31+必须提前创建通知渠道,否则通知可能无法正常响应点击。在Application类或启动页添加渠道创建代码:
private fun createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val name = getString(R.string.channel_name) val descriptionText = getString(R.string.channel_description) val importance = NotificationManager.IMPORTANCE_HIGH val channel = NotificationChannel("CHANNEL_ID", name, importance).apply { description = descriptionText } val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) } }
3. 优化PendingIntent的配置
- 避免固定requestCode导致的PendingIntent缓存问题,改用动态值(如时间戳)
- 确认Flag组合正确,若无需修改Intent内容,
FLAG_IMMUTABLE是安全的;若需后续更新Intent,改用FLAG_MUTABLE
val notifyIntent = Intent(this, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK } // 用动态requestCode避免缓存 val requestCode = System.currentTimeMillis().toInt() val notifyPendingIntent = PendingIntent.getActivity( this, requestCode, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE )
4. 区分FCM前台/后台消息处理
如果是后台接收FCM消息,系统会直接展示通知,不会触发onMessageReceived,此时需在推送 payload 中添加click_action字段,并在Manifest中为MainActivity配置对应Intent Filter:
推送Payload示例:
{ "notification": { "title": "标题", "body": "内容", "click_action": "OPEN_MAIN_ACTIVITY" }, "to": "设备Token" }
Manifest配置:
<activity android:name=".MainActivity" android:exported="true"> <!-- 原有主入口Filter --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- 添加对应click_action的Filter --> <intent-filter> <action android:name="OPEN_MAIN_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
5. 修正代码中的语法错误
你提供的代码混合了Java和Kotlin语法,修正后的完整PushMessagingService代码如下:
class PushMessagingService : FirebaseMessagingService() { override fun onMessageReceived(remoteMessage: RemoteMessage) { super.onMessageReceived(remoteMessage) val notifyIntent = Intent(this, MainActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK } val requestCode = System.currentTimeMillis().toInt() val notifyPendingIntent = PendingIntent.getActivity( this, requestCode, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) val builder = NotificationCompat.Builder(this, "CHANNEL_ID").apply { setSmallIcon(R.drawable.logo) setContentTitle(getString(R.string.app_name)) setContentText(getString(R.string.logging_in)) priority = NotificationCompat.PRIORITY_HIGH setVisibility(NotificationCompat.VISIBILITY_PRIVATE) setContentIntent(notifyPendingIntent) setAutoCancel(true) } val notificationId = AtomicInteger().incrementAndGet() with(NotificationManagerCompat.from(this)) { notify(notificationId, builder.build()) } } }
内容的提问来源于stack exchange,提问作者Swapnil Akubattin
相关产品推荐
相关产品推荐

