为什么iOS 10中的前台未触发我的本地通知?

时间:2017-04-05 03:47:27

标签: ios swift swift3 unusernotificationcenter

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

这是我在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 个答案:

答案 0 :(得分:10)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    //set gina on 

    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) {
    // EZAlertController.alert("you have new meeting ")

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

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

答案 1 :(得分:4)

参考以下答案: UserNotification is not showing (iOS 10)

我的本​​地通知未显示在前台的原因不是因为我" 永远不会在前台看到通知"如上一个答案所述,但因为我需要设置至少body个内容,而我没有在我的代码中设置它。

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

答案 2 :(得分:1)

对我来说,我忘了像这样设置代表:

UNUserNotificationCenter.current().delegate = self

答案 3 :(得分:0)

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