ios - 如何在 Swift 中安排通知
问题描述
我正在尝试在我的项目中安排通知。我没有收到任何错误或运行时崩溃,但是由于某种原因,我在调度时没有在模拟器上收到任何通知。我不确定我到底哪里出错了,因为我在之前的项目中已经成功地做到了这一点。
有没有人有什么建议 ?
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
委托方法并且因为应用程序处于前台时,通知才会出现
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
推荐阅读
- javascript - Javascript - 从字符串数字数组中删除逗号
- python - 导入python文件会导致错误,它本身就可以完美运行
- c# - 使用 RestClient 请求发送文件
- mercurial - 如何将工作目录复制到新的提交
- python - 有没有办法只从 Python 中的 JSON 网站导入某些对象?
- linux - 嵌入在 linux 脚本中的 SQLPLUS 无法按预期工作
- angular - “environment.ts”不在“rootDir”角度库构建错误下
- android - targetAPI = 29 和设备的 Android 版本上的 Android ACTIVITY_RECOGNITION。< 10
- linux - 哪个代码用于在 PS 中打印线程名称?
- azure - Packer azure-arm vhd 构建失败,找不到资源组