react-native-onesignal icon indicating copy to clipboard operation
react-native-onesignal copied to clipboard

[Bug]: Subscription is not set in the dashboard after `OneSignal.login(externalId)`

Open thoriqadillah opened this issue 1 year ago • 4 comments

What happened?

I am integrating one signal with managed expo workflow. my one signal version is ^5.2.2. Previously it worked and i receives notification as usual. But after i let other people handle this task and come back again and test it in my device, i'm unable to register my external id into the dashboard, hence i got no notification

this is the code here i am trying to migrate to one signal from expo notification, so i'm including the expo notification code just in case

const onOneSignalNotificationClicked = (event: NotificationClickEvent) => {
  console.log("OneSignal: notification clicked:", event);
};
 
async function registerOneSignal(token: string) {
  OneSignal.Debug.setLogLevel(LogLevel.Verbose);
  OneSignal.initialize(EXPO_PUBLIC_ONE_SIGNAL_APP_ID);

  await OneSignal.Notifications.requestPermission(true);

  OneSignal.login(token);
  OneSignal.Notifications.addEventListener(
    "click",
    onOneSignalNotificationClicked  
  );

  console.log('external id', await OneSignal.User.getExternalId());
  console.log('one signal id', await OneSignal.User.getOnesignalId());
  console.log('push subs id', await OneSignal.User.pushSubscription.getIdAsync());
  console.log('push subs token', await OneSignal.User.pushSubscription.getTokenAsync()); 
}


async function registerNotification() {
  const { status } = await Notifications.getPermissionsAsync();
  let permissionStatus = status;

  if (status !== "granted") {
    const { status } = await Notifications.requestPermissionsAsync();
    permissionStatus = status;
  }

  if (permissionStatus !== "granted") {
    return;
  }

  const { data: token } = await Notifications.getExpoPushTokenAsync({
    projectId: Constants.expoConfig?.extra?.eas.projectId,
  });

  console.log("Expo token: ", token);

  await registerOneSignal(token);
  await api.saveExpoToken(token, "onesignal");

  if (Platform.OS === "android") {
    Notifications.setNotificationChannelAsync("default", {
      name: "default",
      importance: Notifications.AndroidImportance.MAX,
      vibrationPattern: [0, 250, 250, 250],
      lightColor: "#4EAAF7",
    });
  }
}

Notifications.setNotificationHandler({
  handleNotification: async (notification) => {
    await handleNotification("received", notification);
    return {
      shouldShowAlert: true,
      shouldPlaySound: true,
      shouldSetBadge: false,
    };
  },
});

function App() {
  const lastNotif = Notifications.useLastNotificationResponse();

  useEffect(() => {
    registerNotification();

    Notifications.registerTaskAsync(BG_TASK);

    return () => {
      OneSignal.Notifications.removeEventListener(
        "click",
        onOneSignalNotificationClicked
      );
    };
  }, []);

  useEffect(() => {
    if (Platform.OS === "web") return;

    if (lastNotif) handleNotification("open", lastNotif.notification);
  }, [lastNotif]);
  
  // rest of the code

btw, how do i see the log of one signal? i'm using expo go to develop the app but i receives no log from one signal. but the console log returns this image

Steps to reproduce?

1. install one signal ^5.2.2
2. login by external id
3. external id is never been set

What did you expect to happen?

i expected my device included in the subscription in my dashboard

OneSignal Android SDK version

5.2.2

Android version

13

Specific Android models

- Expo go 49
- Samsung Galaxy A23

Relevant log output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

thoriqadillah avatar Jul 31 '24 14:07 thoriqadillah

@thoriqadillah

OneSignal.login(token);, you shouldn't use a push token as a userId as it's not a value that will be the same for multiple devices or even reinstalls of the app. Calling OneSignal.login is not required, so if you don't have a user concept in your app you can skip it.

btw, how do i see the log of one signal? i'm using expo go to develop the app but i receives no log from one signal.

You will need to use logcat to get these logs, there are two options to get them:

jkasten2 avatar Jul 31 '24 19:07 jkasten2

i think i still can use the push token because i also save them to the db and assign them with user id if user decided to login. So how do one signal know which device to target if i don't call login function?

thoriqadillah avatar Aug 01 '24 01:08 thoriqadillah

hi there, any update for this? i do have some update

fyi, i haven't pluged my phone to run the app, but i did tried using native emulator using android emulator just now, and the thing worked. my emulator is registered in one signal dashboard

now this is confusing, because when i tried to run it using expo go it did not work. but i assume one signal only runs in native environtment. okay, i tried to build the preview version and prod version but using staging api and both did not work

this is my eas.json to build the app

"preview:staging": {
  "distribution": "internal",
  "android": {
    "buildType": "apk"
  },
  "channel": "preview",
},

"production": {
  "channel": "production",
  "autoIncrement": true,
  "ios": {
    "image": "latest"
  },
},

i built both in local. for preview:staging i just straight up move the apk to my phone and installed it. but for the prod, i uploaded the aab to google play console and donwloaded the signed apk.

i can provide you with the log necessary, but in order to provide log, i need to run this app using the native version and using log cat. but the thing is, if run this in native env, its gonna work, so i think the log will achieve nothing, at least in my assumption. so i just want to update you this

thoriqadillah avatar Aug 05 '24 05:08 thoriqadillah

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.

mtebele avatar Oct 09 '24 04:10 mtebele

same here, any updates?

ghost avatar Nov 29 '24 14:11 ghost

Any updates?

mtebele avatar Jan 29 '25 16:01 mtebele

Hey everyone,

To help us debug this issue, is anyone able to provide logs that show something like a 400 error during the initialize() call? I suspect something is wrong with the initial subscription request but without more information it is difficult to deduce.

sherwinski avatar Jan 29 '25 21:01 sherwinski

@sherwinski how would I get those logs? Im running into same issue. iOS seems to be working fine, but Android is mostly a miss and randomly will work

GwFreak01 avatar Feb 03 '25 08:02 GwFreak01

@GwFreak01

If you are on expo, you will need to use logcat to get these logs, there are two options to get them:

Otherwise, you should be able to see the logs in Android Studio after setting OneSignal.Debug.setLogLevel(LogLevel.Verbose) before calling the initialize method.

nan-li avatar Feb 11 '25 04:02 nan-li

Without logs, it is hard to know what is going on, in order to provide any fixes or suggestions.

I do see the following issue mentioned, that was fixed in Release 5.2.9:

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!

Without logs, I am closing this issue as we do not have enough information to offer support. Please feel free to reach out with updated details if they are available.

nan-li avatar Jun 06 '25 00:06 nan-li