当应用程序在后台时,推送通知无法正常工作,Swift

Push notifications doesn't work properly, when app in background, Swift

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

在这里,在 AppDelegate 中,我使用推送,当应用程序关闭时:

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 中,我使用推送,当应用程序打开时:

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)
}

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

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
           }

        } 
}