-1

How to notify every month notification.

-(void)applicationDidEnterBackground:(UIApplication *)application {

        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        format.dateFormat = @"dd-MM-yyyy";
        NSComparisonResult result = [[format stringFromDate:[NSDate new]] compare:[format stringFromDate:[self lastDayOfMonthOfDate:[NSDate date]]]];
        if (result == NSOrderedSame){
            if (![USERDEFAULTS boolForKey:@"IS_MONTH"]) {
                [self getNotifiedForEveryMonth:nil];
            }}
        else{
            [USERDEFAULTS setBool:NO forKey:@"IS_MONTH"];
        }
    }

// getNotifiedForEveryMonth

-(void)getNotifiedForEveryMonth:(id)userinfo{
        // Schedule the notification
    [USERDEFAULTS setBool:YES forKey:@"IS_MONTH"];
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate date];
    localNotification.alertBody = @"Every Month Notificiation";
    localNotification.alertAction = @"Show me the item";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
 }

-(NSDate*)lastDayOfMonthOfDate:(NSDate *)date
{
        //    NSGregorianCalendar
    NSInteger dayCount = [self numberOfDaysInMonthCountForDate:date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents *comp = [calendar components:
                              NSCalendarUnitYear |
                              NSCalendarUnitMonth |
                              NSCalendarUnitDay fromDate:date];
    [comp setDay:dayCount];
    return [calendar dateFromComponents:comp];
}

-(NSInteger)numberOfDaysInMonthCountForDate:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [calendar setTimeZone:[NSTimeZone timeZoneWithName:TIME_ABSOLUTE]];
    NSRange dayRange = [calendar rangeOfUnit:NSCalendarUnitDay
                                      inUnit:NSCalendarUnitMonth
                                     forDate:date];
    return dayRange.length;
}

The above code with i try to notify the notification. Its getting notification every month end day,

If user close the notification and then next month comes the notifcation is not notifying.

After installation the application, how to notify the user very month end day a notification message.

Your inputs are appreciated.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
KkMIW
  • 1,092
  • 1
  • 17
  • 27

1 Answers1

0

1) If you want fire notifications every month, even if user didn't open an app more then month, you should set several notifications (up to 60). For example, set notifications for next 12 month.

2) You should set notifications by this code (on loop, 12 times):

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

3) Before you set new notifications, remove old with this code:

[[UIApplication sharedApplication] cancelAllLocalNotifications];
Igor
  • 12,165
  • 4
  • 57
  • 73