Ios 重复本地通知将删除以前挂起的本地通知
我想每30分钟发送一次本地通知。我已经实现了重复本地通知,但它删除了前面的本地通知。场景如下所述: 我的客户想得到夜间警报。他希望,当他早上醒来时,他可以一次检查所有的通知警报 代码如下:Ios 重复本地通知将删除以前挂起的本地通知,ios,swift,localnotification,unnotificationrequest,unnotificationtrigger,Ios,Swift,Localnotification,Unnotificationrequest,Unnotificationtrigger,我想每30分钟发送一次本地通知。我已经实现了重复本地通知,但它删除了前面的本地通知。场景如下所述: 我的客户想得到夜间警报。他希望,当他早上醒来时,他可以一次检查所有的通知警报 代码如下: func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Ove
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler: {didAllow,error in })
return true
}
func applicationDidEnterBackground(_ application: UIApplication) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.subtitle = "I am your local notification"
content.body = "Yippppiiiieee...."
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let request = UNNotificationRequest(identifier: "testing", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
上一个挂起的通知被取消,因为您使用相同的
标识符创建了一个新通知。依照
如果提供唯一标识符,系统将创建新通知
如果标识符与以前传递的通知匹配,系统将再次提醒用户,用新通知替换旧通知,并将新通知放在列表的顶部
如果标识符与挂起的请求匹配,新请求将替换挂起的请求。
解决方案是始终使用新标识符创建UNNotificationRequest
var notificationCount = 0
func applicationDidEnterBackground(_ application: UIApplication) {
// (...)
notificationCount += 1
let request = UNNotificationRequest(identifier: "testing\(notificationCount)", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
首先,您不应该使用相同的标识符,因为它会删除已计划的标识符
let request = UNNotificationRequest(identifier: "testing", content: content, trigger: trigger)
其次,您必须插入不同的时间间隔
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
//
不要使用相同的标识符。它将覆盖以前的通知
现在您可能会问,如果每次生成一个随机字符串,我将如何识别被点击的通知
比如说,您有一个名为“coffeeTimeNotificationID”的标识符。只需将时间戳值([NSDate timeintervalncereferencedate]]
)附加到此字符串。现在您的字符串如下所示:“coffeeTimeNotificationID1232134314
”
每当用户点击此通知时,只需在标识符字符串中搜索“coffeeTimeNotificationID
”。如果您找到它,您将知道点击了什么类型的通知。如果我想在每30分钟后收到通知,如何设置不同的时间间隔对于唯一标识符,您可以生成随机字符串,并在30分钟的时间间隔内,您可以在当前时间中添加30分钟。使用此选项每30分钟重复一次let trigger=UNTimeIntervalNotificationTrigger(时间间隔:1800,重复:true)。如果您需要不同的正文/内容,则上行将每30分钟重复一次相同的通知,然后将其插入for循环中并设置不同的标识符,将repeating设置为False,当您的第一个通知将触发时,将执行didreceivelocalnotification函数,在该函数中,您可以使用UNTimeIntervalNotificationTrigger(时间间隔:1800,repeats:true)编写代码来触发通知。再次发出3个通知后,它将开始删除以前的通知。
(1...10).forEach {
let content = UNMutableNotificationContent()
content.title = "Hello \($0)"
content.subtitle = "I am your local notification \($0)"
content.body = "Yippppiiiieee.... \($0)"
content.badge = 1
let trigger = UNTimeIntervalNotificationTrigger(timeInterval:TimeInterval($0*1800), repeats: false)
let request = UNNotificationRequest(identifier: "testing\($0)", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}