iOS 10 UNNotificationAction,可以从后台移动到前台模式吗?

时间:2017-03-15 19:41:34

标签: ios swift push-notification ios10

UINotificationAction定义如下

let customAction = UNNotificationAction(identifier: "customCategory.Action", title: "Do something", options: [])

点击后,这将发出一个网络服务请求,使用UNUserNotificationCenterDelegate方法获取数据,如下所示

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    let actionIdentifier = response.actionIdentifier
    switch actionIdentifier {
    case customCategory.Action:
        if authTokenNotExpired {
            // Make service call
        } else {
            // Show login screen (In foreground)
        }

    }
    completionHandler()
}

但是应用程序已登录,当auth令牌过期时,Web服务请求失败。

根据要求,在此故障情形中,应显示登录屏幕。

是否可以将应用程序从后台模式移动到前台以显示登录屏幕?

1 个答案:

答案 0 :(得分:0)

您的问题与您要的问题不同。 一种解决方案是在appdelegate中这样做:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    NotificationCenter.default.addObserver(self, selector: #selector(triggerGetNewToken(note:)), name: .isNeedNewToken, object: nil)
  }

@objc func triggerGetNewToken(note:NSNotification){
    debugLog(logMessage: "GETTING NEW TOKENS NOW IN APPDELEGATE!")
    api_token.param(serverKey: appkey)
}