我正在尝试在我正在构建的应用上实施本地通知。我的大部分代码都遵循In the documentation
所写的内容我将在下面发布我的代码。我目前的问题是通知永远不会出现。我第一次加载应用程序时出现了权限屏幕,我说"允许"
在didFinishLaunchingWithOptions方法的AppDelegate中
didFinishLaunchingWithOptions
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
[self setupNotification];
}];
以下内容也适用于AppDelegate
-(void)setupNotification {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"New:" arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"New Notification"
arguments:nil];
content.sound = [UNNotificationSound defaultSound];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5
repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"NOTIFICATION"
content:content
trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"success");
}
}];
}
正如我之前所说,通知永远不会出现,我无法弄清楚原因。我设置了内容,触发器和请求,然后将请求添加到UNUserNotificationCenter。
有没有人有这方面的工作示例或者可以告诉我哪里出错了?
我找到了类似的答案here,但这个答案并没有解决UNTimeIntervalNotificationTrigger
无效的原因,而是解释了如何设置UNCalendarNotificationTrigger
由于
答案 0 :(得分:-1)
UNUserNotificationCenter Swift 3实施和使用:
// AppDelegate.swift
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
// Override point for customization after application launch.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Setup Notifications
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (bool, error) in
}
return true
}
创建“Extension.swift”文件,并添加:
// Extensions.swift
import UIKit
import UserNotifications
extension UNUserNotificationCenter {
func scheduleNotification(identifier: String, body: String, time: Double) {
let content = UNMutableNotificationContent()
content.body = body
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: false)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if (error != nil) {
print(error!)
} else {
print("Success! ID:\(identifier), Message:\(body), Time:\(trigger.timeInterval.magnitude) seconds")
}
}
}
}
在任何视图控制器中,在“class”(在viewDidLoad之上)声明:
var notificationCenter: UNUserNotificationCenter?
在“viewDidLoad”功能中,初始化UNUserNotificationCenter:
notificationCenter = UNUserNotificationCenter.current()
从视图控制器中的任何功能调用通知:
// Use any identifier, Message and time (in seconds)
self.notificationCenter?.scheduleNotification(identifier: "welcomeScreen", body: "Welcome to my App!", time:20)
这应该在Swift 3和iOS 10中启用本地通知。