The Apple Push Notification Service (APNS) was released way back in iOS 3.0. That was only just over a year ago but given the pace of development in the mobile OS market that now seems like ancient history. APNS was a first tentative attempt by Apple to address the demand for background processes. It allows client applications to receive updates from a server without having to be running. Apple takes care of pushing the notification to the device and the user can then decide whether to launch the application or not.
Local Notifications
The big problem with APNS is that it requires you to run a server somewhere that interacts with APNS to push a notification to a device. For applications that already have a client-server architecture such as an IM client this is no big deal. There are also a number of third parties who can provide a cost efficient service for small scale users who do not want the trouble of running their own server. However for many applications this is still way too complicated.
The release of iOS 4 brought a number of new features aimed at adding multitasking support one of which was local notifications. I am not sure if you can really say that local notifications count towards multitasking but when compared to APNS they are much simpler to use. There is no central service in fact you do not even need an internet connection to schedule and deliver notifications to an application.
To provide a working example of how to add local notifications to an iOS app I have a created a very simple sample application. The user interface is very simple and will not win any design awards. There are two buttons . One for triggering and other for stopping the triggered notifications.
And don,t forget to edit AppDelegate method.
To provide a working example of how to add local notifications to an iOS app I have a created a very simple sample application. The user interface is very simple and will not win any design awards. There are two buttons . One for triggering and other for stopping the triggered notifications.
And don,t forget to edit AppDelegate method.
import UserNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
}
// Override point for customization after application launch.
return true
}
LocalNotification.m
import UIKit
import UserNotifications
import UserNotificationsUI
class ViewController: UIViewController {
let requestIdentifier = "prem"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func triggerButton(_ sender: Any) {
print("Notification will be triggered in 2 sec. Hold on...")
let content = UNMutableNotificationContent()
content.title = "Intro to Notifications"
content.subtitle = "Hello"
content.body = "I learnt local notification"
content.sound = UNNotificationSound.default()
//Delivering the notification in two seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 2, repeats: false)
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self as! UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
print(error?.localizedDescription as Any )
}
}
}
@IBAction func stopButton(_ sender: Any) {
print("Remove pending request.")
let centre = UNUserNotificationCenter.current()
centre.removePendingNotificationRequests(withIdentifiers: [requestIdentifier])
}
}
extension ViewController: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Tapped in notification")
}
//This is key callback to present notification while the app is in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Notification being triggered")
//You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
//to distinguish between notifications
if notification.request.identifier == requestIdentifier{
completionHandler( [.alert,.sound,.badge])
}
}
}
Thanks, hope you enjoyed coding...!!!
Thanks, hope you enjoyed coding...!!!
Comments
Post a Comment