问题
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.
回答1:
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()
回答2:
The above solution for android is deprecated Use:
OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
status.getSubscriptionStatus().getUserId();
回答3:
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
回答4:
for iOS:
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
if let id = status.subscriptionStatus.userId {
oneSignalPlayer_ID = id
print("\nOneSignal UserId:", id)
}
回答5:
@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);
来源:https://stackoverflow.com/questions/36369910/how-to-get-one-signal-unique-user-id-in-mobile