如何在 Mobile 中获取 One Signal 唯一用户 ID?
How to get One Signal unique user id in Mobile?
我正在开发一个餐厅应用程序。我在我的应用程序中使用 One Signal 来发送通知。在这里,当我下订单时,我需要为特定用户发送一个信号的唯一 ID,以获得 "Your Order is placed successfully... It is in Progress.Please wait" 的通知。为此,我需要用户的 One Signal 用户 ID。我怎么才能得到它?我是 One Signal 的新手。请帮助我。
使用OneSignal官方网站中的以下代码获取了OneSignal的唯一ID。
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);
}
});
参考:
以上代码已弃用。请使用以下代码。
String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId()
对于iOS:
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
if let id = status.subscriptionStatus.userId {
oneSignalPlayer_ID = id
print("\nOneSignal UserId:", id)
}
@Lakshmi Reddy
在 iOS 中,您可以按如下方式操作:
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);
android 的上述解决方案已弃用
使用:
OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
status.getSubscriptionStatus().getUserId();
对于 android,您可以使用 getPermissionSubscriptionState 方法和 addSubscriptionObserver 从 3.5.0+ 版本的本机 Android SDK 开始获取用户 ID,即 playerID。
有关使用 getPermissionSubscriptionState 的更多信息,请参阅:https://documentation.onesignal.com/docs/android-native-sdk#section--getpermissionsubscriptionstate-
添加订阅观察者见:https://documentation.onesignal.com/docs/android-native-sdk#section--addsubscriptionobserver-
Github 上有一个本机 Android 示例:
https://github.com/OneSignal/OneSignal-Android-SDK
它位于标记为 AndroidStudio
的示例文件夹中
对于iOS,您可以使用getPermissionSubscriptionState 方法和addSubscriptionObserver 获取原生iOS SDK 2.5.0 版本开始的playerID。
有关使用 getPermissionSubscriptionState 的更多信息,请参阅:https://documentation.onesignal.com/docs/ios-native-sdk#section--getpermissionsubscriptionstate-
添加订阅观察者见:https://documentation.onesignal.com/docs/ios-native-sdk#section--addsubscriptionobserver-
Github 上的 Swift 中有一个 iOS 示例项目可能会有所帮助:https://github.com/OneSignal/OneSignal-iOS-SDK/tree/master/Examples
完整的解决方案:
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();
}
}
String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId()
除非更改 SubscriptionStatus,否则此 returns 为 null。请按照下面的 link 了解如何从 getSubscriptionStatus 的 JSONObject 中获取 OneSignal userId。
如果您使用的是 OneSignal 2.6.1 及以上版本:
var userId = OneSignal.shared.getPermissionSubscriptionState().then((userObject) => userObject.subscriptionStatus.userId).toString();
以上所有内容在 android 中都不适合我。
从最新版本,我得到了这个解决方案。
var userId = OneSignal.getDeviceState().then((value) => {
var deviceInfo= JSON.stringify(value);
console.log(deviceInfo.userId)
})
对于像我这样的任何人都可以使用上面的代码来获取唯一的 UserId。
OneSignal 4.1 Android String playerId = OneSignal.getDeviceState().getUserId();
获取玩家id
将此添加到您的申请中Class
public void onCreate() {
...
OneSignal.initWithContext(this);
OneSignal.setAppId(ONESIGNAL_APP_ID);
// Use One Signal User ID
OneSignal.getDeviceState().getUserId()
...
}
在 Kotlin 中使用下面的代码。
val oneSignalUserID = OneSignal.getDeviceState()!!.userId
Android Java
String userId = OneSignal.getDeviceState().getUserId();
Android科特林
val oneSignalUserID = OneSignal.getDeviceState()!!.userId
iOS OneSignal 版本 3.6.0
guard let userID = OneSignal.getDeviceState().userId else { return }
iOS 旧 OneSignal 版本
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
if let id = status.subscriptionStatus.userId {
oneSignalPlayer_ID = id
print("\nOneSignal UserId:", id)
}
对于 iOS 15 和 OneSignal iOS SDK 版本 3.7.0
if let userId = OneSignal.getDeviceState().userId {
print("OneSignal User ID: \(userId)")
}
使用此代码获取玩家 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);
}
}
我正在开发一个餐厅应用程序。我在我的应用程序中使用 One Signal 来发送通知。在这里,当我下订单时,我需要为特定用户发送一个信号的唯一 ID,以获得 "Your Order is placed successfully... It is in Progress.Please wait" 的通知。为此,我需要用户的 One Signal 用户 ID。我怎么才能得到它?我是 One Signal 的新手。请帮助我。
使用OneSignal官方网站中的以下代码获取了OneSignal的唯一ID。
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);
}
});
参考:
以上代码已弃用。请使用以下代码。
String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId()
对于iOS:
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
if let id = status.subscriptionStatus.userId {
oneSignalPlayer_ID = id
print("\nOneSignal UserId:", id)
}
@Lakshmi Reddy
在 iOS 中,您可以按如下方式操作:
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);
android 的上述解决方案已弃用 使用:
OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
status.getSubscriptionStatus().getUserId();
对于 android,您可以使用 getPermissionSubscriptionState 方法和 addSubscriptionObserver 从 3.5.0+ 版本的本机 Android SDK 开始获取用户 ID,即 playerID。
有关使用 getPermissionSubscriptionState 的更多信息,请参阅:https://documentation.onesignal.com/docs/android-native-sdk#section--getpermissionsubscriptionstate-
添加订阅观察者见:https://documentation.onesignal.com/docs/android-native-sdk#section--addsubscriptionobserver-
Github 上有一个本机 Android 示例: https://github.com/OneSignal/OneSignal-Android-SDK
它位于标记为 AndroidStudio
的示例文件夹中对于iOS,您可以使用getPermissionSubscriptionState 方法和addSubscriptionObserver 获取原生iOS SDK 2.5.0 版本开始的playerID。
有关使用 getPermissionSubscriptionState 的更多信息,请参阅:https://documentation.onesignal.com/docs/ios-native-sdk#section--getpermissionsubscriptionstate-
添加订阅观察者见:https://documentation.onesignal.com/docs/ios-native-sdk#section--addsubscriptionobserver-
Github 上的 Swift 中有一个 iOS 示例项目可能会有所帮助:https://github.com/OneSignal/OneSignal-iOS-SDK/tree/master/Examples
完整的解决方案:
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();
}
}
String UUID = OneSignal.getPermissionSubscriptionState().getSubscriptionStatus().getUserId()
除非更改 SubscriptionStatus,否则此 returns 为 null。请按照下面的 link 了解如何从 getSubscriptionStatus 的 JSONObject 中获取 OneSignal userId。
如果您使用的是 OneSignal 2.6.1 及以上版本:
var userId = OneSignal.shared.getPermissionSubscriptionState().then((userObject) => userObject.subscriptionStatus.userId).toString();
以上所有内容在 android 中都不适合我。 从最新版本,我得到了这个解决方案。
var userId = OneSignal.getDeviceState().then((value) => {
var deviceInfo= JSON.stringify(value);
console.log(deviceInfo.userId)
})
对于像我这样的任何人都可以使用上面的代码来获取唯一的 UserId。
OneSignal 4.1 Android String playerId = OneSignal.getDeviceState().getUserId();
获取玩家id
将此添加到您的申请中Class
public void onCreate() {
...
OneSignal.initWithContext(this);
OneSignal.setAppId(ONESIGNAL_APP_ID);
// Use One Signal User ID
OneSignal.getDeviceState().getUserId()
...
}
在 Kotlin 中使用下面的代码。
val oneSignalUserID = OneSignal.getDeviceState()!!.userId
Android Java
String userId = OneSignal.getDeviceState().getUserId();
Android科特林
val oneSignalUserID = OneSignal.getDeviceState()!!.userId
iOS OneSignal 版本 3.6.0
guard let userID = OneSignal.getDeviceState().userId else { return }
iOS 旧 OneSignal 版本
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
if let id = status.subscriptionStatus.userId {
oneSignalPlayer_ID = id
print("\nOneSignal UserId:", id)
}
对于 iOS 15 和 OneSignal iOS SDK 版本 3.7.0
if let userId = OneSignal.getDeviceState().userId {
print("OneSignal User ID: \(userId)")
}
使用此代码获取玩家 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);
}
}