How to get the unique identifier of a One Signal player in iOS?

How to get the unique identifier of the OneSignal player in iOS? I just installed the iOS SDK in the official OneSignal documentation.

Thank you for any suggestions.

+5
source share
3 answers

You need to use OneSignal watchers, such as OSSubscriptionObserver .

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

For a better explanation, here is the documentation for addSubscriptionObserver

+15
source

I need to get the player ID (or UserId) somewhere inside my code, and I don't want to save it somewhere.

I ended up using this code:

 let userId = OneSignal.getPermissionSubscriptionState().subscriptionStatus.userId 
+1
source

You can find it inside standardUserDefaults. You can get it using the following. I believe that it is installed the first time the application is launched, however it cannot be installed the first time application:didFinishLaunchingWithOptions: called.

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

You can see what else is stored in user defaults by looking at the dictionary view: UserDefaults.standard.dictionaryRepresentation()

-4
source

Source: https://habr.com/ru/post/1268652/


All Articles