I have to generate a local notification for every 28 days,i.e the user will register on a particular date i need to generate a Local notification exactly after 28 days form that registration date,and from then i need to repeatedly generate that local notification for every 28 days.
Asked
Active
Viewed 599 times
1 Answers
0
Check accepted answer from here : How to schedule a local notification in iOS 10 (objective-c)
Try it. Its deprecated but working code. Use it for Before iOS 10.0 :
//Get all previous noti.. NSLog(@"scheduled notifications: --%@----", [[UIApplication sharedApplication] scheduledLocalNotifications]); NSDate *now = [NSDate date]; now = [now dateByAddingTimeInterval:60*60*24*7]; //7 for 7th day of the week. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[NSTimeZone localTimeZone]]; NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now]; NSDate *SetAlarmAt = [calendar dateFromComponents:components]; UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = SetAlarmAt; NSLog(@"FIRE DATE --%@----",[SetAlarmAt description]); localNotification.alertBody =@"Alert"; localNotification.alertAction = [NSString stringWithFormat:@"My test for Weekly alarm"]; localNotification.userInfo = @{ @"alarmID":[NSString stringWithFormat:@"123"], @"SOUND_TYPE":[NSString stringWithFormat:@"hello.mp3"] }; localNotification.repeatInterval=0; //[NSCalendar currentCalendar]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
For iOS 10.0 and later: Now try with UserNotifications framework: Add the framework, and import like #import . In Appdelegate Didfinishluanch method.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (!error) { NSLog(@"request succeeded!"); [self testAlrt]; } }];
In your ibaction or method, write it and test:
NSDate *now = [NSDate date];
// NSLog(@"NSDate--before:%@",now);
now = [now dateByAddingTimeInterval:60*60*24*7];
NSLog(@"NSDate:%@",now);
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now];
NSDate *todaySehri = [calendar dateFromComponents:components]; //unused
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Notification!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"This is local notification message!"
arguments:nil];
objNotificationContent.sound = [UNNotificationSound defaultSound];
/// 4. update application icon badge number
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"ten"
content:objNotificationContent trigger:trigger];
/// 3. schedule localNotification
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"Local Notification succeeded");
}
else {
NSLog(@"Local Notification failed");
}
}];

Community
- 1
- 1

Jamshed Alam
- 12,424
- 5
- 26
- 49
-
Thanks Jamshed ill try it and let you know – Surendra Karibandi Oct 31 '16 at 06:13
-
I have done that for current day. You just multiply with 28 and test. First set the alarm by running it. And then change the date 28 later and re run the app. It should work. My code is working on my live app. – Jamshed Alam Oct 31 '16 at 06:28
-
Jamshed i have to trigger this notification for every 28 days repeatedly do i have to put YES in repeats in UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO]; – Surendra Karibandi Oct 31 '16 at 10:10
-
@SurendraKaribandi have you got solution for 28 days? – Vvk Feb 23 '17 at 05:55