How to get One Signal unique user id in Mobile?
Asked Answered
H

18

34

I am developing a restaurant app. I am using One Signal in my app to send notifications. Here while I place the order I need to send the unique Id of the one signal for a specific user for getting notification of "Your Order is placed successfully... It is in Progress.Please wait". For that I need the user's One Signal user id. How can I get it? I am newbie to One Signal. Please help me.

Hazard answered 2/4, 2016 at 6:17 Comment(0)
H
57

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

This Above Code is Deprecated. Please use the below code.

String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId()
Hazard answered 2/4, 2016 at 7:17 Comment(7)
I was trying to do this on iOS and this explains how to do it. Its the same logic in iOSMonition
Sorry ..no idea for IOS ji @blackmoonHazard
Any solution for iOS @blackmonQuaggy
The above solution for android is deprecated Use: OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState(); status.getSubscriptionStatus().getUserId();Sethsethi
String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId() its returning null! :(Gilbye
Does not work for their latest release 4.0.0. :(Gilbye
@ParamaSudha What method we can use to get UserID from OneSignal in WebView? #72656994Putrescible
S
17

The above solution for android is deprecated Use:

OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
status.getSubscriptionStatus().getUserId();
Sethsethi answered 21/11, 2017 at 17:56 Comment(3)
A one liner: OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId()Nic
String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId() its returning null! :(Gilbye
Does not work for their latest release 4.0.0. :(Gilbye
W
14

Android Java

String userId = OneSignal.getDeviceState().getUserId();

Android Kotlin

val oneSignalUserID = OneSignal.getDeviceState()!!.userId

iOS OneSignal Version 3.6.0

guard let userID = OneSignal.getDeviceState().userId else { return }

iOS Old OneSignal Version

let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
    if let id = status.subscriptionStatus.userId {
    oneSignalPlayer_ID = id
    print("\nOneSignal UserId:", id)
}
Wield answered 1/7, 2021 at 13:25 Comment(2)
This solution works perfectly. Other solutions (including the accepted one) might be deprecated. They didn't work on Android Studio/Java.Handbarrow
What method we can use to get the use id from onesignal in web view app..? #72656994Putrescible
A
4

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

Abominable answered 11/1, 2018 at 2:9 Comment(0)
S
3

for iOS:

let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
    if let id = status.subscriptionStatus.userId {
    oneSignalPlayer_ID = id
    print("\nOneSignal UserId:", id)
}
Shanonshanta answered 28/6, 2017 at 3:15 Comment(0)
E
3

As per the latest documentation,

Can get Subscription-ID or Player-ID as below,

OneSignal.getUser().getPushSubscription().getId()

Similarly, we can get the token as below,

OneSignal.getUser().getPushSubscription().getToken()
Encumber answered 5/10, 2023 at 8:27 Comment(0)
F
1

Complete solution:

import android.app.Application;
import com.onesignal.OneSignal;

public class ApplicationClass extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // OneSignal Initialization
        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();
        String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId();
    }
}
Festive answered 8/5, 2020 at 10:12 Comment(2)
this line returns null OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId();Rive
What method we can use to get UserID from OneSignal in WebView? #72656994Putrescible
G
1

OneSignal 4.1 Android String playerId = OneSignal.getDeviceState().getUserId(); to get player id

Gadroon answered 11/2, 2021 at 13:33 Comment(1)
What method we can use to get UserID from OneSignal in WebView? #72656994Putrescible
A
1

For iOS 15 and as of OneSignal iOS SDK version 3.7.0

if let userId = OneSignal.getDeviceState().userId {
    print("OneSignal User ID: \(userId)")
}
Across answered 7/9, 2021 at 10:3 Comment(2)
What method we can use to get UserID from OneSignal in WebView? #72656994Putrescible
@HarishKolliparat damn I have no idea, this post has been 5 years old. A lot has changed.Across
T
1

On OneSignal v11.0 is OneSignal.getDeviceState() is deprecated. To get userId use this

User.pushSubscription.id

And for token use this:

User.pushSubscription.token

For more follow this: https://documentation.onesignal.com/docs/mobile-sdk#subscription-id

Toothsome answered 12/10, 2023 at 1:15 Comment(0)
H
0

@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);
Holocrine answered 6/9, 2017 at 3:49 Comment(0)
G
0
String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId()

This returns null unless the SubscriptionStatus is changed. Please follow the link below to know how you can get OneSignal userId from getSubscriptionStatus' JSONObject.

https://mcmap.net/q/450829/-sometimes-one-signal-returns-userid-as-null

Gilbye answered 2/12, 2020 at 11:34 Comment(0)
B
0

If you are using OneSignal version 2.6.1 and above:

var userId = OneSignal.shared.getPermissionSubscriptionState().then((userObject) => userObject.subscriptionStatus.userId).toString();
Berg answered 19/12, 2020 at 13:50 Comment(0)
L
0

Add this to your Application Class

public void onCreate() {
   ...
   OneSignal.initWithContext(this);
   OneSignal.setAppId(ONESIGNAL_APP_ID);

   // Use One Signal User ID
   OneSignal.getDeviceState().getUserId()
   ...

}

Lissalissak answered 16/3, 2021 at 6:9 Comment(1)
What method we can use to get UserID from OneSignal in WebView? #72656994Putrescible
T
0

In Kotlin use below code.

val oneSignalUserID = OneSignal.getDeviceState()!!.userId
Tirewoman answered 2/6, 2021 at 12:56 Comment(1)
@4b0 What method we can use to get UserID from OneSignal in WebView? #72656994Putrescible
U
0

Use this code to get player id

public class MainActivity extends Activity implements OSSubscriptionObserver {
  protected void onCreate(Bundle savedInstanceState) {
    OneSignal.addSubscriptionObserver(this);
  }
  
  public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
    if (!stateChanges.getFrom().getSubscribed() &&
        stateChanges.getTo().getSubscribed()) {
         new AlertDialog.Builder(this)
             .setMessage("You've successfully subscribed to push notifications!")
             .show();
        // get player ID
        stateChanges.getTo().getUserId();
      }
   
      Log.i("Debug", "onOSSubscriptionChanged: " + stateChanges);
  }
}
Uchida answered 9/2, 2022 at 17:5 Comment(0)
N
0

