Monday 22 June 2015

Local Notification in iOS


AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    application.applicationIconBadgeNumber = 0;
    
    return YES;
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState state = [application applicationState];
    
    if (state == UIApplicationStateActive)
    {
        NSDate *date = [NSDate date];
        NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
        timeFormatter.dateFormat = @"dd.MMM.yyyy";
        NSString *EventTitle = [NSString stringWithFormat:@"%@ - Today Events",[timeFormatter stringFromDate:date]];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:EventTitle
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
    
    // Request to reload table view data
    [[NSNotificationCenter defaultCenter] postNotificationName:@"addEvent" object:self];
    
    // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}


LocalNotifyController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addEvent) name:@"addEvent" object:nil];
    
    [self reloaddata];
    [self remaining_days];
}


-(void)reloaddata
{
    //add Events
    NSArray *eventdates = [[NSArray alloc]initWithObjects:@"2015-01-09",@"2015-01-10",@"2015-01-11",@"2015-01-12",@"2015-01-13",@"2015-01-14", nil];
    NSArray *eventfunctions = [[NSArray alloc]initWithObjects:@"Buy Gold",@"Buy Silver",@"Buy Diamond",@"Golden day",@"Good Friday",@"Pongal", nil];
    
    for (int r=0; r<5; r++)
    {
        // Schedule the notification
        NSString *event = [NSString stringWithFormat:@"%@ 17:00:00",[eventdates objectAtIndex:r]];
        
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
        NSDate *myDate = [df dateFromString:event];
        
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = myDate;//@"2015-01-09 11:03:31 +0000";
        localNotification.alertBody = [eventfunctions objectAtIndex:r];
        localNotification.alertAction = @"Continue";
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.timeZone = [NSTimeZone localTimeZone];
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
        
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        
        // Request to reload table view data
        [[NSNotificationCenter defaultCenter] postNotificationName:@"addEvent" object:self];
    }
}

-(void)addEvent
{
    //add more event
    
    //NSUserDefaults to find last date & add more events in UILocalNotification.
}

-(void)remaining_days
{
    //Get Count of Remaining Notifications
    int k = (int)[[[UIApplication sharedApplication] scheduledLocalNotifications] count];
    NSLog(@"%d", k);
    
    // Get list of local notifications
    NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    
    for (int i = 0; i < localNotifications.count; i++)
    {
        UILocalNotification *localNotification = [localNotifications objectAtIndex:i];
        
        // Display notification info
        NSString *str_Notify_Name = localNotification.alertBody;
        NSString *str_Notify_Date = [localNotification.fireDate description];
        
        NSLog(@"%@, %@", str_Notify_Name, str_Notify_Date);
    }

}

No comments: