3

我试图理解为什么本地通知没有显示在前台。我相信我添加了所有正确的代码以实现此功能,但是当我的应用程序处于前台时,没有显示横幅。

这是我添加的内容AppDelegate.swift

import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
        UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound, UNAuthorizationOptions.badge]) { (willAllow: Bool, error: Error?) in

            if willAllow == true
            {
                // Do something
            }

        }

        UNUserNotificationCenter.current().delegate = self

        // Override point for customization after application launch.
        return true
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        print("GO IN HERE")
        completionHandler(.alert)
    }

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

    }

}

我的ViewController.swift

override func viewDidLoad()
{
    super.viewDidLoad()

    let content: UNMutableNotificationContent = UNMutableNotificationContent()
    content.sound = UNNotificationSound.default()
    content.subtitle = "This is a test"

    let trigger: UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

    let request: UNNotificationRequest = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

当我的应用程序第一次启动时,我确实看到了允许我的应用程序推送通知的权限警报,但我根本看不到任何通知。

我确实看到了日志输出GO IN HERE,所以willPresent被调用了。

还有什么我想念的吗?

4

4 回答 4

18
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    
    let center = UNUserNotificationCenter.current()
    center.delegate = self
    
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in
        if !accepted {
            print("Notification access denied")
        }
    }
}

在 AppDelegate 中添加:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    
    completionHandler( [.alert, .badge, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    completionHandler()
}
于 2017-08-09T16:19:36.587 回答
3

参考以下答案: 用户通知未显示(iOS 10)

我的本地通知没有显示在前台的原因不是因为我“永远不会在前台看到通知”,而是因为我至少需要设置body内容,而我没有在我的代码中设置。

通过设置content.body = "Some message",我终于可以在我的应用程序运行时在前台看到通知。

于 2017-04-10T17:06:48.120 回答
2

For me it was that I forgot to set the delegate like so:

UNUserNotificationCenter.current().delegate = self
于 2018-06-21T06:57:48.303 回答
-3

您永远不会在前台看到通知,这就是它们的工作方式。如果您想强制它们显示,您可以创建自己的 UIView 并显示它或使用现有库https://github.com/pikacode/EBForeNotification

于 2017-04-05T04:22:33.790 回答