iOS Development

react native – expo notification listener would not work on iOS gadget

Spread the love


I’m engaged on an online view cell utility primarily based on expo with the react-native library for iOS gadgets. my utility is a messenger and safety is essential for me so I need to get push notification physique and title in encrypted customized choice with an object and content-available. in end result physique and title would empty in push notification. in response to expo notification paperwork, we should use two listeners (addNotificationReceivedListener , addNotificationResponseReceivedListener ) to get notifications from Apple push notification service (apns). I put these listeners in my code as talked about in paperwork. Whether or not my app is killed, or is in background, and even in foreground, addNotificationReceivedListener will not name. I check my notifications both with title and physique and with out them ( with content-available: 1 ) however addNotificationReceivedListener not working.

bundle.json:

 "dependencies": {
    "@expo/webpack-config": "^18.1.3",
    "@react-native-async-storage/async-storage": "^1.19.8",
    "@react-native-community/netinfo": "^11.1.0",
    "expo": "~49.0.15",
    "expo-constants": "~14.4.2",
    "expo-contacts": "~12.4.0",
    "expo-dev-client": "~2.4.12",
    "expo-device": "~5.6.0",
    "expo-file-system": "^15.6.0",
    "expo-media-library": "^15.6.0",
    "expo-network": "~5.6.0",
    "expo-notifications": "~0.20.1",
    "expo-splash-screen": "~0.20.5",
    "expo-status-bar": "~1.6.0",
    "react": "18.2.0",
    "react-dom": "^18.2.0",
    "react-native": "0.72.6",
    "react-native-fs": "^2.20.0",
    "react-native-share": "^10.0.1",
    "react-native-web": "~0.19.9",
    "react-native-webview": "^13.6.3",
    "rn-fetch-blob": "^0.12.0",
    "expo-background-fetch": "~11.3.0"
  },
  "devDependencies": {
    "@babel/core": "^7.23.3"
  },

App.js

   Notifications.setNotificationHandler({
    handleNotification: async () => ({
        shouldShowAlert: true,
        shouldPlaySound: true,
        shouldSetBadge: true,
    }),
  });


   async operate registerForPushNotificationsAsync() {
    let token;
    /*await AsyncStorage.getItem('token')*/
    if (System.isDevice && !token) {
        const { standing: existingStatus } = await Notifications.getPermissionsAsync();
        let finalStatus = existingStatus;
        if (existingStatus !== 'granted') {
            const { standing } = await Notifications.requestPermissionsAsync();
            finalStatus = standing;
        }

        if (finalStatus !== 'granted') {
            //alert('Didn't get push token for push notification!');
            return;
        }
    

        token = await Notifications.getDevicePushTokenAsync();
        token = token.knowledge;

    }
    return token;
   }

   useEffect(() => {
     registerForPushNotificationsAsync().then((token) => {
                                setTimeout(() => {
                                    sendPushTokenToWebView(token);
                                }, 1000);
                        });
 
        notificationListener.present  = Notifications.
    addNotificationReceivedListener((notification) => {
            console.log(notification)});

        responseListener.present = Notifications.
   addNotificationResponseReceivedListener((response)  => {
           console.log(response)
        });

         return () => {
            Notifications.removeNotificationSubscription(notificationListener.present);
            Notifications.removeNotificationSubscription(responseListener.present);
        };
    }, []);

Leave a Reply

Your email address will not be published. Required fields are marked *