gpt4 book ai didi

swift - 发送本地通知

转载 作者:行者123 更新时间:2023-11-30 10:49:49 25 4
gpt4 key购买 nike

我添加了代码,以便在单击标注附件 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com