有什么方法可以在 iOS Swift 5 的应用程序处于前台时获取推送通知?

Is there any way to get push notification while App in foreground on iOS Swift 5?

我想为 iOS 创建考勤管理应用程序。 该应用程序将有开始按钮,然后如果它点击该应用程序将有横幅通知来迎接你。

问题是当应用程序处于前台时我无法收到横幅通知。我将在下面显示我的代码。如果我想收到通知,我必须 return 主屏幕或 运行 其他应用程序。 在我的应用程序上,Set Notification 按钮启用了通知,Start 按钮用于设置通知。本想马上通知,但此时我要回家,所以我定了时间timeInterval: 5.

ContentView.swift

import SwiftUI
import Foundation
import UserNotifications

struct ContentView: View {
    var body: some View {
        VStack {
            Button(action: { self.setNotification() }) {
                Text("Set Notification")
            }.padding()

            //added here
            Button(action: { self.btnSend() }) {
                Text("Start")
            }.padding()
        }
    }

    func setNotification() -> Void {
     UNUserNotificationCenter.current().requestAuthorization(
                       options: [.alert,.sound,.badge], completionHandler: {didAllow,Error in
                       print(didAllow) //
                    })
    }

    func btnSend() {
        print("btnSend")

        let content = UNMutableNotificationContent()
        content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)
        content.body = NSString.localizedUserNotificationString(forKey: "Good morning hehe", arguments: nil)
        content.sound = UNNotificationSound.default

        // Deliver the notification in five seconds.
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest(identifier: "FiveSecond",
                                            content: content, trigger: trigger) // Schedule the notification.
        let center = UNUserNotificationCenter.current()
        center.add(request) { (error : Error?) in
             if let theError = error {
                 // Handle any errors
             }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

有人有好的解决方案吗?

如果应用程序在前台,通知横幅不会显示,您需要在 AppDelegate 中捕获通知,创建您自己的自定义视图并从那里处理它。

可能您只需要在 AppDelegate.

中添加一些代码
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    // add UNUserNotificationCenterDelegate
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions)
        -> Void) {
        completionHandler([.alert, .badge, .sound])
    } // add this function

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        UNUserNotificationCenter.current().delegate = self // add this
        return true
    }

    // ... omitted
}

使用

 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent   notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)   { 
   completionHandler([.alert, .sound])
}

前台通知捕获并显示警报、徽章和添加声音的方法。