OneSignal 的最新构建版本会生成一个警告,如“ idsAvailable ”已弃用。我如何使用 getPermissionSubscriptionState() 而不是 idsAvailable 来获取“playerID”,就像我在下面所做的那样?

    OneSignal.idsAvailable({ (userId, pushToken) in

        if (pushToken != nil) {

            if let playerID = userId {

                // do something

            }
        }
    })

最佳答案

OneSignal 保留了非常好的文档。

对于android,您可以使用返回用户ID 和注册ID 的idsAvailable 方法获取用户ID。

userID aka playerID 是 OneSignal UUID 格式的字符串。 (每个设备每个应用程序唯一)
registrationID 是 Google 分配的标识符(每个设备每个应用程序唯一,重新安装时更改)。

对于 iOS,您可以通过相同的方式获取用户 ID,但在 native 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
    }
}

关于ios - 一个信号 - idsAvailable 已弃用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43230152/

10-12 03:37