Reputation: 2095
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.
Upvotes: 17
Views: 16073
Reputation: 3098
IOS with 'OneSignal/OneSignal', '>= 5.0.0', '< 6.0'
you can get from this:
func getOneSignalUserId()-> String {
let playerId : String = OneSignal.User.pushSubscription.id ?? ""
return playerId
}
Upvotes: 4
Reputation: 52
An even better solution right now is to use:
OneSignal.appId()
Upvotes: -1
Reputation: 566
This is what worked for me because the other solutions are deprecated:
let userId = OneSignal.getDeviceState().userId
Make sure you import OneSignal:
import OneSignal
Upvotes: 4
Reputation: 1
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>
Upvotes: 0
Reputation: 4600
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)")
Upvotes: 1
Reputation: 16639
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
Upvotes: 27
Reputation: 2052
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()
Upvotes: -8
Reputation: 950
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
Upvotes: 39