react-native-onesignal
react-native-onesignal copied to clipboard
[question]: OneSignal.User.pushSubscription.getPushSubscriptionId() does not work
How can we help?
Need some help on getting the userId or getPushSubscriptionId from a giver user.
On the same code, I do the following:
OneSignal.Debug.setLogLevel(LogLevel.Verbose);
OneSignal.initialize(Constants.expoConfig.extra.oneSignalAppId);
OneSignal.Notifications.requestPermission(true);
const oneSignalUserPushId = OneSignal.User.pushSubscription.getPushSubscriptionId();
the first time the app runs, oneSignalUserPushId is always empty.
Previously, the userId was assigned regardless of the user accepted the push or not, is there a way to get the userId currently?
Thanks!
Code of Conduct
- [X] I agree to follow this project's Code of Conduct
If your code is more or less exactly the same as you posted, there is a limitation in the doc saying:
Any User namespace calls must be invoked after initialization. Example: OneSignal.User.addTag("tag", "2")
Maybe you should initialize the SDK, then inside a provider subscribe into permissionChange event and wait for confirmation and ONLY THEN ask for the ID. This justified delay may help in your case, but if you don't want to "justify", you can try if a setTimeout
works.
We also have a similar issue while using version 5.0.5. I can not reproduce this by myself, but I get corresponding Sentry-Exceptions.
- Initialize OneSignal during app start
OneSignal.Debug.setLogLevel(LogLevel.Verbose);
OneSignal.initialize(appId);
OneSignalHelper.requestPermission();
OneSignalHelper.addEventListener(...)
- Get the subscription ID. To make sure that OneSignal is correctly initialized, we do a 5-times retry with 2 seconds timeout each. Additionally, the user needs to fill in a login form before this function is executed. That means our last attempt to retrieve the subscription ID is typically 12-20 seconds after app start.
OneSignal.User.pushSubscription.getPushSubscriptionId()
Nevertheless, for some users we don't get a subscription ID. This happens on Android (13, 14) as well as on iOS (15.8 up to 17.4)
Hello there! In 5.1.0 we updated the Push Subscription property getters to be async so that the value is always correct. Please upgrade to this version and let us know if you are still having any issues.
Thanks!