2017-06-08 27 views

答えて

13

OSSubscriptionObserverなどのOneSignalのオブザーバを使用する必要があります。より良い説明のために

// Add OSSubscriptionObserver after UIApplicationDelegate 
class AppDelegate: UIResponder, UIApplicationDelegate, OSSubscriptionObserver { 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Add your AppDelegate as an subscription observer 
     OneSignal.add(self as OSSubscriptionObserver) 
    } 

    // After you add the observer on didFinishLaunching, this method will be called when the notification subscription property changes. 
    func onOSSubscriptionChanged(_ stateChanges: OSSubscriptionStateChanges!) { 
     if !stateChanges.from.subscribed && stateChanges.to.subscribed { 
     print("Subscribed for OneSignal push notifications!") 
     } 
     print("SubscriptionStateChange: \n\(stateChanges)") 

     //The player id is inside stateChanges. But be careful, this value can be nil if the user has not granted you permission to send notifications. 
     if let playerId = stateChanges.to.userId { 
     print("Current playerId \(playerId)") 
     } 
    } 
} 

は、ここにあなたがstandardUserDefaultsの内部でそれを見つけることができますaddSubscriptionObserver

+0

おかげで、これは魅力のように働きました! – aznelite89

+0

あなたは大歓迎です:) – GabrielaBezerra

+0

そんなことは難しいと思います。 OneSignalは、プレーヤーIDをログに出力する必要があります。 –

-2

のドキュメントです。あなたは以下を使ってそれを取り出すことができます。私は最初のアプリの起動時に設定されていると信じていますが、初めてapplication:didFinishLaunchingWithOptions:が呼び出されないことがあります。私はプレイヤーID(またはユーザーID)を取得する必要がありますUserDefaults.standard.dictionaryRepresentation()

0

私のコード内のどこか、と私はいけない:

UserDefaults.standard.string(forKey: "GT_PLAYER_ID") 

あなたは辞書の表現を見ることで、ユーザデフォルトに保存されている他のものを見ることができますどこにでも保存できます。

私はこのコードを使用して終了:

let userId = OneSignal.getPermissionSubscriptionState().subscriptionStatus.userId 
関連する問題