简体   繁体   中英

Fire Date issue in local notification

I have a view controller which contains a field for user to enter date value.In contrast I have a field called "Remind Before Days" for the user to select when the notification should fire.If the remind before day is same day,then notification is set to fire on the date,but when the remind day is before 1 day,then notification should fire before one day the date set(specified).For this I have written a method called -(void)setNotification and here is the implementation code:

- (void)setNotification
{
    //Set notification after confirmation of saved data

    Class cls = NSClassFromString(@"UILocalNotification");
    UILocalNotification *notif = [[cls alloc] init];

    if (cls != nil) 
    {
        textField = [self.fields objectAtIndex:3];

        if (textField.text == @"Same Day") 
        {
            notif.fireDate = [datePicker date];
            notif.timeZone = [NSTimeZone defaultTimeZone];
        }
        else if(textField.text == @"1 Day")
        {
            NSDate *now = [datePicker date];

            // set up date components
            NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:now];
            [components setDay:-1];

            // create a calendar to form date
            NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
            NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];

            notif.fireDate = newDate2;
            notif.timeZone = [NSTimeZone defaultTimeZone];
        }
        notif.timeZone = [NSTimeZone defaultTimeZone];
        notif.alertBody = textView.text;
        notif.alertAction = @"View";
        notif.soundName = @"lazy_afternoon.mp3";
        notif.applicationIconBadgeNumber = 1;
        textField = [self.fields objectAtIndex:1];
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:self.textField.text forKey:kReminder];
        notif.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];
    }
}

Now as we are all aware that when the notification gets fired,the user clicks view.Then we show an alert,the implementation code is written in appDelegate.Here it is:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{    

    // For The Purpose Of Notification.
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls) 
    {
        UILocalNotification *notification = [launchOptions objectForKey:
                                             UIApplicationLaunchOptionsLocalNotificationKey];

        if (notification) 
        {
        NSString *reminderText = [notification.userInfo objectForKey:kReminder];
        [self.viewController showReminder:reminderText];
        }
    }

    application.applicationIconBadgeNumber = 0;
}

Now after local notification is received,we do the following ie:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{
    application.applicationIconBadgeNumber = 0;
    NSString *reminderText = [notification.userInfo objectForKey:kReminder];
    [self.viewController showReminder:reminderText];
}

Now I have set the -(void)setNotification action to right navigation right bar button item titled "Save" as follows:

-(IBAction)save:(id)sender
{
[self setNotification];
}

When I don't specify any condition for fire date ie simply assign as :

notif.fireDate = [datePicker date]; everything's fine with notification(no issues).

But when I do as the above ie condition for fire date,then the notification is not getting fired.Instead the alert is getting fired when I click save.Also when I quit the simulator,I could see some thread problem.I don't understand what's wrong with the code (implementation).I have gone through several links,also the apple documentation of UILocalNotification.Couldn't find out any property or method to set fire Date according to conditions.

I found out a method "repeatTimeInterval" which is relavant and applicable when a notification must be repeated weekly,yearly etc..which doesn't suit the requirement that the "date to be fired is this when the remind days in textField is this"

Can any one please guide me right,

Thanks all in advance :)

I believe if you check this link out you will find out. But from what I have seen the code you have provide is from this site anyway.

http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html

I know I arrived a little late, but for me, the error on your code is here:

if (textField.text == @"Same Day")

Because you can't compare NSStrings using the operator == . I think that if you use isEqualToString:(NSString*) it will work properly. It would be great if you could edit your original post and fix the code, so people looking for info about local notifications won't run into the same problem.

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