Ios 如何在Swift中安排通知
我正在尝试在我的项目中安排通知。我没有收到任何错误或运行时崩溃,但是由于某些原因,我在调度时没有收到模拟器上的任何通知。我不确定我到底错在哪里,因为我在以前的项目中成功地做到了这一点 有人有什么建议吗Ios 如何在Swift中安排通知,ios,swift,uilocalnotification,unusernotificationcenter,Ios,Swift,Uilocalnotification,Unusernotificationcenter,我正在尝试在我的项目中安排通知。我没有收到任何错误或运行时崩溃,但是由于某些原因,我在调度时没有收到模拟器上的任何通知。我不确定我到底错在哪里,因为我在以前的项目中成功地做到了这一点 有人有什么建议吗 import UserNotifications func addNotification(title: String, category: String, UI: String, date: Date) { // Remove Notification removeNoti
import UserNotifications
func addNotification(title: String, category: String, UI: String, date: Date) {
// Remove Notification
removeNotifcation(UI: UI)
// Create Notification
// iOS 10 Notification
if #available(iOS 10.0, *) {
let notif = UNMutableNotificationContent()
notif.title = title
notif.subtitle = "Reminder for \(title)"
notif.body = "Your Reminder for \(title) set for \(date)"
notif.sound = UNNotificationSound.default()
notif.categoryIdentifier = UI
let today = Date()
let interval = date.timeIntervalSince(today as Date)
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)
let request = UNNotificationRequest(identifier: title, content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if error != nil {
print(error as Any)
// completion(Success: false)
} else {
//completion(Sucess: true)
}
})
} else {
let newNotification:UILocalNotification = UILocalNotification()
newNotification.category = category
newNotification.userInfo = [ "UUID" : UI]
newNotification.alertBody = "Your reminder for \(title)"
newNotification.fireDate = date
//notification.repeatInterval = nil
newNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(newNotification)
}
}
func removeNotifcation(UI: String) {
//Remove Notif
//iOS 10 Notification
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [UI])
} else {
//Remove Notif
let app:UIApplication = UIApplication.shared
for oneEvent in app.scheduledLocalNotifications! {
let notification = oneEvent as UILocalNotification
let userInfoCurrent = notification.userInfo!
let uuid = userInfoCurrent["UUID"] as! String
if uuid == UI {
//Cancelling local notification
app.cancelLocalNotification(notification)
break;
}
}
}
}
这就是我调用方法的方式
dataManager.addNotification(title: "String", category: "001", UI: uuid, date: datePicker.date)
只有在您实现了
willPresent
delegate方法并且由于
let today = Date()
此日期会在您运行应用程序时触发,所以请添加一个时间间隔并将应用程序发送到后台,您将看到它,另外,请确保您在AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
print("granted: (\(granted)")
}
return true
}
你也可以看看这个
//
编辑:要验证它是否触发,请执行此操作
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("handling notifications with the TestIdentifier Identifier")
completionHandler()
}
并将委托设置为VC
UNUserNotificationCenter.current().delegate = self
但是我不使用间隔,我使用传递给方法的日期设置间隔,并设置从今天开始的间隔'let interval=date.timeIntervalSince(今天为date)'let notifTrigger=UNTimeIntervalNotificationTrigger(timeInterval:interval,repeats:false)'let request=UNNotificationRequest(标识符:title,content:notif,trigger:notifTrigger)“我确实请求许可,但您没有在间隔中添加任何时间偏移,请传递此触发器让trigger=UNTimeIntervalNotificationTrigger(时间间隔:5,重复:false),在应用程序启动后,在5秒结束前将其发送到后台以查看通知