发送本地通知

时间:2019-02-23 21:56:39

标签: swift nsnotificationcenter

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

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
    }
}

“是” UIAlertAction使用sendNotification作为其处理程序。这应该发送本地通知。它成功打印“已处理操作”,但不发送通知。

这是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()
    }
}

}

1 个答案:

答案 0 :(得分:0)

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

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