OneSignal / react-native-onesignal

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

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

Open thoriqadillah opened 1 month ago

thoriqadillah commented 1 month ago

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

jkasten2 commented 1 month ago

@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:

thoriqadillah commented 1 month ago

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 commented 1 month ago

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