Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule local Notification after app was closed for 7 days

It was quite a bit hard to enable local notifications for my application.

And yeah, I set up my project with a local notification witch I want to schedule after the app had been closed for one week. For example if the user opens the app on saturday 8th, the local notification should appear on the next saturday the 15th, but the time should changed. For example he/she closes the app at 8pm and I don't want to disturb them at 8pm next week so I want to display every notification at 6pm or something like that.

Now you know my problem and here is my code I'm using:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
    NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];

    //set day (saturday)

    [componentsForReferenceDate setDay:26] ;
    [componentsForReferenceDate setMonth:1] ;
    [componentsForReferenceDate setYear:2013] ;

    [componentsForReferenceDate setHour: 17] ;
    [componentsForReferenceDate setMinute:30] ;
    [componentsForReferenceDate setSecond:00] ;

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate];

    // Create the notification

    UILocalNotification *notification = [[UILocalNotification alloc]  init] ;


    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"Du wirst vermisst! \nDeine App braucht dich, schreibe sie zu Ende!"] ;
    notification.alertAction = @"Zurückkehren";
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
    notification.repeatInterval= NSWeekCalendarUnit ;
    notification.soundName = @"Appnotifisound.wav";
    notification.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

}

I know that there have to be more methods for deleting the badge and the didrecievenotification, but I let them out because there are not important for this.

With this code I managed to schedule the notification on every saturday at 5:30pm (Germany). But I wanted to schedule it only once, when the app had been closed for exactly one week. Is that somehow possible? I would be glad if someone could correct my code or give me a solution for this.

Best regards and thank you for reading this long post,

Noah

like image 749
MasterRazer Avatar asked Feb 17 '23 07:02

MasterRazer


2 Answers

You should unschedule previous notification.

for (UILocalNotification *lNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) 
    {
        if ([[lNotification.userInfo valueForKey:@"information"] isEqualToString:@"Some information"])
        {
            [[UIApplication sharedApplication]cancelLocalNotification:lNotification];
            break;
        }
    }

Set fire date by below way:

NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *componentsForReferenceDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                     fromDate:[NSDate date]];
    [componentsForReferenceDate setHour: 17] ;
    [componentsForReferenceDate setMinute:30] ;
    [componentsForReferenceDate setSecond:00] ;

    NSDate *tempDate = [calendar dateFromComponents: componentsForReferenceDate];
    [componentsForReferenceDate release];

    NSDateComponents *comps = [[NSDateComponents alloc]init];
    [comps setDay:7];
    NSDate *fireDateOfNotification = [calendar dateByAddingComponents:comps
                                                               toDate:tempDate options:0]
    [comps release];
like image 64
Apurv Avatar answered Feb 20 '23 01:02

Apurv


The notification.repeatInterval = NSWeekCalendarUnit is causing this. use:

notification.repeatInterval = 0;

This prevent repeating (source).

like image 39
Mike D Avatar answered Feb 20 '23 01:02

Mike D