我正在尝试使用 iOS 10 中本地通知的操作来实现应用程序启动(从非活动状态)。
我已经按照在 iOS 中的特定时间启动本地通知,并且该应用程序可以正常启动以响应本地通知。但是我想要从这里执行一个操作以响应通知中的数据。
在 iOS 8 和 9 中,我在 AppDelegate 中进行了设置
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
if (application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background) {
NotificationCenter.default.post(name: Notification.Name(rawValue: "noteName", object: notification.alertBody)
并且观察者在 ViewController 中捕捉到它
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.txtFromNotifier), name: NSNotification.Name(rawValue: "noteName", object: nil)
现在在 iOS 10 中的 AppDelegate 中:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// Determine the user action
switch response.actionIdentifier {
case UNNotificationDismissActionIdentifier:
print("Dismiss Action")
case UNNotificationDefaultActionIdentifier:
print("Default")
// do something here
我无法找到如何从 UNNotification 默认操作(启动后在控制台中打印“默认”)传递参数并在 ViewController 中执行 txtFromNotifier 函数。尝试使用 NotificationCenter post / addObserver 组合在应用程序处于后台时有效,但在应用程序处于非活动状态时无法到达那里。
有任何想法吗?