在后台运行应用程序时,Swift

时间:2019-11-27 14:39:41

标签: swift push-notification apple-push-notifications appdelegate

我已在我的应用程序中添加了推送通知。它们可以在用户按下时打开特定的屏幕。当应用程序完全关闭时,以及在前台打开应用程序时,它们都可以正常工作。但是,当它处于背景中时,通过按推(而不是所需的推)来打开第一个屏幕。

在这里,在AppDelegate中,当应用关闭时,我使用push:

let pushManager = PushNotificationManager(userID: "UserID")
    pushManager.registerForPushNotifications()

    let notificationOption = launchOptions?[.remoteNotification]

    if let notification = notificationOption as? [String: AnyObject],
        let aps = notification["aps"] as? [String: AnyObject] {
        print(aps)
        let storyboard = UIStoryboard(name: "Invoice", bundle: nil)
        guard let returnPage = storyboard.instantiateViewController(withIdentifier:
            "\(InvoiceViewController.self)") as? InvoiceViewController else { return true }
        if let navBar = self.window?.rootViewController as? UINavigationController {
            navBar.pushViewController(returnPage, animated: true)
        }
    }

在这里,同样在AppDelegate中,当应用打开时,我使用push:

func application(
  _ application: UIApplication,
  didReceiveRemoteNotification userInfo: [AnyHashable: Any],
  fetchCompletionHandler completionHandler:
  @escaping (UIBackgroundFetchResult) -> Void
) {
  guard let aps = userInfo["aps"] as? [String: AnyObject] else {
    completionHandler(.failed)
    return
  }

    let storyboard = UIStoryboard(name: "Invoice", bundle: nil)
            guard let returnPage = storyboard.instantiateViewController(withIdentifier:
                "\(InvoiceViewController.self)") as? InvoiceViewController else { return }
    if let navBar = self.window?.rootViewController as? UINavigationController {
        navBar.pushViewController(returnPage, animated: true)
    }
  print(aps)
}

1 个答案:

答案 0 :(得分:1)

您应该使用以下代码来处理用户对推送通知的响应。

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

这是UNUserNotificationCenterDelegate的一部分,专门用于用户响应,因此您也不必担心应用程序状态。 希望对您有所帮助。

编辑:要使用它,请先在didFinishLaunching中注册该子

let replyAction = UNTextInputNotificationAction(identifier: identifier,
                                                        title: "Reply",
                                               options:.authenticationRequired, textInputButtonTitle: "Button", textInputPlaceholder: "")

let newMessageCategory = UNNotificationCategory(identifier: someIdentifier,
                                                        actions: [replyAction],
                                                        intentIdentifiers: [],
                                                        options: [])

        UNUserNotificationCenter.current().setNotificationCategories([newMessageCategory])
UNUserNotificationCenter.current().delegate = self

然后注册上述方法。您将在参数中获得如下信息:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping Func0) {
        let userInfo = response.notification.request.content.userInfo
        let actionIdentifier = response.actionIdentifier

        if actionIdentifier == UNNotificationDefaultActionIdentifier {
              // handle navigation here
           }

        } 
}