OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.57k stars 373 forks source link

Can't execute One Signal with hooks #1152

Closed AllyssonAlas closed 3 years ago

AllyssonAlas commented 3 years ago

Description: Just making a very small sample to later on install One Signal on my projects, so i copied and pasted the code from install guide docs and put it on a function and just call inside of an useEffect, I'm using the button to call the function just to test purposes.

import React, {useEffect} from 'react';
import {Button, Alert} from 'react-native';

import OneSignal from 'react-native-onesignal';

const App = () => {
  useEffect(() => {
    initOneSignal();
  }, []);

  async function initOneSignal() {
    try {
      OneSignal.setLogLevel(6, 0);
      OneSignal.setAppId('my-one-sginal-key');
      OneSignal.setRequiresUserPrivacyConsent(true);
      OneSignal.promptForPushNotificationsWithUserResponse((response) => {
        console.log('Prompt response:', response);
      });

      /* O N E S I G N A L  H A N D L E R S */

      OneSignal.setNotificationWillShowInForegroundHandler(
        (notifReceivedEvent) => {
          console.log('OneSignal: notification will show in foreground:', notifReceivedEvent );
          let notif = notifReceivedEvent.getNotification();

          const button1 = {
            text: 'Cancel',
            onPress: () => { notifReceivedEvent.complete() },
            style: 'cancel',
          };

          const button2 = {
            text: 'Complete',
            onPress: () => { notifReceivedEvent.complete(notif) },
          };

          Alert.alert('Complete notification?', 'Test', [button1, button2], { cancelable: true });
        },
      );
      OneSignal.setNotificationOpenedHandler((notification) => {
        console.log('OneSignal: notification opened:', notification);
      });
      OneSignal.setInAppMessageClickHandler((event) => {
        console.log('OneSignal IAM clicked:', event);
      });
      OneSignal.addEmailSubscriptionObserver((event) => {
        console.log('OneSignal: email subscription changed: ', event);
      });
      OneSignal.addSubscriptionObserver((event) => {
        console.log('OneSignal: subscription changed:', event);
        // this.setState({isSubscribed: event.to.isSubscribed});
      });
      OneSignal.addPermissionObserver((event) => {
        console.log('OneSignal: permission changed:', event);
      });

      const deviceState = await OneSignal.getDeviceState();

      console.log(deviceState);
    } catch (error) {
      console.log(error);
    }
  }

  return <Button title={'one signal'} onPress={initOneSignal} />;
};

export default App;

Environment react: 16.13.1 react-native" 0.63.4, react-native-onesignal: 4.0.3

installed by node using the docs: https://documentation.onesignal.com/docs/react-native-sdk-setup

Steps to Reproduce Issue: Just install and start the project

Anything else: The console.log is showing this string: "promptForPushNotificationsWithUserResponse: this function is not supported on Android" OneSignal.getDeviceState function is returning this object: {isEmailSubscribed: false, isSubscribed: false, isPushDisabled: false, hasNotificationPermission: true} Adb logcat is showing this when app starts:

