I'd like to schedule local notifications using iOS 10. I'd like to know how to do this. I've looked all around the web, but I keep finding clues only for registering and handeling the notifications. Not for the scheduling of a local notification.
So, does anyone know how to do this?
The difference between the two stems from one major point: Local notifications originate from the application itself, as opposed to Push notifications which are triggered from a remote server.
As you are already aware, you can schedule maximum of 64 notifications per app. If you add more than that, the system will keep the soonest firing 64 notifications and will discard the other.
You schedule local notifications at a time when your app is running either in the foreground or background. After scheduling a notification, the system takes on the responsibility of delivering the notification to the user at the appropriate time. Your app does not need to be running for the system to deliver the notification.
Local notifications give you a way to alert the user at times when your app might not be running. You schedule local notifications at a time when your app is running either in the foreground or background. After scheduling a notification, the system takes on the responsibility of delivering the notification to the user at the appropriate time.
Well in iOS 10 Apple has deprecated UILocalNotification which means it is time to get familiar with a new notifications framework. This is a long post so let’s start easy by importing the new notifications framework:
After scheduling a notification, the system takes on the responsibility of delivering the notification to the user at the appropriate time. Your app does not need to be running for the system to deliver the notification.
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");
}
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With