react native – expo notification listener doesn’t work on iOS device


I am working on a web view mobile application based on expo with the react-native library for iOS devices. my application is a messenger and security is very important for me so I want to get push notification body and title in encrypted custom option with an object and content-available. in result body and title would empty in push notification. according to expo notification documents, we must use two listeners (addNotificationReceivedListener , addNotificationResponseReceivedListener ) to get notifications from Apple push notification service (apns). I put these listeners in my code as mentioned in documents. Whether my app is killed, or is in background, or even in foreground, addNotificationReceivedListener won’t call. I test my notifications either with title and body and without them ( with content-available: 1 ) but addNotificationReceivedListener not working.

package.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 function registerForPushNotificationsAsync() {
    let token;
    /*await AsyncStorage.getItem('token')*/
    if (Device.isDevice && !token) {
        const { status: existingStatus } = await Notifications.getPermissionsAsync();
        let finalStatus = existingStatus;
        if (existingStatus !== 'granted') {
            const { status } = await Notifications.requestPermissionsAsync();
            finalStatus = status;
        }

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

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

    }
    return token;
   }

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

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

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

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img