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

React Native 0.63.3适配SDK31后通知与通话功能失效求助

解决React Native 0.63.3升级SDK31后通知失效及PendingIntent崩溃问题

一、先解决PendingIntent崩溃问题

你添加FLAG_MUTABLE后崩溃,核心原因大概率是项目依赖的AndroidX核心库版本过低,不支持API31新增的这个标记,同时可能存在代码语法错误:

  • 升级androidx.core:core依赖到至少1.7.0版本(该版本开始支持FLAG_MUTABLE/FLAG_IMMUTABLE),在app/build.gradle的dependencies块中添加或修改:
    implementation 'androidx.core:core:1.7.0'
    
  • 修正PendingIntent代码逻辑:如果你的代码是Java语言,要注意用|而不是Kotlin的or来拼接标记,同时确保上下文参数正确,示例:
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        notifyPendingIntent = PendingIntent.getBroadcast(
            getApplicationContext(),
            REQUEST_CODE,
            notifyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE
        );
    } else {
        notifyPendingIntent = PendingIntent.getBroadcast(
            getApplicationContext(),
            REQUEST_CODE,
            notifyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
    }
    

二、修复通知接收失效问题

1. 强制配置通知渠道

API31开始对通知渠道要求更严格,所有通知必须绑定有效渠道:

  • 提前创建通话专用通知渠道,示例代码:
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(
            "CALL_NOTIFICATION_CHANNEL",
            "通话通知",
            NotificationManager.IMPORTANCE_HIGH
        );
        channel.setDescription("呼入/呼出通话状态提醒");
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
    
  • 发送通知时必须指定该渠道ID:
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "CALL_NOTIFICATION_CHANNEL")
        .setSmallIcon(R.drawable.ic_call_notification)
        .setContentTitle("通话提醒")
        .setContentText("有新的呼入电话")
        .setPriority(NotificationCompat.PRIORITY_HIGH);
    

2. 检查权限配置

  • 在AndroidManifest.xml中添加通知权限:
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
    
  • 针对API33+设备,需要在代码中动态申请POST_NOTIFICATIONS权限:
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            ActivityResultContracts.RequestPermission requestPermission = new ActivityResultContracts.RequestPermission();
            registerForActivityResult(requestPermission, isGranted -> {
                // 处理权限申请结果
            }).launch(Manifest.permission.POST_NOTIFICATIONS);
        }
    }
    

3. 确认组件的exported属性

确保处理通知的广播接收器、服务在清单中正确配置android:exported:

  • 如果组件需要接收系统或第三方SDK的调用,设置为true;仅内部调用则设为false,示例:
    <receiver
        android:name=".CallNotificationReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="com.yourapp.ACTION_CALL_NOTIFICATION" />
        </intent-filter>
    </receiver>
    

4. 升级第三方推送/通知库

如果项目使用了react-native-push-notification这类第三方库,升级到支持SDK31的版本(比如react-native-push-notification 8.x系列),无需升级React Native本身,同步调整其依赖的AndroidX库版本即可。

三、验证步骤

  1. 清理Android端缓存:执行./gradlew clean
  2. 重新构建项目:react-native run-android
  3. 分别测试普通通知、通话状态触发通知的接收逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 16:35:23