这可能是一个提前的问题,但我想知道用什么代替UILocalNotification
在 iOS 10 中。我正在开发一个具有部署目标 iOS 8 的应用程序,所以可以使用吗UILocalNotification
?
是的,您可以使用UILocalNotification
,旧的 API 在 iOS 10 上也可以正常工作,但我们最好使用用户通知框架中的 API。还有一些新功能,只能与 iOS 10 用户通知框架一起使用。
远程通知也会发生这种情况,有关更多信息:Here https://stackoverflow.com/a/40253296/3395008.
新功能:
- 现在,在 iOS 10 中,当应用程序位于前台时,您也可以显示警报、声音或增加徽章
- 现在,当用户点击(或滑动)操作按钮时,即使应用程序已被终止,您也可以在一处处理所有事件。
- 支持3D触摸而不是滑动手势。
- 现在,您只需一行代码即可删除特定的本地通知。
- 支持具有自定义 UI 的丰富通知。
我们很容易转换UILocalNotification
iOS 10 的 API
用户通知框架 API,它们非常相似。
我在这里写了一个demo来展示如何同时使用新旧API:iOS 10 适配技巧 https://github.com/ChenYilong/iOS10AdaptationTips .
例如,
使用 Swift 实现:
-
导入用户通知
/// Notification become independent from UIKit
import UserNotifications
-
请求 localNotification 的授权
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
-
安排本地通知
-
更新应用程序图标徽章编号
@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 实现:
-
导入用户通知
// Notifications are independent from UIKit
#import <UserNotifications/UserNotifications.h>
-
请求 localNotification 的授权
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
[self showAlert];
}
}];
-
安排本地通知
-
更新应用程序图标徽章编号
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
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“如果重复,时间间隔必须至少为 60”
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)