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 の組み合わせを使用しようとすると、アプリがバックグラウンドにある場合は機能しますが、アプリが非アクティブな場合は機能しません。
何か案は?