iOS 10 UNNotificationAction,可以从后台模式切换到前台模式吗?

标签 ios swift push-notification ios10

UINotificationAction 定义如下

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

单击时,将发出 Web 服务请求以使用 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()
}

但是应用程序已登录,并且当身份验证 token 过期时,Web 服务请求失败。

根据要求,在这种失败情况下,应该显示登录屏幕。

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

最佳答案

你的问题与你想要的不同..为了有这样的.. 1 种解决方案是在 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)
}

关于iOS 10 UNNotificationAction,可以从后台模式切换到前台模式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42819235/

相关文章:

ios - 是否每个具有 'Edit' 或 'Done' 按钮的 View 都​​需要位于 UINavigationController 内?

ios - UIAlertController 和 UIViewAlertForUnsatisfiableConstraints 错误

ios - ViewController 中的 SQLite 错误 14

android - 在安卓上推送通知

ios - 如何检查什么杀死了我的音频放大器应用程序? (ios, objective-c )

ios - 用于 UIBarButtonSystemItemAdd 和其他的 minic iOS 7 UIBarButton 字体

ios - 当应用程序直接在设备中运行并创建 ipa 文件时,为什么设备 token 生成不同?

ios - 当内容不适合时 UIView 多行 [iOS]

push-notification - notification.permission 设置为拒绝且不存在于阻止中

android - 如何做 SNS 推送通知? CreatePlatformEndpointResult 返回 null