15

How to retrieve the OneSignal users' unique player id in iOS? I only found iOS SDK Setup in OneSignal Official Documentation.

Thanks if any suggestions are given.

Imad Ali
  • 3,261
  • 1
  • 25
  • 33
aznelite89
  • 2,025
  • 4
  • 21
  • 32

7 Answers7

39

You need to use OneSignal's observers 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

GabrielaBezerra
  • 920
  • 11
  • 16
27

I do need to get the Player Id (Or UserId) somewhere inside my code, and i dont want to save it anywhere.

I ended up using this code:

let userId = OneSignal.getPermissionSubscriptionState().subscriptionStatus.userId
MBH
  • 16,271
  • 19
  • 99
  • 149
  • Hi @MBH, when I used the your code let userId = OneSignal.getPermissionSubscriptionState().subscriptionStatus.userId , I am getting the following error "MUST PROVIDE A VALID CALLBACK" – Venkatesh Somu Oct 16 '18 at 04:49
2

This is what worked for me because the other solutions are deprecated:

let userId = OneSignal.getDeviceState().userId

Make sure you import OneSignal:

import OneSignal
Donat Kabashi
  • 384
  • 4
  • 10
1
    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 // This one
    print("userID = \(userID)")
    let pushToken = status.subscriptionStatus.pushToken
    print("pushToken = \(pushToken)")
Ahmed Safadi
  • 4,402
  • 37
  • 33
0

On WebViewGold-based apps on iOS (WKWebView / UIWebView) in combination with OneSignal ,you might need a little bit of back-end work to connect it to our OneSignal API: You should save the ?onesignal_push_id=XYZ parameter in a database, and fire the OneSignal API (https://documentation.onesignal.com/docs/onesignal-api) for that specific user as soon as a push notification should be sent. This has to be done by your back-end/webserver. WebViewGold only offers the built-in OneSignal API and the ability to deliver the ?onesignal_push_id=XYZ appendix to your WebView URL call.

So activate the „kPushEnhanceUrl“ option in Config.swift (by switching the value from false to true) to append the individual user ID via ?onesignal_push_id=XYZ to your WebView URL.

If your WebView URL is https://www.example.org, WebViewGold will call https://www.example.org?onesignal_push_id=XYZ instead. Only your FIRST URL request will get that GET variable, so save it in a session or in a cookie to access it on your linked pages. An alternative or additional way would be to retrieve & process the information on any page via JavaScript:

<script>

window.location.href = "getonesignalplayerid://";

alert(onesignalplayerid);

</script>
-1

An even better solution right now is to use:

OneSignal.appId()
-8

You can find it inside of standardUserDefaults. You can retrieve it using the following. I believe it is set at the first app launch, however, it might not be set the first time application:didFinishLaunchingWithOptions: is called.

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

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

michaellindahl
  • 2,012
  • 5
  • 36
  • 64