问题描述
这可能是一个问题,但我想知道在iOS10中使用什么代替 UILocalNotification
。我目前正在开发一个部署目标为iOS8的应用程序,所以可以使用 UILocalNotification
?
It may be a question in advance but I wonder what to use instead of UILocalNotification
in iOS10. I am currently working on an app which has deployment target iOS8 so will it be ok to use UILocalNotification
?
推荐答案
是的,您可以使用 UILocalNotification
,旧的API也适用于iOS10,但我们最好使用User Notifications框架中的API代替。还有一些新功能,您只能使用iOS10用户通知框架。
Yes, you can use UILocalNotification
, old APIs also works fine with iOS10, but we had better use the APIs in the User Notifications framework instead. There are also some new features, you can only use with iOS10 User Notifications framework.
这也适用于远程通知,有关详细信息:。
This also happens to Remote Notification, for more information: Here.
新功能:
- 现在,你可以使用iOS 10在应用程序处于前台时显示警报,声音或增加徽章
- 现在你可以在一个地方处理所有事件当用户点击(或滑动)动作按钮时,即使应用程序已经被杀死。
- 支持3D触摸而不是滑动手势。
- 现在,您只需按行代码删除特定的本地通知。
- 支持使用自定义UI的Rich Notification。
- 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 specifical local notification just by one row code.
- Support Rich Notification with custom UI.
我们很容易将 UILocalNotification
API转换为iOS10
用户通知框架API,它们非常相似。
It is really easy for us to convert UILocalNotification
APIs to iOS10User Notifications framework APIs, they are really similar.
我在这里写了一个Demo来展示如何同时使用新旧API:。
I write a Demo here to show how to use new and old APIs at the same time: iOS10AdaptationTips .
例如,
使用Swift实现:
-
import UserNotifications
import UserNotifications
/// Notification become independent from UIKit
import UserNotifications
请求localNotification的授权
request authorization for localNotification
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
安排localNotification
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"])
}
Objective-C实施:
Objective-C implementation:
-
import UserNotifications
import UserNotifications
// Notifications are independent from UIKit
#import <UserNotifications/UserNotifications.h>
请求localNotification授权
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];
}
}];
安排localNotification
schedule localNotification
更新应用程序图标徽章编号
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!");
}
}];
返回此处获取更多信息:。
Go to here for more information: iOS10AdaptationTips.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)
这篇关于UILocalNotification在iOS10中已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!