如何使用Expo在Android平台推送紧急/关键通知
Android 关键通知(免打扰可打断)配置方案(Expo SDK 49)
一、申请必要权限
Android 实现免打扰下可接收的关键通知,需申请两个核心权限:基础通知权限和通知政策权限(用于修改免打扰规则)。
import { PermissionsAndroid } from 'react-native'; import * as Notifications from 'expo-notifications'; async function requestCriticalNotificationPermissions() { // 申请基础通知权限 const notificationPermission = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS ); // 申请通知政策权限(允许控制免打扰下的通知展示) const policyPermission = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_NOTIFICATION_POLICY ); return notificationPermission === 'granted' && policyPermission === 'granted'; }
二、创建高优先级通知渠道
Android 的通知行为由渠道控制,需创建一个最高优先级、允许打断免打扰的渠道:
async function setupCriticalNotificationChannel() { await Notifications.setNotificationChannelAsync('critical_alerts', { name: '关键通知渠道', description: '用于发送可打断免打扰的紧急通知', importance: Notifications.AndroidImportance.MAX, // 最高优先级 vibrationPattern: [0, 250, 250, 250], lightColor: '#FF2D55', interruptionFilter: Notifications.AndroidInterruptionFilter.ALL, // 允许打断免打扰 }); }
三、发送关键通知
发送通知时指定上述创建的渠道,确保通知走高优先级通道:
async function sendCriticalAlert() { await Notifications.scheduleNotificationAsync({ content: { title: '紧急提醒', body: '这是一条可在免打扰模式下弹出的关键通知', sound: 'default', priority: Notifications.AndroidPriority.MAX, }, trigger: null, // 立即发送 channelId: 'critical_alerts', // 绑定关键通知渠道 }); }
补充说明
- 华为设备的「允许打断」权限,对应上述渠道配置中的
interruptionFilter: ALL,配合最高优先级可实现免打扰下的通知展示。 ACCESS_NOTIFICATION_POLICY属于危险权限,用户需手动在系统设置中授权应用的「修改免打扰权限」。- Expo 托管环境下无需 eject,SDK 49 已支持上述所有配置。
内容的提问来源于stack exchange,提问作者Nermin
相关产品推荐
相关产品推荐

