2017-06-08 6 views
5

आईओएस में OneSignal उपयोगकर्ताओं की अद्वितीय प्लेयर आईडी कैसे पुनर्प्राप्त करें? मुझे केवल एक सिग्नल आधिकारिक दस्तावेज़ीकरण में आईओएस एसडीके सेटअप मिला।आईओएस में एक सिग्नल उपयोगकर्ता की अद्वितीय प्लेयर आईडी कैसे प्राप्त करें?

धन्यवाद अगर कोई सुझाव दिया गया है।

उत्तर

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

एक बेहतर विवरण के लिए, यहाँ के लिए addSubscriptionObserver

+0

धन्यवाद, यह एक आकर्षण की तरह काम किया! – aznelite89

+0

आपका स्वागत है :) – GabrielaBezerra

+0

मुझे लगता है कि ऐसी चीज के लिए मुश्किल है। OneSignal लॉग में प्लेयर आईडी मुद्रित करना चाहिए। –

-2

प्रलेखन आप इसे standardUserDefaults के अंदर मिल सकती है। आप इसे निम्नलिखित का उपयोग कर पुनर्प्राप्त कर सकते हैं। मेरा मानना ​​है कि यह पहले ऐप लॉन्च पर सेट है, हालांकि, यह पहली बार सेट नहीं किया जा सकता है application:didFinishLaunchingWithOptions: कहा जाता है।

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

आप देख सकते हैं और क्या उपयोगकर्ता चूक में शब्दकोश प्रतिनिधित्व को देखकर संग्रहीत किया जाता है: UserDefaults.standard.dictionaryRepresentation()

0

मैं अपने कोड के अंदर प्लेयर आईडी प्राप्त करने के लिए (या UserId) कहीं जरूरत है, और मुझे नहीं चाहते कहीं भी इसे बचाने के लिए।

मैं इस कोड का उपयोग समाप्त हो गया:

let userId = OneSignal.getPermissionSubscriptionState().subscriptionStatus.userId 
संबंधित मुद्दे