Try this code to get the player id.

OneSignal Version - com.onesignal:OneSignal:4.7.4

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.Nullable;
import com.onesignal.OSSubscriptionObserver;
import com.onesignal.OSSubscriptionStateChanges;
import com.onesignal.OneSignal;

public class OneSignal_PlayerID extends Activity implement OSSubscriptionObserver {

private static String TAG = "OneSignal_PlayerID";
private static final String ONESIGNAL_APP_ID = "xxx-xxx-xxx-xxx";

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    OneSignal.initWithContext(this);
    OneSignal.setAppId(ONESIGNAL_APP_ID);
    OneSignal.addSubscriptionObserver(this);

}

@Override
public void onOSSubscriptionChanged(OSSubscriptionStateChanges stateChanges) {
    if (stateChanges.getTo().isSubscribed()){
        String playerId = stateChanges.getTo().getUserId();
        if (playerId!=null){
            Log.i(TAG,"OneSignal Player ID : "+playerId);
        }
     }
   }
}
Novelistic answered 26/6, 2022 at 9:55 Comment(1)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Mackenziemackerel
C
-1

All of the above did not work for me in android. from the latest version, I got this solution.

var userId = OneSignal.getDeviceState().then((value) => {
  var deviceInfo= JSON.stringify(value);
  console.log(deviceInfo.userId)
})

for anyone like me can use the above code to get a unique UserId.

Cosgrove answered 16/1, 2021 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.