0

In my app, I setup local notifications for some events. However, NSDateFormatter does not format date and time correctly. When I format a date, It gives 3 hours earlier. Similarly, when I get the current time using NSDate(), it gives 3 hours earlier as UTC. I faced the same problem for another event in my app, but I could solve it by adding dateFormatter.timeZone = NSTimeZone(name: "GMT"). The current issue is related to the code below:

var dateFormatter1 = NSDateFormatter()
var dateFormatter2 = NSDateFormatter()
dateFormatter1.dateFormat = "yyyy-MM-dd"
dateFormatter2.dateFormat = "yyyy-MM-dd HH:mm"

let mainItemList: Array<BagItem> = self.GetMainItemsInfoInBag()
for(item) in mainItemList
{
    let dateString = dateFormatter1.stringFromDate(item.date) + " " + item.time
    // Send servey notification 
    let serveyNotificationTime = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitHour, value: Int((120 + item.itemService.duration) / 60), toDate: dateFormatter2.dateFromString(dateString)!, options: NSCalendarOptions.WrapComponents)

    self.SetUpLocalNotification(serveyNotificationTime, message: "Do you want to join to the servey?": true)

    // if time for now is earlier than reservation time - 1 based on hour, set reminder notification.
    if(NSDate().dateByAddingTimeInterval(3*60*60).compare(NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitHour, value: -1, toDate: dateFormatter2.dateFromString(dateString)!, options: NSCalendarOptions.WrapComponents)!) == NSComparisonResult.OrderedAscending)
    {
         let reminderNotificationTime = NSCalendar.currentCalendar().dateByAddingUnit(NSCalendarUnit.CalendarUnitHour, value: -1, toDate: dateFormatter2.dateFromString(dateString)!, options: NSCalendarOptions.WrapComponents)
         self.SetUpLocalNotification(reminderNotificationTime, message: message, withCategory: false)
    }
}

// Set Up Local Notifications
private func SetUpLocalNotification(notificationTime: NSDate!, message: String!, withCategory: Bool!)
{
    var app: UIApplication = UIApplication.sharedApplication()
    var notifyAlarm: UILocalNotification! = UILocalNotification()
    if(notifyAlarm != nil)
    {
        notifyAlarm.fireDate  = notificationTime
        notifyAlarm.timeZone  = NSTimeZone.defaultTimeZone()
        if(withCategory == true)
        {
            notifyAlarm.category  = "FIRST_CATEGORY"
        }
        notifyAlarm.soundName = UILocalNotificationDefaultSoundName
        notifyAlarm.alertBody = message
        app.scheduleLocalNotification(notifyAlarm)
    }
}

In the code above, I am trying to setup local notifications as "reminder" and "join servey". However, I can not set up local notification times correctly.

Thank you for your answers

Best regards

saksut
  • 673
  • 1
  • 7
  • 14

1 Answers1

0

You just have to setup your fireDate after setting up your timeZone.

notifyAlarm.timeZone = NSTimeZone.localTimeZone()
notifyAlarm.fireDate = notificationTime

Also you shouldn't compare a boolean var to true. It is redundant

if withCategory { 
    notifyAlarm.category  = "FIRST_CATEGORY"
}

You will need to remove also the .dateByAddingTimeInterval(3*60*60) from your code.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • When I give timeZone 'dateFormatter.timeZone = NSTimeZone(name: "GMT")' as mentioned above, it setup local notification to further. Which format should I use? – saksut Apr 13 '15 at 08:45
  • 1
    always use localTime. btw it is the default. It will always just store a point in time (GMT). so if you are at some place with 3 hours offset, it will store the relative GMT time. – Leo Dabus Apr 13 '15 at 08:46
  • Ok I will try 'localTimeZone' and write you again. Thank you – saksut Apr 13 '15 at 08:51
  • the problem is probably the way you are using dateByAddingUnit – Leo Dabus Apr 13 '15 at 08:58
  • try adding dateFormatter2.timeZone = NSTimeZone.localTimeZone() – Leo Dabus Apr 13 '15 at 09:00
  • I have tried it too, for now, it shows date and time correctly. However, while I debugging I saw the time in UTC format still. So, it doesn't set notification to correct time – saksut Apr 13 '15 at 09:16
  • NSDate is always UTC/GMT – Leo Dabus Apr 13 '15 at 09:18
  • It is just a point in time. it will be displayed all over the world with different local time zones time offsets – Leo Dabus Apr 13 '15 at 09:18