Local and Push Notifications in IOS version compatible

IOS 12 :- Group notification

set threadIdentifier UNMutableNotificationContent to create group notification

create local notification group

let content = UNMutableNotificationContent()
content.title = "Group Notifications"
content.body = "Body of notification"
content.threadIdentifier = "group-identifire"

create remote notification group need to pass thread-id in payload

{
 "aps" : {
     "alert" : {
         "title" : "Group Notifications",
         "body" : "Body of notification"
     }
     "thread-id" : "group-identifire"
 }
}

IOS 11 :- You can also use following code for iOS 11. No any kind of changes requires in push and local notification

Creating A Notification Request

import UserNotifications

if #available(iOS 10.0, *) {
    //iOS 10.0 and greater
    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { granted, error in
        DispatchQueue.main.async {
            if granted {
                UIApplication.shared.registerForRemoteNotifications()
            }
            else {
                //Do stuff if unsuccessful...
            }
        }
   })
}
else { 
    //iOS 9
    let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
    let setting = UIUserNotificationSettings(types: type, categories: nil)
    UIApplication.shared.registerUserNotificationSettings(setting)
    UIApplication.shared.registerForRemoteNotifications()
}

Schedule Local Notification

if #available(iOS 10.0, *) {
    //iOS 10 or above version
    let center = UNUserNotificationCenter.current()
    let content = UNMutableNotificationContent()
    content.title = "Late wake up call"
    content.body = "The early bird catches the worm, but the second mouse gets the cheese."
    content.categoryIdentifier = "alarm"
    content.userInfo = ["customData": "fizzbuzz"]
    content.sound = UNNotificationSound.default()

    var dateComponents = DateComponents()
    dateComponents.hour = 15
    dateComponents.minute = 49
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
    center.add(request)
} else {
    // ios 9 
    let notification = UILocalNotification()
    notification.fireDate = NSDate(timeIntervalSinceNow: 5) as Date
    notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
    notification.alertAction = "be awesome!"
    notification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.shared.scheduleLocalNotification(notification)
}

UIApplicationDelegate

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenParts = deviceToken.map { data -> String in
        return String(format: "%02.2hhx", data)
    }
    let token = tokenParts.joined()
    print(token)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

}

UNUserNotificationCenterDelegate

Only available in ios 10 and above version

The method will be called on the delegate only if the application is in the foreground

You can present default banner with helping of following method

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.badge,.alert,.sound])
}

The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction

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

}

Leave a Comment