Support Topics Documentation Slack YouTube Blog

Swift Device Registration


(Phil Boyd) #1

In my app delegate, I have the following:


 func registerDevice() {
 backendless?.messaging.registerForRemoteNotifications()
 }
 
 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
 backendless?.messaging.registerDeviceToken(deviceToken)
 }
 
 func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
 // handle error
 }

My didRegister and didFailToRegister functions are not called after attempting to get a token.


(Olga Danylova) #2

Hello Phil,
In order to allow other users with similar issues review this topic I have to make it public. OK?
What version of Backendless are you using?

Regards, Olga


(Phil Boyd) #3

I’m using 3.x


(Olga Danylova) #4

Please, check this code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        backendless.hostURL = "http://api.backendless.com"
        backendless.initApp("XXXXX",
                            secret: "XXXXX",
                            version: "v1")
        
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
             application.registerForRemoteNotifications()
        }
        return true
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        backendless.messaging.registerDeviceToken(deviceToken,
                                                  response: {
                                                    (result: String?) -> Void in
                                                    print("Device token: \(deviceToken)")
        },
                                                  error: {
                                                    (fault: Fault?) -> Void in
                                                    print("Fault: \(String(describing: fault))")
        })
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to register from remote notifictions: \(error)")
    }

Regards, Olga


(Phil Boyd) #5

Thank you for the help Olga! The functions are being called now.