React Native中如何检测通知接收?需实现外部通知触发视频暂停
React Native 检测通知接收并暂停视频实现方案
一、核心差异说明
Android支持系统级通知监听,可以捕获所有应用(包括Messenger等第三方应用)的通知;而iOS受沙盒机制限制,仅能监听当前应用自身的通知,无法获取其他应用的通知事件。以下分平台给出实现方案:
二、Android 实现步骤
1. 创建通知监听服务
在Android项目的android/app/src/main/java/[你的应用包名]/路径下,新建NotificationListener.java文件,用于监听系统通知:
package com.yourapppackage; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.modules.core.DeviceEventManagerModule; public class NotificationListener extends NotificationListenerService { private static final String TAG = "NotificationListener"; @Override public void onNotificationPosted(StatusBarNotification sbn) { super.onNotificationPosted(sbn); // 过滤掉自身应用的通知,避免重复触发 String appPackage = getPackageName(); if (!sbn.getPackageName().equals(appPackage)) { // 向React Native发送通知事件 sendNotificationEvent(sbn.getPackageName()); } } private void sendNotificationEvent(String packageName) { ReactApplicationContext reactContext = new ReactApplicationContext(this); if (reactContext.hasActiveCatalystInstance()) { reactContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit("externalNotificationReceived", packageName); } } }
2. 配置AndroidManifest.xml
在android/app/src/main/AndroidManifest.xml的<application>标签内,注册上述服务:
<service android:name=".NotificationListener" android:label="应用通知监听" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service>
3. 引导用户开启权限
通知监听需要用户手动授权,在React Native中添加跳转权限设置页的逻辑:
import { Platform, NativeModules } from 'react-native'; // 跳转至通知监听权限设置页 const openNotificationPermissionSettings = () => { if (Platform.OS === 'android') { NativeModules.NotificationSettings.openListenerSettings(); } };
对应的Android原生模块(需在MainApplication.java中注册):
package com.yourapppackage; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import android.content.Intent; public class NotificationSettingsModule extends ReactContextBaseJavaModule { public NotificationSettingsModule(ReactApplicationContext reactContext) { super(reactContext); } @Override public String getName() { return "NotificationSettings"; } @ReactMethod public void openListenerSettings() { Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); getCurrentActivity().startActivity(intent); } }
4. 监听通知事件并暂停视频
在React Native业务代码中,监听原生发送的事件,触发暂停逻辑:
import { DeviceEventEmitter, useEffect } from 'react-native'; // 假设你有一个控制视频播放的函数 const pauseVideo = () => { // 执行视频暂停操作 }; useEffect(() => { const notificationSubscription = DeviceEventEmitter.addListener( 'externalNotificationReceived', (packageName) => { console.log('收到第三方应用通知:', packageName); pauseVideo(); } ); // 组件卸载时移除监听 return () => notificationSubscription.remove(); }, []);
三、iOS 实现方案(仅支持自身应用通知)
iOS无法监听第三方应用通知,若仅需处理自身应用的通知,可通过Firebase Messaging或原生推送实现:
1. 请求推送权限
import messaging from '@react-native-firebase/messaging'; const requestPushPermission = async () => { const authStatus = await messaging().requestPermission(); const hasPermission = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL; if (hasPermission) { console.log('推送权限已开启'); } };
2. 监听通知接收并暂停视频
import { useEffect } from 'react-native'; import messaging from '@react-native-firebase/messaging'; const pauseVideo = () => { // 执行视频暂停操作 }; useEffect(() => { const unsubscribe = messaging().onMessage(async remoteMessage => { console.log('收到自身应用通知:', remoteMessage); pauseVideo(); }); return unsubscribe; }, []);
四、关键注意事项
- Android端:必须引导用户手动开启通知监听权限,否则无法捕获第三方应用通知
- Android端:务必过滤自身应用的通知,避免触发无效的暂停逻辑
- iOS端:受系统限制,无法实现第三方应用通知监听,仅能处理自身应用推送
内容的提问来源于stack exchange,提问作者AlmaG3st
相关产品推荐
相关产品推荐

