I want to send local notification after every 30 minutes. I have implemented repeating local notification but it removes the preceding local notifications. The scenario is as explained : My client wants to get night alerts. He wants that when he wakes up in the morning he can check all the notification alerts at once.
Here is the code:
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)
}
Don't use the same identifier. It will override the previous notification.
Now you would ask, how will I identify which notification was tapped If I generate a random string every time?
So let's say, you have this identifier named "coffeeTimeNotificationID". Just append a timestamp value (
[NSDate timeIntervalSinceReferenceDate]]
) to this string. Now your string would look like this: "coffeeTimeNotificationID1232134314
".Whenever user taps on this notification, just search for "
coffeeTimeNotificationID
", in the identifier string. If you find it, you'll know what type of notification was tapped.First you shouldn't use the same identifier as it removes already scheduled ones
second you have to insert different TimeInterval
//
Previous pending notification is canceled, because you create a new one with the same
identifier
. As per documentationThe solution is to always create
UNNotificationRequest
with a new identifier