Swift - 本地通知没有被触发

Swift - Local Notification doesn't get triggered

我正在 Swift 3 中编码,我只是想 现在 发送通知,没有任何延迟或间隔。但是,通知永远不会被触发。这是我的代码..

ViewController代码

import UserNotifications

class HomeViewController: UIViewController{
    var isGrantedNotificationAccess:Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()

        UNUserNotificationCenter.current().requestAuthorization(
            options: [.alert,.sound,.badge],
            completionHandler: { (granted,error) in
                self.isGrantedNotificationAccess = granted
        })

        if isGrantedNotificationAccess{
            triggerNotification()
        }
    }

    //triggerNotification func goes here
}

触发通知函数:

func triggerNotification(){
    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey: "Notification Testing", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: "This is a test", arguments: nil)
    content.sound = UNNotificationSound.default()
    content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
    let trigger = UNTimeIntervalNotificationTrigger(
        timeInterval: 1.0,
        repeats: false)

    let request = UNNotificationRequest.init(identifier: "testTriggerNotif", content: content, trigger: trigger)

    let center = UNUserNotificationCenter.current()
    center.add(request)
}

我做错了什么?

UNUserNotificationCenter.current().requestAuthorization 的完成处理程序异步工作(该方法在不同的线程中处理)。这就是为什么在 completionHandler 完成之前读取代码中的 "if isGrantedNotificationAccess.." 语句的原因。

有一些方法可以解决这个问题。一种是使用NotificationCenter将requestAuthorization方法结束通知给HomeViewController(见下例)

import UserNotifications

class HomeViewController: UIViewController{

let notification = Notification.Name("requestAuthorizationComplete")

override func viewDidLoad() {
    super.viewDidLoad()

    // Register self as Observer to receive notification
    NotificationCenter.default.addObserver(self, selector: #selector(self.triggerNotification), name: notification, object: nil)

    UNUserNotificationCenter.current().requestAuthorization(
        options: [.alert,.sound,.badge],
        completionHandler: { (granted,error) in
        NotificationCenter.default.post(name: self.notification, object: nil)
    })

}

func triggerNotification(notification:NSNotification?){
 // this function will be called once requestAuthorization is complete
}

你错过了处理,当应用程序在前台时,你没有指定通知的外观或呈现方式。

在添加通知时设置下面的行以指定您希望在用户使用应用程序时显示横幅(iOS 10 项新功能)。

在构建 UNMutableNotificationContent 对象时添加以下代码行:

content.setValue("YES", forKeyPath: "shouldAlwaysAlertWhileAppIsForeground")

您还应该在 AppDelegate 中添加以下方法:

// This method will be called when app received push notifications in foreground

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