1

I'm having a problem with my local notification that is: if the device is in off state or when changing the date of phone, the notification is kept in queue and firing along with next notification. Why?

Since need to have very concern about the date of notification.. How to assure correct delivery of notification?

Where should we put code for removing expired notification?

Saranjith
  • 11,242
  • 5
  • 69
  • 122
  • Possible duplicate of [Dismiss an already delivered UILocalNotification?](https://stackoverflow.com/questions/10652274/dismiss-an-already-delivered-uilocalnotification) – mfaani Jun 05 '17 at 08:04
  • or possible duplicate of [this](https://stackoverflow.com/questions/38922892/how-can-i-removed-previously-delivered-notifications-when-a-new-notification-arr). If you're using iOS 10, then the link in my answer would have enough information... – mfaani Jun 05 '17 at 08:05
  • In your title, do you mean _correct_ or _current_? – Cœur Aug 05 '17 at 12:44

1 Answers1

2

So you mean you had a notification named ABC to be triggered in 30 minutes...but then you turned your iPhone off for 3 hours...then turned it on and saw that notification ABC notification even though its time was passed? AFAIK ....the notification will still be there unless you remove it using removeDeliveredNotifications(withIdentifiers:).

Basically call:

UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notificationIdentifer1", "notificationIdentifer2"]).

For more information see this moment of this WWDC video.

Also see this tutorial look for the section "4. Managing Notifications"

As to address your edit:

To be honest I'm not sure...but here's what I think you should do:

I suggest to make the class that creates the notifications:

Step1: Conform to UNUserNotificationCenterDelegate

Step2: Implement:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
 withCompletionHandler completionHandler: @escaping () -> Void) {
}

Now that you turned your device on...This function would get called only if app is in background. There you have a chance to kill your obsolete notifications.

This function is called in both foreground and background, if it's background then you can kill obsolete notifications following step3, if it's foreground well then you don't need to do anything, it send off the queue itself, because it was shown!

Step3:

Inside the function you implemented do:

UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: {deliveredNotifications -> () in
            print("\(deliveredNotifications.count) Delivered notifications-------")
            for notification in deliveredNotifications{
               if (someCondition){
             UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["notificationIdentifer1", "notificationIdentifer2"]).

              }
            }            
        })

As for the iPhone time change issue... you should come up with a reasonable condition and find the notification and remove it...or simply change your notification trigger from a UNCalendarNotificationTrigger to UNTimeIntervalNotificationTrigger. (though again I'm not sure if your timer resets itself or not. I think it shouldn't and you're good).

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • So app needed to opened by user again after turning on the device to run our code right – Saranjith Jun 05 '17 at 08:19
  • @Saranjith see my edit. But I'm learning just as you are. Didn't test my own suggestions :/ but hopefully they'll work for you. – mfaani Jun 05 '17 at 09:00
  • Thanks for your effort made I have doubted - **Now that you turned your device on...This function would get called only if app is in background.** How my application will be in background when device is turning on? – Saranjith Jun 06 '17 at 04:27
  • @Saranjith I coupd be wrong but from what I have learned there is no way of achieving what you want. Though you could still use the deprecated UILocalNotification. Using that you would get a trigger even if app is in background and just turned. But it would be destined to break in yhe upcoming iOS versions – mfaani Jun 06 '17 at 05:06