简体   繁体   中英

Local Notification set fire time

I want to set the daily alarm on the bases of user input. Like user will select time from date picker "10:30", Then i need to set alarm at a that time daily. I write the following code:

func setAlarmAtTime(#time:NSString, withMessage message:NSString){
    var loacalNotification = UILocalNotification();
    var calendar = NSCalendar.currentCalendar();
    calendar.timeZone = NSTimeZone.localTimeZone()
    var components = calendar.components(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit, fromDate: NSDate.date());
    NSLog("%@",NSDate.date())
    NSLog(time);
    var timeComponents = time.componentsSeparatedByString(":");
    components.hour = timeComponents[0].integerValue;
    components.minute = timeComponents[1].integerValue;
    if components.isValidDateInCalendar(calendar){
        var fireDate = calendar.dateFromComponents(components);
        NSLog("%@",fireDate!);
        loacalNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay;
        loacalNotification.timeZone = NSTimeZone.localTimeZone();
        loacalNotification.fireDate = fireDate;
        loacalNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay;
        loacalNotification.soundName = UILocalNotificationDefaultSoundName;
        loacalNotification.alertBody = message;
    }

But it shows different time based on time zone following are the behaviour when i try to set alarm at 6:40:

Current Date:  2014-08-12 12:07:21 +0000
Alarm Time: 6:40
Fire Date: 2014-08-12 01:10:00 +0000

I tried to set time zone to local as well as current but nothing works :(

        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"dd-MMM-yyyy HH:mm"];
        NSDate *reminderDate = [df dateFromString:self.lblDate.text];
        // Schedule the notification
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = reminderDate;
        localNotification.alertBody = self.txtName.text;
        localNotification.alertAction = @"Show me the item";
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.soundName =@"sound.mp3";
        NSMutableDictionary *notifyDict = [[NSMutableDictionary alloc] init];
        [notifyDict setValue:self.lblType.text forKey:NOTIFICATION_TYPE];
        [notifyDict setValue:self.txtName.text forKey:NOTIFICATION_TITLE];
        if (![self.tvDescription.text isEqualToString:@"Write Description"]) {
            [notifyDict setValue:self.tvDescription.text forKey:NOTIFICATION_DESCRIPTION];
        }
        [notifyDict setValue:self.lblDate.text forKey:NOTIFICATION_DATE];
        localNotification.userInfo = notifyDict;
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        [self.navigationController popViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];

The time that it is given, is in greenwich mean time (GMT)

we just need to set

localNotification.timeZone = [NSTimeZone systemTimeZone]; 

as it will represent your device or system time zone and schedule your app

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM