这可能是一个预先提出的问题,但我想知道UILocalNotification
在iOS 10中使用什么而不是使用UILocalNotification
什么。
是的,您可以使用UILocalNotification
,旧的API也可以在iOS 10上正常工作,但是我们最好在User Notifications框架中使用这些API。还有一些新功能,您只能与iOS 10 User Notifications框架一起使用。
有关更多信息,“远程通知”也会发生这种情况:此处。
新的功能:
对于我们来说,将UILocalNotification
API转换为iOS 10用户通知框架API真的很容易,它们确实很相似。
我在此处编写了一个演示,以展示如何同时使用新旧API:iOS 10适应技巧。
例如,
使用Swift实现:
导入用户通知
/// Notification become independent from UIKit
import UserNotifications
请求本地通知的授权
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"])
}
目标C的实现:
导入用户通知
// Notifications are independent from UIKit
#import <UserNotifications/UserNotifications.h>
请求本地通知的授权
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!");
}
}];
由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“如果重复,则时间间隔必须至少为60”
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60, repeats: true)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句