2

Yesterday I updated 2 of my apps to swift 3.

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
    UIApplication.shared.registerForRemoteNotifications()
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print(error.localizedDescription)
}



func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Save Installation if registered successfully
}


//MARK: Recieved Notification
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    print("received a notification")

}

so background push notification works fine on both apps, but in one of my app its not receiving any pushes in foreground that is, didReceiveRemoteNotification is never getting called

Things I have checked, Push Notifications enabled in Capabilities

Using this code to register for push notifications on both apps

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil))

Not only that, I have tried UNUserNotificationCenterDelegate for iOS 10 but still none of its delegate functions get called.

This only doesn't work on iOS 10 phones, iOS 8 and 9 works like charm. So I'm not really sure why its never calling didReceiveRemoteNotification in only one of my 2 swift 3 apps on ios 10 when the app is open

This is my iOS 10 code I tried

//Added in didFinishLaunchingWithOptions
    if #available(iOS 10.0, *) {
                let center  = UNUserNotificationCenter.current()
                center.delegate = self
                center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
                    if error == nil{
                        UIApplication.shared.registerForRemoteNotifications()
                    }
                }
            }

//Delegates
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("push2")
    }

    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("push1")
    }
user528432
  • 411
  • 2
  • 5
  • 18
  • `UIApplication.shared.registerUserNotificationSettings` But that's not how you do it in iOS 10. You use the UNUserNotificationCenter to register. Your `application:didReceiveRemoteNotification` and so forth are completely irrelevant. You need to rewrite your code _completely_. Watch the relevant WWDC 2016 videos (there are two of them). – matt Sep 24 '16 at 15:23
  • @matt as i wrote in the question, that I have tried UNUserNotificationCenter's delegates and the new registering code but still it doesn;t work, i have tried this http://stackoverflow.com/questions/39382852/didreceiveremotenotification-not-called-ios-10 as well, Also old code works fine on my other app thats what my questions says – user528432 Sep 24 '16 at 15:28
  • If you are compiling against iOS 10 you must switch to UNUserNotificationCenter. So you have two choices: switch, or don't compile against iOS 10. You have not shown any UNUserNotificationCenter code so I have no reason to believe you are doing it right. – matt Sep 24 '16 at 15:30
  • @matt updated the answer, – user528432 Sep 24 '16 at 15:46

1 Answers1

1
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        self.initializeNotificationServices()
}

 func initializeNotificationServices() -> Void {

        if #available(iOS 10.0, *) {

            UNUserNotificationCenter.currentNotificationCenter().delegate = self
            UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
                if granted {
                    //self.registerCategory()
                    //self.scheduleNotification("test", interval: 3)
                    //self.scheduleNotification("test2", interval: 5)
                    let types : UIUserNotificationType =  [.Badge, .Sound, .Alert]
                    let mySettings : UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes: types, categories: nil)
                    UIApplication.sharedApplication().registerUserNotificationSettings(mySettings)
                    UIApplication.sharedApplication().registerForRemoteNotifications()

                }
            }
        }
        else {

            // Fallback on earlier versions
            let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)
            UIApplication.sharedApplication().registerUserNotificationSettings(settings)

            // This is an asynchronous method to retrieve a Device Token
            // Callbacks are in AppDelegate.swift
            // Success = didRegisterForRemoteNotificationsWithDeviceToken
            // Fail = didFailToRegisterForRemoteNotificationsWithError
            UIApplication.sharedApplication().registerForRemoteNotifications()
        }
    }

    @available(iOS 10.0, *)
    func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
        print("willPresent")
        completionHandler([.Badge, .Alert, .Sound])
    }

    @available(iOS 10.0, *)
    func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
        self.didReceiveBookingAppRemoteNotification(response.notification.request.content.userInfo)
        print("didReceive == >> \(response.notification.request.content.userInfo)")
        completionHandler()
    }


//for Lower to ios 10 version
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        self.getNotifInfo(userInfo)
    } 

Make sure you have to enable push notification from project's Target => Capabilities and you have to add it's framework UserNotifications.framework

Nand Parikh
  • 205
  • 1
  • 2
  • 9