3

Hello I hope someone can help me with this issue. I'm still new to ios development. I'm testing UILocalNotification, I'm able to set the firedate and everything is working fine but if the firedate is in the past the local notification fires right away. Also everytime I exit the app the notification will show up again in the notification center. Is there anyway to make sure that a local notification only fires if the date is set to the future? I searched SO but I couldn't find my answer. Any help will be greatly appreciated.

This is the code in the viewDidLoad method

class ViewController: UIViewController {


    var time : NSDate?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        var comp = NSDateComponents()
        comp.year = 2015
        comp.month = 01
        comp.day = 20
        comp.hour = 16
        comp.minute = 00
        var calender = NSCalendar.currentCalendar()

        time = calender.dateFromComponents(comp)

        var notification = UILocalNotification()
        notification.alertBody = "this works"
        notification.fireDate = time
        notification.timeZone = NSTimeZone.defaultTimeZone()



        UIApplication.sharedApplication().scheduleLocalNotification(notification)
    }

This is the code in the AppDelegate

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        let notificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge
        let notificationSetiings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSetiings)
        return true
    }
gnm1978
  • 137
  • 1
  • 9

2 Answers2

5

If you set the fireDate to a past date, the notification will fire right away. Apple is designed and documented that well in their UILocalNotification documentation.

fireDate Property

The date and time when the system should deliver the notification. Declaration

Swift

@NSCopying var fireDate: NSDate?

Objective-C

@property(nonatomic, copy) NSDate *fireDate

Discussion

The fire date is interpreted according to the value specified in the timeZone property. If the specified value is nil or is a date in the past, the notification is delivered immediately.

So it's programmers responsibility to handle this situation. One thing you can do is, compare the date you are going to set with current date. If it is less don't set it.

Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • can you please elaborate on how I can compare two dates? Thank you in advace – gnm1978 Jan 21 '15 at 23:04
  • @GhassanMohammed: Check http://stackoverflow.com/questions/6112075/ios-compare-two-dates – Midhun MP Jan 21 '15 at 23:08
  • I just read the link but if I do compare the dates and the date is in the future, will this work even if the app is in the background? – gnm1978 Jan 21 '15 at 23:32
  • @GhassanMohammed: I didn't get your question. I mean to compare dates first, if date is past don't set to fireDate property, else set it. – Midhun MP Jan 22 '15 at 00:02
  • say I have an array of times like [15:15, 16:54, 21:21.....] this array will change depending on the location and the day. The user will get notifications according to this time array. My question is where do I use the compare method. Viewdidload or viewWillApear etc. It would be nice if you can give me a simple code example to compare a time and then schedule the notification. I only started learning this a couple of month's ago – gnm1978 Jan 22 '15 at 00:10
  • never mind I got it to work thank you @Midhun MP.... I was just a little confused. Thank you for your help – gnm1978 Jan 22 '15 at 00:46
0

The problem there is that you have to define local time zone before setting the fireDate.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • So you had two problems, now you have one. The problem is probably the way you are creating your date – Leo Dabus Jan 21 '15 at 23:29
  • The way Im creating the date is in the viewDidLoad method its just a test date. I will take a look at you answers hopefully I can find it – gnm1978 Jan 21 '15 at 23:36
  • http://stackoverflow.com/questions/27366127/setting-multiple-times-for-notifications-in-swift – Leo Dabus Jan 21 '15 at 23:43
  • I read your links but I still dont know how to apply it to my situation – gnm1978 Jan 22 '15 at 00:21
  • ok I just copy pasted the extension that you made for the other guy. But when I use the fireDateAt func to add my hours and min nothing happens the notification doesnt get fired. – gnm1978 Jan 22 '15 at 00:28
  • ok nevermind it did fire. But now I'm back to the same problem. If I leave the fire time and min in the past, the notification get fired right away even thos its in the past – gnm1978 Jan 22 '15 at 00:33
  • If it solved your problem don't forget to vote it up. Feel free to open another question as this one should be used as a reference for future visitors. – Leo Dabus Jan 22 '15 at 02:01
  • it didn't solve my problem. If you read my comment I'm back to the same problem where if the time is in the past the notification fires right away. But the problem was solved by @Midhun MP by using the NSDate compare method. Thank you for trying anyways – gnm1978 Jan 22 '15 at 02:10