Can't register for Sendbird push notifications in Swift

221 Views Asked by At

I have an iOS app in which I am using Sendbird successfully. Nevertheless, the functionality of receiving notifications when the app is running in background does not work. In fact, when I try to register the user for these notifications in the AppSelegate.swift file, I get the error 800101 ERR_CONNECTION_REQUIRED, what suggests I am not connecting correctly to the Sendbird server. However, I have checked that the connection is established without giving errors as soon as the app launches and before trying to register for those notifications.

This is the code for establishing the connection:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        SBDMain.initWithApplicationId(Constants.SENDBIRD_APP_ID, useCaching: false) {

} completionHandler: { error in
    print(error)
}
    ...
    }

This is my code for trying to register:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
        SBDMain.registerDevicePushToken(deviceToken, unique: true, completionHandler: { (status, error) in
                if error == nil {
                    if status == SBDPushTokenRegistrationStatus.pending {
                        return
                    }
                    else {
                        print("Successfully registered")
                    }
                }
                else {
                     print(error)
                }
            })
    }

As I said, I have made sure the sendbird application is initialized before the registration, but for some reason, when trying to register for the push notifications I get the "connection required" error. The rest of the Sendbird functionalities work fine, so I guess the connection is well established. The notifications work fine on Android using the same data.

1

There are 1 best solutions below

1
On BEST ANSWER

Disclaimer: I am a Sendbird employee.

Hi Pepito,

I'm unsure specifically what version you're on however it looks like you may not be calling the actual connect method prior to trying to register the token.

There is a two step process when creating a connection. The first is initializing the SDK with your Application ID. The second is actually calling the connect method with the userId and accessToken(If you're using them).

// Initialize a SBDMain instance to use APIs in the client app.
SBDMain.initWithApplicationId(APP_ID, useCaching: false) {

} completionHandler: { error in

}

// The USER_ID below should be unique to your Sendbird application.
SBDMain.connect(withUserId: USER_ID, completionHandler: { (user, error) in
    guard error == nil else {
        // Handle error.
    }

    // The user is connected to Sendbird server.
    ...
})

If you're still having trouble after this, I suggest you reach out on our community forums so we can acquire more information from you. https://community.sendbird.com