2017-04-05 59 views
6

OneSignal的最新版本生成警告,如'idsAvailable'已被棄用。我怎麼能得到'playerID'使用getPermissionSubscriptionState()而不是idsAvailable,因爲我在下面做?一個信號 - idsAvailable已棄用

OneSignal.idsAvailable({ (userId, pushToken) in 

     if (pushToken != nil) { 

      if let playerID = userId { 

       // do something 

      } 
     } 
    }) 

回答

8

OneSignal保持真正的好文件。

對於android,您可以使用idsAvailable方法獲取用戶標識,該方法返回用戶標識和註冊標識。

userID aka playerID是OneSignal UUID格式化的字符串。 (每個應用每個設備都是唯一的) registrationID是Google分配的標識符(每個應用每個設備都是唯一的,並在重新安裝時發生更改)。

對於iOS,您可以以相同方式獲取用戶標識,但在本機iOS SDK的2.5.0+版本中添加了getPermissionSubscriptionState方法和addSubscriptionObserver。

你可以得到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 
    } 
} 
0

userIdplayerId相同,將它l在我的Swift 4應用程序中使用的一些示例分析(單例)類中:

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

}