IOS device registration using Asynchronous Methods

Hi there
i want to register my swift app for push notification , i tried the synchronos methods first for testing as below


 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
 // Override point for customization after application launch.
 backendless.initApp(appid, secret:secretkey, version:appversion)
 backendless.messaging.registerForRemoteNotifications()
 return true
 }
 
 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
 backendless.messaging.registerDeviceToken(deviceToken)
 print("delay")
 }
 
 func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
 print("ERROE : \(error)")
 }

and its works fine but its slow my app lunch “of course” so i tried the asynchronous methods instead but its not working !! please could u check my code if i miss something


 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
 // Override point for customization after application launch.
 backendless.initApp(appid, secret:secretkey, version:appversion)
 backendless.messaging.registerForRemoteNotifications()
 return true
 }
 
 
 func registerDeviceToken(deviceToken:NSData!,responder : IResponder!){
 backendless.messaging.registerDeviceToken(deviceToken)
 print("\(responder.description)")
 
 }

Use attached project as example of push notification organization.

TestPushNotify.zip (19.55MB)

thanks for ur response but the example uses the same synchronous methods , and not the Asynchronous

Mriyadh, what method in our example is synchronous?

Before you may publish push messages you have to register a device:

        backendless.messaging.notificationTypes = UIUserNotificationType.Alert.rawValue | UIUserNotificationType.Sound.rawValue | UIUserNotificationType.Badge.rawValue
        backendless.messaging.registerDeviceWithChannels(["testing"])
        backendless.messaging.pushReceiver = self
  1. Set notification types
  2. Start the device registration process with the list of channels
  3. Set IBEPushReceiver object as pushReceiver delegate

That is all what you need to do. All further actions work for you asynchronous via app delegates.

If the device will be registered, didRegisterForRemoteNotificationsWithDeviceId delegate will be called, and you can start a message publishing to registered channels, the received message will be got via didReceiveRemoteNotification(notification delegate.

Otherwise didFailToRegisterForRemoteNotificationsWithError delegate with error showing the problem will be called.

sorry for not be so clear , my app is not sending any push notification its only receive them and i publish them via backendless console .

in backendless Docs there is 2 ways to register the device token step 2
Registering the device token with Backendless Synchronous and Asynchronous as u can see that from my code i used the Synchronous method

and its worked fine for registering my device in the default channel and receive the notifications as well , my case is if the internet connection is not good this registration process will take some time and freeze the UI ( check my code \ print(“delay”) in the registration method for the device token ) so i need to use the Asynchronous methods instead to avoid such issues

https://backendless.com/documentation/messaging/ios/messaging_device_registration.htm

But I don’t see any asynchronous calls in your code.

You should use these methods in this case:

-(void)registerDeviceToken:(NSData *)deviceToken responder:(id <IResponder>)responder;

or

-(void)registerDevice:(NSArray<NSString*> *)channels expiration:(NSDate *)expiration token:(NSData *)deviceToken response:(void(^)(NSString *))responseBlock error:(void(^)(Fault *))errorBlock;

And I didn’t understand this your method - what do it do?

func registerDeviceToken(deviceToken:NSData!,responder : IResponder!){
backendless.messaging.registerDeviceToken(deviceToken)
print("\(responder.description)")
}

this method according to ur docs should register the device token in backendless right ?

i just want to register the device token using asynchronous method to avoid any freeze in my UI
this function freezes my UI









func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

        backendless.messaging.registerDeviceToken(deviceToken)

        print("delay")

    }

so i tried below method for registering the device token which is not working !!









    func registerDeviceToken(deviceToken:NSData!,responder : IResponder!){

    backendless.messaging.registerDeviceToken(deviceToken)

    print("\(responder.description)")

        

    }

plz if u didn’t understand my case could u just share the full code for registering the device token using asynchronous method which will solve my issue

backendless.messaging.registerDeviceToken(deviceToken) is sync - so what did you change?

ok in this case, how can i register the device token using async instead of sync ?

The simplest way is:

backendless.messaging.registerDeviceToken(deviceToken, responder: nil)

or to catch a result or error via blocks:

 backendless.messaging.registerDeviceToken(
 deviceToken,
 response: {(result: String!) -> () in
 print("Device has been registered: \(result)")
 },
 error: { (fault : Fault!) -> () in
 print("Server reported an error: \(fault)")
 })

thanx my friend works like charm , thanks again for ur great support, and i hope to add it to backendless docs may help someone else