2017-04-20 73 views
1

我想从一个本地通知在iOS的行动10.启动应用程序10

我按照Launch a local notification at a specific time in 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默认操作获得(“默认”打印在t他在启动后控制台)传递参数并在ViewController中执行txtFromNotifier函数。试图在应用程序处于后台时使用NotificationCenter post/addObserver组合,但在应用程序处于非活动状态时并未到达那里。

任何想法?

+0

我明白了,但即使是我自己的问题,我也无法回答。 – Mick

+0

我有类似的问题。在接收视图控制器中调用addObserver之前,您的解决方案是否与通知获得广播有关? –

+0

@IanLeatherbury是的。 IIRC在ViewController中加载观察者之前,用户通知发生在AppDelegate中。我发现了一些想法,但是我很久没有忘记它在哪里。基本上,我在延迟中将通知帖子封装在AppDelegate中。 – Mick

回答

0

我找到了解决方案。我在通知广播中包裹了一段延迟。我的AppDelegate现在看起来像这样。对于选择在didFinishLaunchingWithOptions通知中心:

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 
} 

在didReceiveNotification我选择了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 
     } */ 
    } 
} 

,并使用@available选项来选择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") 
     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方法相同。