Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILocalNotification is deprecated in iOS 10

It may be a question in advance but I wonder what to use instead of UILocalNotification in iOS 10. I am working on an app which has deployment target iOS 8 so will it be ok to use UILocalNotification?

like image 973
Roohul Avatar asked Jun 21 '16 08:06

Roohul


People also ask

How many local notifications can be scheduled iOS?

As you are already aware, you can schedule maximum of 64 notifications per app. If you add more than that, the system will keep the soonest firing 64 notifications and will discard the other.

What is local notification in iOS?

With local notifications, your app configures the notification details locally and passes those details to the system, which then handles the delivery of the notification when your app is not in the foreground. Local notifications are supported on iOS, tvOS, and watchOS.

What is UNMutableNotificationContent?

Overview. Create a UNMutableNotificationContent object when you want to specify the payload for a local notification. Specifically, use this object to specify the title and message for an alert, the sound to play, or the value to assign to your app's badge.


2 Answers

Yes, you can use UILocalNotification, old APIs also works fine with iOS 10, but we had better use the APIs in the User Notifications framework instead. There are also some new features, you can only use with iOS 10 User Notifications framework.

This also happens to Remote Notification, for more information: Here.

New Features:

  • Now you can either present alert, sound or increase badge while the app is in foreground too with iOS 10
  • Now you can handle all event in one place when user tapped (or slided) the action button, even while the app has already been killed.
  • Support 3D touch instead of sliding gesture.
  • Now you can remove specific local notifications with just one line of code.
  • Support Rich Notification with custom UI.

It is really easy for us 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 new and old APIs at the same time: iOS 10 Adaptation Tips .

For example,

With Swift implementation:

  1. import UserNotifications

    ///    Notification become independent from UIKit import UserNotifications 
  2. request authorization for localNotification

        let center = UNUserNotificationCenter.current()     center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in         // Enable or disable features based on authorization.     } 
  3. schedule localNotification

  4. 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;     content.categoryIdentifier = "com.elonchan.localNotification"     // Deliver the notification in 60 seconds.     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 specifical notification:     // center.removePendingNotificationRequests(withIdentifiers: ["FiveSecond"]) } 

Objective-C implementation:

  1. import UserNotifications

    // Notifications are independent from UIKit #import <UserNotifications/UserNotifications.h> 
  2. 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];                           }                       }]; 
  3. schedule localNotification

  4. update application icon badge number

    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!");     } }]; 

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) 
like image 184
ChenYilong Avatar answered Oct 03 '22 23:10

ChenYilong


Apple have done it again, the correct implementation is: AppDelegate.swift

if #available(iOS 10.0, *) {         let center = UNUserNotificationCenter.currentNotificationCenter()         center.requestAuthorizationWithOptions([.Alert, .Sound]) { (granted, error) in             // Enable or disable features based on authorization.         }     } else {         // Fallback on earlier versions     } 

and don't forget to add

import UserNotifications 
like image 27
Golan Shay Avatar answered Oct 04 '22 00:10

Golan Shay