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.
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:
This Above Code is Deprecated. Please use the below code.
String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId()
The above solution for android is deprecated Use:
OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
status.getSubscriptionStatus().getUserId();
OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId()
–
Nic 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)
}
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)
}
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()
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();
}
}
OneSignal 4.1 Android String playerId = OneSignal.getDeviceState().getUserId();
to get player id
For iOS 15 and as of OneSignal iOS SDK version 3.7.0
if let userId = OneSignal.getDeviceState().userId {
print("OneSignal User ID: \(userId)")
}
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
@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);
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
If you are using OneSignal version 2.6.1 and above:
var userId = OneSignal.shared.getPermissionSubscriptionState().then((userObject) => userObject.subscriptionStatus.userId).toString();
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()
...
}
In Kotlin use below code.
val oneSignalUserID = OneSignal.getDeviceState()!!.userId
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);
}
}
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);
}
}
}
}
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.
© 2022 - 2024 — McMap. All rights reserved.