1- import * as Notifications from 'expo-notifications ' ;
1+ import { isRunningInExpoGo } from 'expo' ;
22import { router } from 'expo-router' ;
33import { useEffect } from 'react' ;
44import { NativeModules , Platform } from 'react-native' ;
@@ -17,6 +17,20 @@ type JPushNotificationResult = {
1717 title ?: string ;
1818} ;
1919
20+ type NotificationsModule = typeof import ( 'expo-notifications' ) ;
21+ type NotificationPermissionsStatus =
22+ import ( 'expo-notifications' ) . NotificationPermissionsStatus ;
23+
24+ let notificationsModulePromise : Promise < NotificationsModule > | null = null ;
25+
26+ const getNotificationsModule =
27+ async ( ) : Promise < NotificationsModule | null > => {
28+ if ( isRunningInExpoGo ( ) ) return null ;
29+
30+ notificationsModulePromise ??= import ( 'expo-notifications' ) ;
31+ return notificationsModulePromise ;
32+ } ;
33+
2034const normalizeExtras = ( extras : unknown ) : Record < string , string > | null => {
2135 if ( ! extras ) return null ;
2236 if ( typeof extras === 'string' ) {
@@ -238,7 +252,8 @@ const handleCustomMessage = (result: unknown) => {
238252} ;
239253
240254const hasGrantedPushPermission = (
241- settings : Notifications . NotificationPermissionsStatus
255+ settings : NotificationPermissionsStatus ,
256+ Notifications : NotificationsModule
242257) => {
243258 return (
244259 settings . granted ||
@@ -247,8 +262,11 @@ const hasGrantedPushPermission = (
247262} ;
248263
249264export const ensurePushPermission = async ( requestIfNeeded = false ) => {
265+ const Notifications = await getNotificationsModule ( ) ;
266+ if ( ! Notifications ) return false ;
267+
250268 const currentSettings = await Notifications . getPermissionsAsync ( ) ;
251- if ( hasGrantedPushPermission ( currentSettings ) ) {
269+ if ( hasGrantedPushPermission ( currentSettings , Notifications ) ) {
252270 return true ;
253271 }
254272
@@ -257,7 +275,7 @@ export const ensurePushPermission = async (requestIfNeeded = false) => {
257275 }
258276
259277 const requestedSettings = await Notifications . requestPermissionsAsync ( ) ;
260- return hasGrantedPushPermission ( requestedSettings ) ;
278+ return hasGrantedPushPermission ( requestedSettings , Notifications ) ;
261279} ;
262280
263281const registerJPushListeners = async ( ) => {
@@ -297,6 +315,9 @@ export const initializeJPush = async ({
297315 return false ;
298316 }
299317
318+ const Notifications = await getNotificationsModule ( ) ;
319+ if ( ! Notifications ) return false ;
320+
300321 if ( Platform . OS === 'android' ) {
301322 await Notifications . setNotificationChannelAsync ( 'tips' , {
302323 name : '消息通知' ,
0 commit comments