Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIApplication.registerForRemoteNotifications() must be called from main thread only

Xcode 9 (iOS 11) showing me an error/warning while registering for Push (remote) notification.

Here is error message

enter image description here

And here is code, I've tried:

let center  = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
        if error == nil{
              UIApplication.shared.registerForRemoteNotifications()
        }
 }

Error/Warning Line:

UIApplication.shared.registerForRemoteNotifications()

How to resolve this?

like image 527
Krunal Avatar asked Jun 06 '17 13:06

Krunal


2 Answers

In swift4

You can solve this issue with

DispatchQueue.main.async {
  UIApplication.shared.registerForRemoteNotifications()
}

Hope this will help...

like image 173
Wasim K. Memon Avatar answered Nov 09 '22 00:11

Wasim K. Memon


For Objective C, the below code works

    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    });
like image 51
R. Mohan Avatar answered Nov 09 '22 00:11

R. Mohan