If you diff SDK 'iOS 10.0'(Xcode 8) and SDK 'iOS 9.0' with this command below, you will find six UIKit classes related to notifications are deprecated in SDK 'iOS 10.0'(Xcode 8) .
Old api also works fine with SDK 'iOS 10.0'(Xcode 8) , but we had better use the APIs in the User Notifications framework instead.
In addation to these classes, the handleActionWithIdentifier:forLocalNotification:, handleActionWithIdentifier:forRemoteNotification:, didReceiveLocalNotification:withCompletion:, and didReceiveRemoteNotification:withCompletion: WatchKit methods. Use handleActionWithIdentifier:forNotification: and didReceiveNotification:withCompletion: instead.
Also the notification-handling methods in WKExtensionDelegate, such as didReceiveRemoteNotification: and handleActionWithIdentifier:forRemoteNotification:. Instead of using these methods, first create a delegate object that adopts the UNUserNotificationCenterDelegate protocol and implement the appropriate methods. Then assign the delegate object to the delegate property of the singleton UNUserNotificationCenter object.
SDK 'iOS 10.0'(Xcode 8) introduces the User Notifications framework (UserNotifications.framework), independent from UIKit, which supports the delivery and handling of local and remote notifications. so it's' both a new and old framework. You use the classes of this framework to schedule the delivery of local notifications based on specific conditions, such as time or location. Apps and app extensions can use this framework to receive and potentially modify local and remote notifications when they are delivered to the user’s device.
Also introduced in SDK 'iOS 10.0'(Xcode 8) , the User Notifications UI framework (UserNotificationsUI.framework) lets you customize the appearance of local and remote notifications when they appear on the user’s device. You use this framework to define an app extension that receives the notification data and provides the corresponding visual representation. Your extension can also respond to custom actions associated with those notifications.
I'll introduce the User Notifications framework in two parts:
Local Notification
Remote Notification
LocalNotification : write everything in one place
Someone may have the same question with this guy:
It is impossible for the first question, but local notification may be the best way to help you In terms of waking the app at a certain time, even a certain place. Because LocalNotification is just for scheduling the delivery of local notifications based on specific conditions, such as time or location.
LocalNotification has a limit, you can't trigger a block of code to run when the notification is fired. You can, however, trigger a block of code to execute with a UNNotificationAction by adding an action to your notification and using userNotificationCenter(_:didReceive:withCompletionHandler:) on UNUserNotificationCenter.currentNotificationCenter(). That is to say, it's impossiable to run a snippet in background at a certain time, without notifiying user. This feature is limited byond iOS8.
schedule the delivery of local notifications based on time
Big Diff:
Now you can either present alert, sound or increase badge while the app is in foreground too with SDK 'iOS 10.0'(Xcode 8)
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 specifical local notification just by one row code.
/// Notification become 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;
content.categoryIdentifier = "com.elonchan.localNotification"
// Deliver the notification in five 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"])
}
With Objective-C implemation:
import UserNotifications
/// Notification become independent from UIKit
@import UserNotifications;
Check dependencies
Signing for Your-Prject-Name requires a development team. Select a development team in the Target Editor.
Warning: The Copy Bundle Resources build phase contains this target's Info.plist file '/Users//Info.plist'.
Code signing is required for product type 'Application' in SDK 'iOS 10.0'
Before you access privacy-sensitive data like Camera, Contacts, and so on, you must ask for the authorization, your app will crash when you access them.Then Xcode will log like:
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSContactsUsageDescription key with a string value explaining to the user how the app uses this data.
How to deal with this?
As apple say:
You must statically declare your app’s intended use of protected data classes by including the appropriate purpose string keys in your Info.plist file.
Open the file in your project named info.plist, right click it, opening as Source Code, paste this code below to it. Or you can open info.plist as Property List by default, click the add button, Xcode will give you the suggest completions while typing Privacy - with the help of keyboard ⬆️ and ⬇️.
The list of frameworks that count as private data is a long one:
Contacts, Calendar, Reminders, Photos, Bluetooth Sharing, Microphone, Camera, Location, Health, HomeKit, Media Library, Motion, CallKit, Speech Recognition, SiriKit, TV Provider.
Remember to write your description why you ask for this authorization, between <string> and </string>, or your app will be rejected by apple:
请发表评论