发送本地通知

Sending Local Notification

我添加了代码以在单击标注附件视图的警报操作时发送推送通知。

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if view.rightCalloutAccessoryView == control {
        //right accessory

        let notificationPublisher = NotificationPublisher()

        let sendNotification = { (action:UIAlertAction!) -> Void in
            print("Action handled")
            notificationPublisher.sendNotification(title:"Hey",subtitle:"We made a",body:"notification",delayInterval: nil)
        }

        print("Button Press")
        let alertController = UIAlertController(title: "", message: "This will start alerts", preferredStyle: .alert)

        alertController.addAction(UIAlertAction(title: "Yes", style: .default, handler: sendNotification))
        alertController.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))

        self.present(alertController, animated: true, completion: nil)
    } else {
        // left Accessory
    }
}

"Yes" UIAlertAction 使用 sendNotification 作为其处理程序。这应该发送本地通知。它成功打印 "Action Handled",但不发送通知。

这是 NotificationsPublisher.swift 文件

import Foundation
import UserNotifications

class NotificationPublisher: NSObject{

func sendNotification(title: String,
                      subtitle: String,
                      body: String,
                      delayInterval: Int?){
    let notificationContent = UNMutableNotificationContent()
    notificationContent.title = title
    notificationContent.subtitle = subtitle
    notificationContent.body = body

    var delayTimeTrigger: UNTimeIntervalNotificationTrigger?

    if let delayInterval = delayInterval {
        delayTimeTrigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(delayInterval), repeats: false)
    }

    notificationContent.sound = UNNotificationSound.default

    UNUserNotificationCenter.current().delegate = self

    let request = UNNotificationRequest(identifier: "TestLocalNotification", content: notificationContent, trigger: delayTimeTrigger)

    UNUserNotificationCenter.current().add(request){error in
        if let error = error {
            print(error.localizedDescription)
        }
    }
}

}

extension NotificationPublisher: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("The notification is about to be presented")
    completionHandler([.badge,.sound,.alert])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    let identifier = response.actionIdentifier

    switch identifier {
    case UNNotificationDismissActionIdentifier:
        print("The notification was dismissed")
        completionHandler()
    case UNNotificationDefaultActionIdentifier:
        print("The user opened the app from the notification")
    default:
        print("The default case was called")
        completionHandler()
    }
}

}

在 mapView 函数中使用 let notificationPublisher = NotificationPublisher() 会导致问题。我通过将 private let notificationPublisher = NotificationPublisher() 置于 viewDidLoad() 之上,使其成为 ViewController 的 属性。然后,在 mapView 的 sendNotification 函数中,将其引用为

self.notificationPublisher.sendNotification(title:"Title",subtitle:"Subtitle",body:"notification",delayInterval: nil)