Local notification repeatInterval - ios

My app is about showing student timetable and I want to add a local notification repeatInterval.
I plan to use switch button for this.
I tried this code and it doesn't work right now :
-(IBAction)theSwitch:(id)sender{
if (switcher.on) {
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
[dateComponent setWeekday:3]; // For tuesday
[dateComponent setHour:13];
[dateComponent setMinute:41];
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:#"ALARM!"];
[notification setFireDate:fireDate];
notification.repeatInterval = NSWeekCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
else {UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:nil];
}
}
So I need some more guidance here on this and some suggestions on how to achieve this.

If you not able to get local notification than please check this code below.
Add code in AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle launching from a notification
UILocalNotification *localNotification =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
NSLog(#"Recieved Notification %#", localNotification);
}
return YES;
}
Add method to handle local notification
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification {
// Handle the notificaton when the app is running
NSLog(#"Recieved Notification %#", notification);
}

Related

why I'm not receiving the local notification. when I'm setting reminder for a day in Week in objective c

I'm developing an application where I need to set the reminder for a particular day. I'm Using Local notification and finding the date of selected Day of current week.
this is my code:
In viewController.m
NSArray* components12 = [self.LblTime.text componentsSeparatedByString:#":"];
NSString *Hours = [components12 objectAtIndex:0];
NSString *Minutes =[components12 objectAtIndex:1];
NSDate *currentDate = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorianCalendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDateComponents *components = [gregorianCalendar components:(NSYearCalendarUnit| NSMonthCalendarUnit
| NSDayCalendarUnit| NSWeekdayCalendarUnit|NSWeekCalendarUnit) fromDate:currentDate];
NSLog(#"Current week day number %ld",(long)[components weekday]);
NSLog(#"Current week number %ld",(long)[components week]);
NSLog(#"Current month's day %ld",(long)[components day]);
NSLog(#"Current month %ld",(long)[components month]);
NSLog(#"Current year %ld",(long)[components year]);
NSDateComponents *dt=[[NSDateComponents alloc]init];
//Passing the Time (hours and minutes ) and Selected day with date
[dt setHour: [Hours integerValue]];
[dt setMinute:[Minutes integerValue]];
[dt setSecond:0];
[dt setWeek:[components week]];
[dt setWeekday:selecteddayValue];/// set the week Selected ay from picker
[dt setMonth:[components month]];
[dt setYear:[components year]];
NSDate *Date=[gregorianCalendar dateFromComponents:dt];// get the date of selected day of week
NSLog(#"Sunday date :%#",Date);
//Adding the reminder through Local notifiction
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate =Date;
localNotification.alertBody = #"Ready For Next Workout!";
localNotification.soundName = UILocalNotificationDefaultSoundName;
// localNotification.applicationIconBadgeNumber = 1; // increment
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Object 1", #"Key 1", #"Object 2", #"Key 2", nil];
localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[self showAlert:#"Reminder set Successfully"];
in appDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
// Handle launching from a notification
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
return YES;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
Now I'm not receiving the notification. Anyone please help for this.that would be very apperitiate.
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0)
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}

Local Notification not working iOS 8

I want to fire local notification on particular time daily, i have wrote this code snippet but it is not receiving notification.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// running on iOS8
if ([application respondsToSelector:#selector(registerUserNotificationSettings:)])
{
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
[application registerUserNotificationSettings:settings];
}
else // iOS 7 or earlier
{
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[application registerForRemoteNotificationTypes:myTypes];
}
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSLog(#"didReceiveLocalNotification----");
application.applicationIconBadgeNumber = 0;
}
//ViewController class
- (void)viewDidLoad
{
[super viewDidLoad];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setHour:7];
[dateComponents setMinute:59];
NSDate *currentDate = [NSDate date]; //2015-04-13 07:56:09 +0000
NSDate *fireDate = nil;
fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody = #"Notiication success....";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
My Log shows didReceiveLocalNotification but notification not appearing in device :(
Where i am making mistake please help.
Thanks in advance
The notification won't appear while app is in foreground. The notification will show up only if the app totally close or in the background.

App Delegate's application:didReceiveLocalNotification: not called

In my app delegate the method - (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification is never called.
This is how I create the notification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
// set UUID, which we will store in userDefaults for later
NSMutableDictionary *myUserInfo = [[NSMutableDictionary alloc] init];
NSString *uuid = [[NSProcessInfo processInfo] globallyUniqueString];
[myUserInfo setValue:uuid forKey:KEY_UUID];
[myUserInfo setValue:#"month" forKey:KEY_UNIT];
[myUserInfo setObject:#YES forKey:KEY_RESCHEDULE];
NSInteger row = [_wurmProphylaxePickerView selectedRowInComponent:0];
switch (row) {
case 0:
[myUserInfo setValue:#2 forKey:KEY_FREQUENCY];
break;
case 1:
[myUserInfo setValue:#4 forKey:KEY_FREQUENCY];
break;
case 2:
[myUserInfo setValue:#6 forKey:KEY_FREQUENCY];
break;
default:
[myUserInfo setValue:#4 forKey:KEY_FREQUENCY];
break;
}
notification.userInfo = myUserInfo;
// calculate date for next notification, depends on the user's selection
NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *myComps = [[NSDateComponents alloc] init];
[myComps setMinute:1];
notification.fireDate = [calendar dateByAddingComponents:myComps toDate:today options:0];
notification.timeZone = [NSTimeZone localTimeZone];
notification.alertBody = #"My alertBody";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
And this is in my app delegates, but is never called:
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
BOOL repeat = [[userInfo objectForKey:KEY_RESCHEDULE] boolValue];
if (repeat)
{
NSInteger frequency = (NSInteger)[userInfo objectForKey:KEY_FREQUENCY];
NSString *unit = (NSString *)[userInfo objectForKey:KEY_UNIT];
NSString *uuid = (NSString *)[userInfo objectForKey:KEY_UUID];
// calculate date for next notification
NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *myComps = [[NSDateComponents alloc] init];
if ([unit isEqualToString:#"month"]) {
//[myComps setMonth:frequency];
[myComps setMinute:frequency];
} else {
}
// create new notification
UILocalNotification *newNotification = [[UILocalNotification alloc] init];
newNotification.fireDate = [calendar dateByAddingComponents:myComps toDate:today options:0];
newNotification.timeZone = [NSTimeZone localTimeZone];
newNotification.alertAction = notification.alertAction;
newNotification.alertBody = notification.alertBody;
newNotification.userInfo = notification.userInfo;
// schedule it
[[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
}
}
tested on iOS 8, not sure about iOS 7...
If the app is not active when the notification fires, you would handle this in didFinishLaunchingWithOptions as illustrated by this example from the Handling Local and Remote Notifications section of the Local and Push Notifications Programming Guide:
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
[viewController displayItem:itemName]; // custom method
app.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
The didReceiveLocalNotification is called if the app was active when the notification fired.

Notification fireTime not working. Sends notifications immediately in ios

I have used the following code to send the local notification in particular time. It works fine when calling from a method but when i am calling from another method its not working.
Code as follows:
-(void)notificationUserInfo:(NSDictionary *)notificationData
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy HH:mm"];
NSString *tipid = [notificationData objectForKey:#"receivedTipsId"];
NSString *currentDate = [notificationData objectForKey:#"receivedDate"];
NSString *tipsCategoryid = [notificationData objectForKey:#"categoryId"];
NSString *todayDateTime = [notificationData objectForKey:#"todayDate"];
NSString *categoryName = [notificationData objectForKey:#"categoryName"];
NSString *tipsForNotification = [notificationData objectForKey:#"tipsForNotification"];
NSString *cardType = [notificationData objectForKey:#"cardType"];
//Assigning the notification contents
NSDate *updatedDateFormat = [dateFormat dateFromString:todayDateTime];
notification.fireDate = updatedDateFormat;
notification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:tipid, #"receivedTipsId", currentDate, #"receivedDate", tipsCategoryid, #"categoryId", cardType, #"cardType", nil];
notification.alertBody = [NSString stringWithFormat:#"%#: %#",categoryName, tipsForNotification];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Thanks in advance.
If you are using this in iOS 8 then you have to register your app for local notification.
IN app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { /*...*/ }
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)])
{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
}
And then
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (!localNotification) {
break;
}
**localNotification.timeZone = [NSTimeZone defaultTimeZone];**
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:20]];
localNotification.alertBody = #"message";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Use above code.
This is because of problem with time and date format. When i used this method, I have given the firedate as past date. eg., instead of dd/MM/yyyy i used MM/dd/yyyy.
So 11/12/2014 is returned as 12/11/2014. So the notification is received immediately after the notification scheduled.

Issue with local notification

i am trying to get notification when date of array become same as current date this code is i am using for this task
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setDateFormat:#"dd/MM/yyyy"];
UILocalNotification *notification = [[UILocalNotification alloc] init];
for (int i=0;i<_convertedBdates.count;i++)
{
NSDate *date =[Form dateFromString:[_convertedBdates objectAtIndex:i ]];
NSLog(#"date%#",date);
if(notification)
{
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = #"New ";
notification.alertAction = #"View";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
suggest me what i am doing wrong
have you put this code in your app delegate?
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"Notification Received, %#, set for date %#", notification.alertBody, notification.fireDate);
}
if problem persist then i am here :)
If your NSDate is less than the current date then notification will not be fire. If you are setting notification date on the user birthdate then this will happen.
If your application is in running mode then :
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
above method is called.
Hope this will help you.
All the best !!!

Resources