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

调用react-native-push-notification遇权限错误:需SCHEDLE.EXACT-ALARM权限

解决react-native-push-notification精确闹钟权限错误

1. 在AndroidManifest.xml中声明权限

打开android/app/src/main/AndroidManifest.xml,在<manifest>标签内添加权限声明:

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

2. 动态申请Android 12+权限

从Android 12(API 31)开始,SCHEDULE_EXACT_ALARM属于需用户主动授权的权限,仅清单声明无法生效。调用推送通知前需先检查并申请权限:

用PermissionsAndroid实现动态申请

import { PermissionsAndroid, Platform } from 'react-native';
import PushNotification from 'react-native-push-notification';

const requestExactAlarmPermission = async () => {
  if (Platform.OS === 'android' && Platform.Version >= 31) {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.SCHEDULE_EXACT_ALARM,
        {
          title: '需要精确闹钟权限',
          message: '应用需要该权限才能正常推送定时通知',
          buttonNeutral: '稍后处理',
          buttonNegative: '取消',
          buttonPositive: '允许',
        },
      );
      return granted === PermissionsAndroid.RESULTS.GRANTED;
    } catch (err) {
      console.warn(err);
      return false;
    }
  }
  return true;
};

// 初始化推送前先校验权限
const initPushNotification = async () => {
  const hasPermission = await requestExactAlarmPermission();
  if (hasPermission) {
    PushNotification.configure({
      // 你的推送配置项
    });
  } else {
    console.log('用户拒绝精确闹钟权限,定时通知功能将受限');
  }
};

3. 处理权限被拒绝的场景

若用户拒绝权限,可引导其前往应用设置手动开启:

import { Linking } from 'react-native';

const openAppSettings = () => {
  Linking.openSettings();
};

额外说明

  • 仅Android 12及以上版本需要处理该权限,低版本无需额外操作
  • 若业务无需精确定时,可查看react-native-push-notification文档,确认是否有配置项切换为非精确闹钟模式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 19:31:02