Backendless Support
 
Solved

Swift Device Registration

In my app delegate, I have the following:

  1. 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.

Leave a Comment

Comments (4)

photo
1

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

photo
1

I'm using 3.x

photo
1

Please, check this code:

  1. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  2. backendless.hostURL = "http://api.backendless.com";
  3. backendless.initApp("XXXXX",
  4. secret: "XXXXX",
  5. version: "v1")
  6. let center = UNUserNotificationCenter.current()
  7. center.delegate = self
  8. center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
  9. application.registerForRemoteNotifications()
  10. }
  11. return true
  12. }
  13. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  14. backendless.messaging.registerDeviceToken(deviceToken,
  15. response: {
  16. (result: String?) -> Void in
  17. print("Device token: \(deviceToken)")
  18. },
  19. error: {
  20. (fault: Fault?) -> Void in
  21. print("Fault: \(String(describing: fault))")
  22. })
  23. }
  24. func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
  25. print("Failed to register from remote notifictions: \(error)")
  26. }

Regards, Olga

photo
1

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

photo