0

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)

}
Pooja Mishra
  • 17
  • 1
  • 8

3 Answers3

0

Previous pending notification is canceled, because you create a new one with the same identifier. As per documentation

If you provide a unique identifier, the system creates a new notification.

If the identifier matches a previously delivered notification, the system alerts the user again, replaces the old notification with the new one, and places the new notification at the top of the list.

If the identifier matches a pending request, the new request replaces the pending request.

The solution is to always create UNNotificationRequest with a new identifier

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)
}
Community
  • 1
  • 1
mag_zbc
  • 6,801
  • 14
  • 40
  • 62
0

First you shouldn't use the same identifier as it removes already scheduled ones

 let request = UNNotificationRequest(identifier: "testing", content: content, trigger: trigger)

second you have to insert different TimeInterval

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)

//

(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)

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • How can I set a different time interval when I want to get notification after every 30 minutes – Pooja Mishra Aug 07 '18 at 07:53
  • for Unique identifier you can generate random string and for 30 min time interval, you can add 30 min in current time. – Nimit Aug 07 '18 at 08:00
  • Use this to repeat every 30 minutes **let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1800, repeats: true)** – Shehata Gamal Aug 07 '18 at 08:03
  • The above line will repeat the same notification every 30 minutes , if you need different body/content , then insert it inside a for-loop and set different identifier , with repeating set to false – Shehata Gamal Aug 07 '18 at 08:07
  • when your first notification will fire, didreceivelocalnotification function will execute,where you can write code to fire notification using UNTimeIntervalNotificationTrigger(timeInterval: 1800, repeats: true). – Nimit Aug 07 '18 at 08:11
  • I have a bunch of different notifications to fire after every 30 minutes – Pooja Mishra Aug 07 '18 at 08:19
  • Yes this edit is working fine. But how would I define the count for loop? Here it is static i.e 10 – Pooja Mishra Aug 07 '18 at 09:36
0

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.

nr5
  • 4,228
  • 8
  • 42
  • 82