OneSignal的最新版本会生成一个警告,例如' idsAvailable '已弃用。我怎样才能获得“玩家ID”#39;使用 getPermissionSubscriptionState()代替idsAvailable,就像我在下面做的那样?
OneSignal.idsAvailable({ (userId, pushToken) in
if (pushToken != nil) {
if let playerID = userId {
// do something
}
}
})
答案 0 :(得分:10)
OneSignal保留了非常好的文档。
对于Android,您可以使用返回userID和registrationID的idsAvailable
方法获取用户ID。
userID aka playerID是OneSignal UUID格式的字符串。 (每个应用程序每个设备唯一) registrationID是Google指定的标识符(每个应用每个设备唯一,重新安装时更改)。
对于iOS,您可以以相同的方式获取用户ID,但在本机iOS SDK的2.5.0+版本中添加了getPermissionSubscriptionState方法和addSubscriptionObserver。
您可以按如下方式获取Swift的OSPermissionSubscriptionState:
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
let hasPrompted = status.permissionStatus.hasPrompted
print("hasPrompted = \(hasPrompted)")
let userStatus = status.permissionStatus.status
print("userStatus = \(userStatus)")
let isSubscribed = status.subscriptionStatus.subscribed
print("isSubscribed = \(isSubscribed)")
let userSubscriptionSetting = status.subscriptionStatus.userSubscriptionSetting
print("userSubscriptionSetting = \(userSubscriptionSetting)")
let userID = status.subscriptionStatus.userId
print("userID = \(userID)")
let pushToken = status.subscriptionStatus.pushToken
print("pushToken = \(pushToken)")
对于Objective-C:
OSPermissionSubscriptionState* status = [OneSignal getPermissionSubscriptionState];
status.permissionStatus.hasPrompted
status.permissionStatus.status
status.subscriptionStatus.subscribed
status.subscriptionStatus.userSubscriptionSetting
status.subscriptionStatus.userId
status.subscriptionStatus.pushToken
所以上面的代码现在看起来像这样:
let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
let userID = status.subscriptionStatus.userId
print("userID = \(userID)")
let pushToken = status.subscriptionStatus.pushToken
print("pushToken = \(pushToken)")
if pushToken != nil {
if let playerID = userID {
// do something
}
}
答案 1 :(得分:0)
我刚刚在文档中发现,“userID”的意思是“playerID”。
https://documentation.onesignal.com/docs/ios-native-sdk#section--getpermissionsubscriptionstate-
答案 2 :(得分:0)
userId
与playerId
相同,将其全部放在我在Swift 4应用中使用的一些示例Analytics(singleton)类中:
class Analytics : NSObject, OSSubscriptionObserver {
///the singleton
static let shared = Analytics()
func setupOneSignal(launchOptions: [UIApplicationLaunchOptionsKey : Any]?) {
OneSignal.setLogLevel(.LL_VERBOSE, visualLevel: .LL_NONE)
OneSignal.add(self as OSSubscriptionObserver)
let settings : [String : Any] = [kOSSettingsKeyInAppLaunchURL : false]
OneSignal.initWithLaunchOptions(
launchOptions,
appId:Environment.oneSignalAppId,
handleNotificationReceived: {
(osnotification) in
//.....
},
handleNotificationAction: {
(osNotificationOpenedResult) in
//.....
},
settings: settings)
}
func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) {
if let state = OneSignal.getPermissionSubscriptionState(),
let userId = state.subscriptionStatus.userId,
let pushToken = state.subscriptionStatus.pushToken {
print("onesignal.userId : \(userId)")
print("onesignal.pushToken : \(pushToken)")
}
}
}