Push Notifications in iOS 10 [Swift]

Push Notification iOS 10 Swift
Push Notification iOS 10 Swift

The new framework called “UserNotifications” is introduced with iOS 10 SDK. The UserNotifications framework (UserNotifications.framework) supports the delivery and handling of local and remote notifications.

So, Let see what we have to change to get the push notifications in iOS 10.

Steps for implement code to handle push notifications in iOS 10

  1. Import UserNotifications.framework in your AppDelegate file
  2. import UserNotifications
    

    Also add UNUserNotificationCenterDelegate.

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    }
    

  3. Register for push notification
  4. Before registration check the version of iOS and then based on versions do the code. For iOS 7 code was different, fro iOS 8 & 9 code was different and again for iOS 10 code is different.

    As per my opinion you have to set the deployment target to iOS 8 or iOS 9 and later. For this you can check the adoption ratio of iOS in the devices.

    Add code in your did finish launching

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        registerForRemoteNotification()        
        return true
    }
    
    func registerForRemoteNotification() {
        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()
                }
            }
        }
        else {
            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
    

  5. Handling delegate methods for UserNotifications
  6. You will be surprise that notification displayed when application in foreground too in iOS 10. As we know that in old versions we display alert or something else which will be look like notification comes in foreground.

    There are two delegate methods need to be handled :

    //Called when a notification is delivered to a foreground app.
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("User Info = ",notification.request.content.userInfo)
        completionHandler([.alert, .badge, .sound])
    }
    
    //Called to let your app know which action was selected by the user for a given notification.    
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("User Info = ",response.notification.request.content.userInfo)
        completionHandler()
    }
    

  7. Add Push Notifications Entitlements
  8. Go to your project target’s Capabilities tab and add Push Notifications Entitlements.

    Push Notifications Entitlements
    Push Notifications Entitlements

    If it’s available in your certificates then it will enable directly else configure your profile with the certificates and you can enable this capability by that.

All Done!

Happy Coding 🙂

I have added sample code of swift on github with iOS 8, 9 and 10 support of push notification.
I have also posted about Push Notifications in iOS 10 [Objective-C].

  • Chris Gonzalez

    Hi, how can i capture data from push when app is un background or closed?

    • Ashish Kakkad

      You can do it when user click on notification. Because if app is not active so we can’t do anything!

  • Miguel Ruiz Bas

    where is the token to send push to it (device)?

    • Ashish Kakkad

      Check the tutorial available at github.

  • Gabriel Cuesta Arza

    How can I get the Push Token of the mobile when the user accepts to get notifications?

    • Ashish Kakkad

      Check the tutorial available at github. Link available in blog.

  • Nitesh

    Hey guys, i’m new to Push notification. I have script in NodeJS which send push notification to user when someone comment’s on his profile. But push notification is sent based on the device token so for example when A user logs in with his iPhone and then he log out and logs with account B from same iPhone then there device token will be same. So if someone comments on A’s profile then that notification is sent on B’s device which is currently logged in on the same iPhone. How to handle this type of situation ?

    • Ashish Kakkad

      You have to set blank text in database when user logout.

  • Ganesh Kumar

    @ashish_kakkad:disqus i have implemented firebase push in ios 10. when i send push from firebase, i can receive the push message only when the app is in foreground mode. i dont receive any message when the app is in background mode or it is force stopped