Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Signal - idsAvailable is deprecated

OneSignal's latest build version generate a warning like 'idsAvailable' is deprecated. How can I get 'playerID' using getPermissionSubscriptionState() instead idsAvailable as I did below?

    OneSignal.idsAvailable({ (userId, pushToken) in

        if (pushToken != nil) {

            if let playerID = userId {

                // do something

            }
        }
    })
like image 428
iamburak Avatar asked Apr 05 '17 11:04

iamburak


1 Answers

OneSignal keeps really good documentation.

For android you can get userIDs by using the idsAvailable method which returns the userID and registrationID.

userID aka playerID is a OneSignal UUID formatted string. (unique per device per app) registrationID is a Google assigned identifier (unique per device per app and changes on reinstalls).

For iOS, you can get the userIDs in the same way but in version 2.5.0+ of the native iOS SDK added getPermissionSubscriptionState method and addSubscriptionObserver.

You can get the OSPermissionSubscriptionState for Swift as follows:

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)")

For Objective-C:

OSPermissionSubscriptionState* status = [OneSignal getPermissionSubscriptionState];
status.permissionStatus.hasPrompted
status.permissionStatus.status

status.subscriptionStatus.subscribed
status.subscriptionStatus.userSubscriptionSetting
status.subscriptionStatus.userId
status.subscriptionStatus.pushToken

So your above code will now look something like this:

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
    }
}
like image 167
JonF Avatar answered Nov 05 '22 00:11

JonF