What's new in UILocalNotification with iOS 10
suggest changeYou can use UILocalNotification (deprecated in iOS 10). The old APIs also work on iOS 10, but it is better to use the User Notifications framework instead. There are also some new features you can use only with the iOS 10 User Notifications framework.
This also happens to Remote Notification, for more information: Here.
New Features:
- Now you can present an alert, play a sound, or increase the badge while the app is in the foreground, too, with iOS 10
- Now you can handle every event in one place when the user taps (or slides) the action button, even while the app has already been killed.
- Support 3D Touch instead of a sliding gesture.
- Now you can remove a specific local notification with one line of code.
- Support Rich Notifications with a custom UI.
It is really easy to convert UILocalNotification APIs to iOS 10 User Notifications framework APIs; they are really similar.
I wrote a demo here to show how to use the new and old APIs at the same time: iOS10AdaptationTips .
For example,
With Swift implementation:
- import UserNotifications
/// Notifications are independent from UIKit
import UserNotifications
- request authorization for localNotification
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
- schedule localNotification
- update application icon badge number
@IBAction func triggerNotification(){
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Elon said:", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Hello Tom!Get up, let's play with Jerry!", arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = (UIApplication.shared().applicationIconBadgeNumber + 1) as NSNumber
content.categoryIdentifier = "com.elonchan.localNotification"
// Repeating interval must be at least 60 seconds. The identifier is still "FiveSecond".
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0, repeats: true)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)
}
@IBAction func stopNotification(_ sender: AnyObject) {
let center = UNUserNotificationCenter.current()
center.removeAllPendingNotificationRequests()
// or you can remove a specific notification:
// center.removePendingNotificationRequests(withIdentifiers: ["FiveSecond"])
}
These Swift samples use Swift 3 names (UIApplication.shared(), UNNotificationSound.default()).
Objective-C implementation:
- import UserNotifications
// Notifications are independent from UIKit
#import <UserNotifications/UserNotifications.h>
- request authorization for localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
[self showAlert];
}
}];
- schedule localNotification
- update application icon badge number
showAlert in the authorization handler above is not defined on this page.
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:"
arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!"
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
// 4. update application icon badge number
content.badge = [NSNumber numberWithInteger:([UIApplication sharedApplication].applicationIconBadgeNumber + 1)];
// Deliver the notification in five seconds.
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5.f
repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
content:content
trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"add NotificationRequest succeeded!");
}
}];
Go to here for more information: iOS10AdaptationTips.
updated
Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘time interval must be at least 60 if repeating’
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)