OneSignal / react-native-onesignal

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

[question]: getIdAsync is returning null with latest SDK #1708

Open rikinshah23 opened 6 months ago

rikinshah23 commented 6 months ago

How can we help?

I am using version "react-native-onesignal": "^5.1.3",

Initializing the OneSignal SDK in App.js in my React-native application like so:

useEffect(() => {
    StatusBar.setBarStyle('dark-content');
    //OneSignal Init Code
    OneSignal.initialize(cloudFunctionConfig.OneSignalAppID);
  }, []);

I am using getIdAsync and getOnesignalId functions and it always returning null

hasOptedIn always comes as true so that part is working.

getTokenAsync also returns the value.

useEffect(() => {
    async function setPlayerId() {
      try {
        if (!OneSignal) {
          console.log('OneSignal is not initialized');
          return;
        }
        console.log('OneSignal', OneSignal);

        OneSignal.login(user?.uid);
        OneSignal.User.pushSubscription.optIn();

        const hasOptedIn =
          await OneSignal.User.pushSubscription.getOptedInAsync();
        console.log('hasOptedIn', hasOptedIn);

        const onesignalId = await OneSignal.User.getOnesignalId();
        console.log('onesignalId', onesignalId);
        const playerId = await OneSignal.User.pushSubscription.getIdAsync();

        console.log(
          'getPushSubscriptionId',

          await OneSignal.User.pushSubscription.getIdAsync(),
          await OneSignal.User.pushSubscription.getTokenAsync(),
          await OneSignal.User.pushSubscription.getOptedInAsync(),
        );

        console.log('playerId', playerId);
      } catch (error) {
        console.log('setPlayerId error', error);
      } finally {
        setEffect2Done(true);
      }
    }
    setPlayerId();
  }, [user, loadingUser]);

Code of Conduct

wneild commented 6 months ago

Experiencing the same thing with 5.1.3 and the latest release 5.2.0. Works fine with 5.1.0.

nan-li commented 6 months ago

Hi @rikinshah23 and @wneild are you seeing this on both iOS and Android?

blackshadev commented 6 months ago

Experiencing the same thing with 5.2.0 on android. It seems to work fine on iOS.

Also I reverted back to 5.0.0 and using the sync version of the API , I also cannot see the notificationId anymore.

emanusantos commented 5 months ago

same issue here with latest releases. any updates on this?

jdmoliner commented 4 months ago

Same issue here with 5.2.1. This is my code:

useEffect(() => {
    const initialize = async () => {

      // OneSignal Initialization
      OneSignal.initialize(Constants.expoConfig.extra.oneSignalAppId);
      OneSignal.Notifications.requestPermission(true);
      console.log("requestPermission:");
      OneSignal.Notifications.addEventListener("click", (event) => {
        console.log("OneSignal: notification clicked:", event);
      });
      OneSignal.Notifications.addEventListener("foregroundWillDisplay", (event) => {
        event.getNotification().display();
      });
      OneSignal.Notifications.addEventListener("permissionChange", (granted) => {
        console.log("OneSignal: permission changed:", granted);
        fetchAndSendSubscriptionInfo();
      });

      const fetchAndSendSubscriptionInfo = async () => {
        try {
          const subId = await OneSignal.User.pushSubscription.getIdAsync();
          console.log("subId:", subId); // <----- here I get null
          if (subId) await updateSubscriptionToken(subId);
        } catch (error) {
          console.error("Error fetching subscription info:", error);
        }
      };
      fetchAndSendSubscriptionInfo();
    };

    initialize();
  }, []);
jkasten2 commented 4 months ago

To help debug the issue can provide the following details?

  1. Is the User and Subscription being shown on the OneSignal dashboard?

  2. Can you enable OneSignal Verbose logging and share the log with us?

    OneSignal.Debug.setLogLevel(LogLevel.Verbose);
    • For Android share the logcat, for iOS start your app from Xcode and share the runtime log.
VBarzionov commented 3 months ago

In my case (Android, OneSignal 5.1, 5.2) getIdAsync() returns NULL after very first run after app install.

OneSignal.initialize(ONESIGNAL_APP_ID);
OneSignal.Notifications.requestPermission(true).then(
  granted => {
    if (granted) {
      const subscriptionId = OneSignal.User.pushSubscription.getIdAsync();
      // if Android13+ subscriptionId NOT NULL  => system asks for perms
      // else NULL first time
  }
});
midhunevolvier commented 3 months ago

Experiencing the same thing with 5.2.2, any updates on this?

mtebele commented 1 month ago

In my case, users who installed our app with OneSignal v4 and then updated it to the version with OneSignal v5 are not receiving notifications until they open the app. So, we cannot reach users who automatically updated the app!

We need an urgent solution.

vipul-dm commented 2 weeks ago

Experiencing the same with version 5.1.3 Is there any Solution to this yet as OneSignal ID is returning null only on the initial app installation, but it works correctly on subsequent app launches after quitting.