2
votes

In my app, I have setup a notification system using OneSignal and Firebase. However, when I prompt the OneSignal Notification Alert and the user allows notifications, the userID/playerID returns nil. Here is the following code:

static func getNotificationPermissionsAndStorePlayerIdInFireabse(currentUserId: String) {
    OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification
    OneSignal.promptForPushNotifications(userResponse: { accepted in
        if accepted == true {
            let status: OSPermissionSubscriptionState = OneSignal.getPermissionSubscriptionState()

            let userID = status.subscriptionStatus.userId
            let pushToken = status.subscriptionStatus.pushToken

            if pushToken != nil {
                if let playerID = userID {
                    FIRDatabase.database().reference().child("users").child(currentUserId).child("oneSignal").setValue([playerID: pushToken!])
                }
            }
        }
    })
}

In the code above I access the userID and push token, however, the userID returns nil. The push token, on the other hand, has a value. I don't know why the userId is nil. Any help would be appreciated.

PS: Please note I call this function after my user logs in.

1
try printing userID maybe value is not being added there and try printing your [ status.subscriptionStatus.userId ] too - iOS Geek
its printing nil - Rohan Vasishth
both printing nil ? - iOS Geek

1 Answers

0
votes

What version of OneSignal do you have? I have an app that uses OneSignal but I use different function to access user's OneSignal ID: (I actually couldn't find function you are using)

OneSignal.idsAvailable({ (userId: String?, registrationId: String?) 
  // Check for ID
  if let onesignalUserId = userId {
    // Save OneSignalId to Firebase
  }
})