UserNotifications Framework - Push Notification In iOS 10 Swift 3.0

Apple introduced new framework UserNotifications.framework for delivery and handling of local and remote notification. The UserNotifications framework supported iOS 10.0 + , tvOS 10.0 + , and watchOS 3.0+.

We are discussing only about the code for notification. Here the code for both iOS 10+ and also below of iOS version in Swift 3.0.

Step 1 : Import UserNotifications framework in your AppDelegate file for iOS 10.
import UserNotifications
Step 2 : Add UNUserNotificationCenterDelegate in AppDelegate.
class AppDelegate: UIResponder, UIApplicationDelegate , UNUserNotificationCenterDelegate{
Step 3 : To register local or remote notification, Add the code in didFinishLaunch delegate method.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
    [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [UNAuthorizationOptions.sound ], completionHandler: { (granted, error) in
            if error == nil{
                UIApplication.shared.registerForRemoteNotifications()
            }
        })
    } else {
        let settings  = UIUserNotificationSettings(types: [UIUserNotificationType.alert , UIUserNotificationType.badge , UIUserNotificationType.sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }
    return true
}
Step 4: Now the Local and Remote notification response is Handle by its delegate method. When You are working iOS 10 or later, then notification response handle in UNUserNotificationDelegate methods.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("User Info === \(notification.request.content.userInfo)")
    // Handle code here.
    completionHandler([UNNotificationPresentationOptions.sound , UNNotificationPresentationOptions.alert , UNNotificationPresentationOptions.badge])
}

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("User Info === \(response.notification.request.content.userInfo)")
    completionHandler()
}
Step 5: Below iOS 10, Local and Remote notification response is Handle by its Application delegate method. 
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let characterSet = CharacterSet(charactersIn: "<>")
    let deviceTokenString: String = ((deviceToken.description as NSString).trimmingCharacters(in: characterSet) as NSString).replacingOccurrences(of: " ", with: "") as String
    print(deviceTokenString)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    print(userInfo)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print(error.localizedDescription)
}


Any issue related to this post comment. and also comment to improve the post.

Thanks. 
UserNotifications Framework - Push Notification In iOS 10 Swift 3.0 UserNotifications Framework - Push Notification In iOS 10 Swift 3.0 Reviewed by Kirit Modi on 23:35:00 Rating: 5

No comments:

Powered by Blogger.