2017-04-20 1 views
1

私はLaunch a local notification at a specific time in iOS従っている10.起動アプリ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) 

とAppDelegateで、今のViewController

override func viewDidLoad() { 
    super.viewDidLoad() 

    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.txtFromNotifier), name: NSNotification.Name(rawValue: "noteName", object: nil) 

とiOS 10内でそれをキャッチオブザーバーでセットアップを持っていた:

@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 Defaultアクションから取得する方法を見つけることができませんでした( "Default"はt彼は起動後にコンソールを起動する)、パラメータを渡して、ViewControllerでtxtFromNotifier関数を実行します。 NotificationCenter post/addObserverの組み合わせを使用しようとすると、アプリがバックグラウンドにあるときに機能しますが、アプリが非アクティブになってもそこには入りません。

アイデア?

+0

私はそれを理解しましたが、私自身の質問にも答えることができません。 – Mick

+0

私は同様の問題を抱えています。あなたのソリューションは、受信ビューコントローラでaddObserverが呼び出される前に通知をブロードキャストすることと関係がありましたか? –

+0

@IanLeatherburyはい、そうでした。 IIRC ViewControllerにオブザーバがロードされる前にAppDelegateでユーザー通知が発生していました。私はいくつかのアイデアを見つけましたが、それはずっとずっとずっとどこを忘れています。基本的に、私はAppDelegateの通知ポストを遅れてラップしました。 – Mick

答えて

0

解決策が見つかりました。私は通知のブロードキャストの周りに遅延をラップしました。私のAppDelegateはこのようになりました。 didFinishLaunchingWithOptionsで通知センターを選択する:

didReceiveNotificationで
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    if #available(iOS 10.0, *) { 
     let center = UNUserNotificationCenter.current() 
     center.delegate = self 
     let options: UNAuthorizationOptions = [.alert, .badge, .sound] 
     center.requestAuthorization(options: options) { 
      (granted, error) in 
      if !granted { 
       print("Something went wrong") 
      } 
     } 
    } else { 
     application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert , .badge , .sound], categories: nil)) 
    } 
    return true 
} 

私はiOSの10 ...

func application(_ application: UIApplication, didReceive notification: UILocalNotification) { 
    if #available(iOS 10.0, *) { 
     // different notification centre for iOS 10, see @available below 
    } else { 
     if (application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background) { 
      runAfterDelay(2.0) { // 2 second delay, not sure if needed but doesn't seem to hurt - runAfterDelay function is below 
       NotificationCenter.default.post(name: Notification.Name(rawValue: NSLocalizedString("ticker_notification_name", comment: "")), object: notification.alertBody) 
      } 
     } /*else { 
     // handle the local notification when the app is open, if needed 
     } */ 
    } 
} 

を選択して、iOSの10を選択するために@availableオプションを使用します。

@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") 
     runAfterDelay(3.0) { // 3 second delay to give observer time to load. 3 seconds is arbitrary. runAfterDelay function is below 
      NotificationCenter.default.post(name: Notification.Name(rawValue: NSLocalizedString("ticker_notification_name", comment: "")), object: response) // .body 
     } 
    case "Snooze": 
     print("Snooze") 
    case "Delete": 
     print("Delete") 
    default: 
     print("Unknown action") 
    } 
    completionHandler() 
} 

観察者が靴下を置く時間を与えるrunAfterDelay関数:

func runAfterDelay(_ delay: Double, closure:@escaping()->()) { 
    let when = DispatchTime.now() + delay 
    DispatchQueue.main.asyncAfter(deadline: when, execute: closure) 
} 

私は、オブザーバーに何も変更を加える必要はなかったと思います。自分の改訂履歴に変更は見られません。元の質問のaddObserverメソッドと同じように見えます。

関連する問題