React native onesignal android issue

57 Views Asked by At

I am building a react native app , i want to implement onesignal for push notifications . everything works perfectly on my ios device but with android device it wont subscribe the user after Onesignal.login(String(userId)). i m using react-native-onesignal 5.0.4 And react native 0.73

in App.js:

    useEffect(() => {
     OneSignal.Debug.setLogLevel(LogLevel.Verbose);

     OneSignal.initialize(
      Platform.OS === 'ios' ? ONESIGNAL_API_IOS : ONESIGNAL_API_ANDROID,
     );
     OneSignal.User.pushSubscription.addEventListener('change', () =>
      OneSignal.Notifications.requestPermission(true),
     );
     OneSignal.Notifications.requestPermission(true);
     }, []);

in login.js:

    const handleLogin = async () => {
    try {
      if (await validate()) {
        const response = await dispatch(userLogin(login));
        const userId = response?.payload?.user?.id;

        console.log(response);
        if (userId) {
          console.log(userId)
          OneSignal.login(String(userId));
        } else {
          console.error('User ID not found in the response data');
        }
      } else {
        console.error('Form validation failed');
      }
    } catch (error) {
      console.log('An error occurred during login:', error);
    }
    };
0

There are 0 best solutions below