How to get One Signal unique user id in Mobile?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 02:22:27

Got the unique Id of OneSignal by using the following code which is in official site of OneSignal.

OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
        @Override
        public void idsAvailable(String userId, String registrationId) {
            Log.d("debug", "User:" + userId);
            if (registrationId != null)
                Log.d("debug", "registrationId:" + registrationId);

        }
    });

Refer:

Getting code in Official Site

The above solution for android is deprecated Use:

OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
status.getSubscriptionStatus().getUserId();

For android, you can get the userIDs aka playerIDs starting in the 3.5.0+ version of the native Android SDK using the getPermissionSubscriptionState method and addSubscriptionObserver.

For more on using getPermissionSubscriptionState see: https://documentation.onesignal.com/docs/android-native-sdk#section--getpermissionsubscriptionstate-

For adding subscription observers see: https://documentation.onesignal.com/docs/android-native-sdk#section--addsubscriptionobserver-

There is a Native Android example on Github: https://github.com/OneSignal/OneSignal-Android-SDK

It is in the Examples folder labeled AndroidStudio

For iOS, you can get the playerIDs starting in the 2.5.0 version of the native iOS SDK using the getPermissionSubscriptionState method and addSubscriptionObserver.

For more on using getPermissionSubscriptionState see: https://documentation.onesignal.com/docs/ios-native-sdk#section--getpermissionsubscriptionstate-

For adding subscription observers see: https://documentation.onesignal.com/docs/ios-native-sdk#section--addsubscriptionobserver-

There is an iOS Example project in Swift on Github that might be of some assistance: https://github.com/OneSignal/OneSignal-iOS-SDK/tree/master/Examples

for iOS:

let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
    if let id = status.subscriptionStatus.userId {
    oneSignalPlayer_ID = id
    print("\nOneSignal UserId:", id)
}

@Lakshmi Reddy

in iOS, you can do like below:

OSPermissionSubscriptionState* status = [OneSignal getPermissionSubscriptionState];
    status.permissionStatus.hasPrompted
    status.permissionStatus.status
//                                                            <- OneSignal idsAvailable is deprecated.it's the alternative
    status.subscriptionStatus.subscribed
    status.subscriptionStatus.userSubscriptionSetting
    status.subscriptionStatus.userId
    status.subscriptionStatus.pushToken
NSLog(@"Push Token - %@",status.subscriptionStatus.pushToken);
NSLog(@"Push User ID - %@",status.subscriptionStatus.userId);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!