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

问题描述 投票:0回答:1

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

[在这里,在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)
}
swift push-notification apple-push-notifications appdelegate
1个回答
0
投票

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

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
           }

        } 
}
© www.soinside.com 2019 - 2024. All rights reserved.