2

I am trying to collect the OneSignalIDs of the users. The following code adds one playerID for each user. When the same user logs in with a different device, the playerID is updated.

    let status : OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
    let oneSignalUser = status.subscriptionStatus.userId

    if oneSignalUser != nil {
        let aUser = Auth.auth().currentUser!

        let post = ["playerID": oneSignalUser!] as [String: Any]
        Database.database().reference().child("usersToOneSignal").child(aUser.uid).setValue(post)
    }

enter image description here

I want every user to be added to every device. So I want more than one playerID under the key "kwvZPH......" (this is FireBase UserKey). How can I do this?

1
  • Use updateChildValues() as shown in documentation :=) Commented Sep 26, 2017 at 9:00

1 Answer 1

2

You can achieve what you want by saving the OneSignal ID as the key instead of the value. Then the playerID won't be overridden on login to a new device. The value doesn't matter, as long as it exists (I will set it to true).

let status : OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()
let oneSignalUserID = status.subscriptionStatus.userId

if oneSignalUser != nil {
    let aUser = Auth.auth().currentUser!

    // get the original path you were using
    let originalPath = Database.database().reference().child("usersToOneSignal").child(aUser.uid)
    // set `true` at the OneSignal key in the database
    originalPath.child(oneSignalUserID!).setValue(true)
}

Will give a database:

- usersToOneSignal
     |
     | - kwzphiusgdfoiaudbksdhf763truygi
       |
       | - 4e762345-76654afedc-65342287fc-57635472: true
       | - 7645736-efcab873465-7635476823f-ab62354: true 

Obviously you will need to change how you retrieve the values.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I tried to create a ChildKey from the UIDevice...uuidstring. But your method is much more useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.