E libc    : Access denied finding property "vendor.perf.gestureflingboost.enable"
01-26 16:31:03.617 30849 30849 E libc    : Access denied finding property "vendor.perf.gestureflingboost.enable"
01-26 16:31:03.647 30849 30897 E ion     : ioctl c0044901 failed with code -1: Invalid argument
01-26 16:31:03.679 30849 30898 E unknown:ReactNative: ReactInstanceManager.createReactContext: mJSIModulePackage null
01-26 16:31:03.811 30849 30904 E OneSignal: Cannot change requiresUserPrivacyConsent() from TRUE to FALSE
01-26 16:31:03.812 30849 30904 E unknown:ReactNative: ReactInstanceManager.attachRootViewToInstance()
01-26 16:31:04.160 30849 30904 E unknown:ReactRootView: runApplication: call AppRegistry.runApplication
01-26 16:31:04.393 30849 30849 E unknown:ReactNative: ReactInstanceManager.attachRootViewToInstance()
01-26 16:31:04.400 30849 30849 E unknown:ReactRootView: runApplication: call AppRegistry.runApplication
01-26 16:31:05.068  1499  1663 E WifiVendorHal: getWifiLinkLayerStats(l.940) failed {.code = ERROR_NOT_SUPPORTED, .description = }
01-26 16:31:05.258   855  1089 E storaged: getDiskStats failed with result NOT_SUPPORTED and size 0
01-26 16:31:05.667  2122 30927 E memtrack: Couldn't load memtrack module
01-26 16:31:07.060 30849 30942 E OneSignal: Cannot change requiresUserPrivacyConsent() from TRUE to FALSE
01-26 16:31:08.079  1499  1663 E WifiVendorHal: getWifiLinkLayerStats(l.940) failed {.code = ERROR_NOT_SUPPORTED, .description = }
01-26 16:31:11.092  1499  1663 E WifiVendorHal: getWifiLinkLayerStats(l.940) failed {.code = ERROR_NOT_SUPPORTED, .description = }
01-26 16:31:11.262  2724 30947 E memtrack: Couldn't load memtrack module
AllyssonAlas commented 3 years ago

UPDATE:

Errors from Android Studio's Logcat (filtered with "onesignal" keyword):

2021-01-26 17:11:49.736 3144-3365/? E/OneSignal: Cannot change requiresUserPrivacyConsent() from TRUE to FALSE
2021-01-26 17:11:49.737 3144-3365/? W/OneSignal: appContext set, but please call setAppId(appId) with a valid appId to complete OneSignal init!
2021-01-26 17:11:51.116 3144-3365/? V/OneSignal: setAppId called with id: 8fe68dc0-52b2-4c44-a788-1a66778d2e0a changing id from: null
2021-01-26 17:11:51.117 3144-3365/? V/OneSignal: Starting OneSignal initialization!
2021-01-26 17:11:51.120 3144-3365/? V/OneSignal: No class found, not setting up OSRemoteNotificationReceivedHandler
2021-01-26 17:11:51.121 3144-3365/? V/OneSignal: OneSignal SDK initialization delayed, waiting for privacy consent to be set.
2021-01-26 17:11:51.123 3144-3365/? E/OneSignal: Cannot change requiresUserPrivacyConsent() from TRUE to FALSE
2021-01-26 17:20:21.576 3144-3144/? D/OneSignal: onActivityPaused: com.testworkshop.MainActivity@43f3963
2021-01-26 17:20:21.576 3144-3144/? D/OneSignal: curActivity is NOW: null
2021-01-26 17:20:21.643 3144-3144/? D/OneSignal: onActivityStopped: com.testworkshop.MainActivity@43f3963
2021-01-26 17:20:21.643 3144-3144/? D/OneSignal: curActivity is NOW: null
2021-01-26 17:20:22.414 3144-3144/? D/OneSignal: onActivityDestroyed: com.testworkshop.MainActivity@43f3963
2021-01-26 17:20:22.414 3144-3144/? D/OneSignal: curActivity is NOW: null
zain1629 commented 3 years ago

Yeah same issue. Any work arounds?

rgomezp commented 3 years ago

Howdy, The issue is you are requiring privacy consent but you are not setting the consent to granted.

Reference

I.e: if you use the function setRequiresUserPrivacyConsent you need to actually provide consent via provideUserConsent.

Take a look at our React Native documentation for more info.

Enjoy!

AllyssonAlas commented 3 years ago

Thx, it worked! But why isn't this in the quickstart section?

rgomezp commented 3 years ago

Howdy, The quickstart section is aimed to help set up your app with OneSignal. For details regarding specific functions, please see the main SDK reference.

Enjoy!

YinhaoHe commented 3 years ago

hey I used promptForPushNotificationsWithUserResponse to prompt and successfully registered my ios devices. But when I push notification, there are nothing showing up on iphone, android works perfect with no issues at all. Any ideas?