Saturday, May 25, 2013

UIApplication scheduleLocalNotification example in Objective C (iOS)


[UIApplication scheduleLocalNotification]

Schedules a local notification for delivery at its encapsulated date and time.
- (void)scheduleLocalNotification:(UILocalNotification *)notification
Parameters of [UIApplication scheduleLocalNotification]
notification
A local notification that the operating system delivers for the application at the date and time specified in the fireDate property of notification. Because the operating system copies notification, you may release it once you have scheduled it.

Example of [UIApplication scheduleLocalNotification]

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

    // Set the fire date/time
    [localNotification setFireDate:yourDate];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

    localNotification.applicationIconBadgeNumber=1;

    // Setup alert notification
    [localNotification setAlertAction:@"Open App"];
    [localNotification setAlertBody:[randonQuotesDic objectForKey:@"Rajneesh"]];
    [localNotification setAlertBody:@"You had set a Local Notification on this time"];

    localNotification.soundName=UILocalNotificationDefaultSoundName;
    [localNotification setHasAction:YES];      
    UIApplication *app=[UIApplication sharedApplication];
    [app scheduleLocalNotification:localNotification];

Example of [UIApplication scheduleLocalNotification]
-(void)endProxyAndWriteToSystemLocalNotification
{
        _proxying = NO;
        NSDate *dateAnchor = [NSDate date];

        NSEnumerator *enumerator = [self.notifications objectEnumerator];
        NSInteger i = 0;
        while (i < maxLocalNotifCount) {
            UILocalNotification *n = [enumerator nextObject];
            if (!d) {
                break;
            }

            if ([n.fireDate timeIntervalSinceDate:dateAnchor] >= 0) {
                [[UIApplication sharedApplication] scheduleLocalNotification:n];
                i++;
            }
        }

        [self.notificationDatas removeAllObjects];

}
Example of [UIApplication scheduleLocalNotification]
-(void) scheduleLocalNotifications
{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    for (int i = 0; i < 60; i++)
    {
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        if (localNotif == nil)
            return;
        NSDate *sleepDate = [[NSDate date] dateByAddingTimeInterval:i * 60];
        NSLog(@"Sleepdate is: %@", sleepDate);

        localNotif.fireDate = sleepDate;    

        NSLog(@"fireDate is %@",localNotif.fireDate);
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
        localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"This is local notification %i"), i];

        localNotif.alertAction = NSLocalizedString(@"View Details", nil);
        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
        NSLog(@"scheduledLocalNotifications are %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
        [localNotif release];

    }

    [pool release];
}