swift - 发送本地通知

标签 swift nsnotificationcenter

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

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)

关于swift - 发送本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54846618/

相关文章:

ios - 十六进制(长字符串)l 到二进制

garbage-collection - 在 Swift 中,程序员负责打破对象之间的循环吗?

快速表格 View ,在表格 View 单元格中播放视频,内存泄漏

ios - 当导航 Controller 推/拉 UIViewController 时,如何添加和删除通知观察器?

ios - 应用程序在后台时本地通知声音不播放 - Swift

swift - 在 Swift 2.1 中使用泛型的 NSNotification 子类

swift - 无法摆脱 NSMenuItem 修饰符掩码中的 Shift-Key

ios - 如何创建 .ipa 文件以便用户可以像 AppStore 一样下载和安装?

ios - NSNotification 被 UITabBarController 多次调用

ios - 今天扩展 UIWebView