Hello i am facing a weird kind problem. Actually i want to schedule a daily notification (only once a day) at 8:00 AM. Below is my code for scheduling daily notification.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:#"08:00"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody = #"You just received a local notification";
localNotification.alertAction = #"View Details";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[formatter release];
[date release];
My problem is, I receive 2 local notifications. One at 8:00 AM and other at 10:00 AM. Why i am getting notification at 10:00 AM. I am scheduling it at 8:00 AM only. I know UILocalNotification library have some other weird kind of problems/bugs on most of apple devices. I just want to confirm whether there is some mistake in my code or it is a weird behaviour of UILocalNotification Library. I dont know why Apple is not working on resolving the issues being reported by many developers about UILocalNotification.
Note: I am using Xcode 4.6 and iOS 6.1
This would be the BEST link to get your answer.
Related
I am working on Alarm base project in iOS objective c.
I had implemented UILocalnotification for alarm when application is in background.
But problem i am facing is sound only plays for 30sec.
How can i increase the time interval for the sound or is there any other way to implement it instead of UILocalnotification .
- (IBAction)Alert:(id)sender{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd"];
//NSDate *date = [NSDate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate =[NSDate dateWithTimeIntervalSinceNow:15];
localNotif.timeZone = [NSTimeZone localTimeZone];
localNotif.alertBody = #"Emergency";
localNotif.alertAction = #"View";
localNotif.soundName = #"police.mp3";
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSYearCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Note : mp3 file i am using for UILocalnotification is of more that 60min duration.
Please advice.
Thank you
If device is in active mode, notification sound only play for 5 seconds. If device is in sleep mode, notification will play 30 seconds sound. I guess, this is standard behavi
As per Apple guidelines, you need to use mp3 file with 30 seconds duration. Please check below link.
https://developer.apple.com/reference/usernotifications/unnotificationsound
Note: FYI, UILocalNotification is deprecated from iOS10. You need to start using UNUserNotificationCenter.
link1
link2
If I want to set local notification to repeat every 2 week than how to achieve this?
I have read several questions and answers here, they have mention to reschedule local notification but how to reschedule local notification while application is in background ?
I will appreciate any help on same.
Thanks.
To reschedule UILocal Notification use this ,
NSDateFormatter *dat= [[NSDateFormatter alloc]init];
[dat setLocale:[NSLocale currentLocale]];
[dat setTimeZone:[NSTimeZone systemTimeZone]];
//[dat setDateFormat:#"YYYY-MM-dd"];// YYYY-MM-dd hh:mm a
//NSString *dateM=[dat stringFromDate:datM];
//[dat setDateFormat:#"YYYY-MM-dd h:mm a"];
NSDate *reminderDate=[NSDate date];
//Use manual option too for timing
reminderDate =[reminderDate dateByAddingTimeInterval:1*24*60*60*7];
UILocalNotification *localnoification = [[UILocalNotification alloc]init];
localnoification.fireDate=reminderDate;
localnoification.timeZone = [NSTimeZone defaultTimeZone];
localnoification.alertBody = word;
localnoification.alertAction = #"Tap to see word of the day";
//May Add Custom Sound Also
//localnoification.soundName = UILocalNotificationDefaultSoundName;
localnoification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
//localnoification.applicationIconBadgeNumber = 1;
localnoification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localnoification];
I am working with the notification part of the project, in this I want the sound notification which will notify the user about the activity. The code I am working with is below:
-(void) scheduleNotificationForDate {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateParts = [[NSDateComponents alloc] init];
[dateParts setHour:18];
[dateParts setMinute:20];
[dateParts setSecond:00];
NSDate *sDate = [calendar dateFromComponents:dateParts];
NSLog(#"date : %#", sDate);
localNotif.fireDate = sDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertAction = NSLocalizedString(#"View Details", nil);
localNotif.alertBody = #"Hello Testing";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"notification started");
}
With the help of this code I am getting the written notification on my device but I don't hear any alert sound when notification is alerted.
Please have a look on the code and try to locate my error. Your help will be much appreciable.
Look at your console.
date : 0001-01-01 17:26:32 +0000
You have scheduled the notification ~2000 years in the past, because you didn't specify components for year, month and day. Setting a fireDate that is in the past means the notification is delivered immediately.
If you want to schedule a notification for today at 18:30 you could do something like this:
// get year, month and day components from current time
NSDateComponents *dateParts = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
[dateParts setHour:18];
[dateParts setMinute:20];
[dateParts setSecond:00];
May be you have notification sound for this app disabled in Settings/Notification Center?
If your app is in active state it will not give alert sound, put it in background and test it.
NOTE: make sure your system volume is on .
I want to schedule UILocalNotificaion for 30 consecutive days at 8:00 AM daily and i want to implement that functionality with one UILocationNotification instance only. Here is my code to schedule local notification.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:#"08:00"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody = #"You just received a local notification";
localNotification.alertAction = #"View Details";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[formatter release];
[date release];
It will fire daily notifications forever at 08:00AM, but i want to be notified for 30 consecutive days only. The easiest solution is to fire 30 UILocalNotifications but i want to do that with 1 UILocalNotification instance. How can i do that? Please help.
In UILocalNotification userInfo property save NSDate object when you create Notification.
When you open Notification,
check with the current date with date in userinfo . If there is difference is more than 30 days you can cancel your local notification.
NSDate *now = [NSDate date];
// now a NSDate object for now + 30 days
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:30];
NSDate *endDate = [gregorian dateByAddingComponents:offsetComponents toDate:now options:0];
After that check:
if([currentDate isEarlierDate:endDate]){
//Fire the notification
}
I know this question has asked many times on StackOverflow but i couldn't able to set alarm in my app because i am very new to iOS? I am following this tutorial to set an alarm:
Setting a reminder using UILocalNotification in iOS.
However, it doesn't seems to be working for me.
I am in need to set alarm daily lets say 5.00 PM daily. I can't use date picker for choosing the time.
First on your xib, (or code) set the date picker mode: Time (Default is date & time)
The system assumes that the firedate is the current date, and the time is the time the user have chosen. This is not a problem because you set a repeat interval so it will work. I have tested it.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setFireDate:datePicker.date];
[localNotif setRepeatInterval:NSDayCalendarUnit];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
PS: It would be a good idea to set the seconds to 0 using NSDateComponents class so as to set the alarm to ring at the first second of the minute you want. You can check the:
Local notifications in iOS.
tutorial you posted on how to do this.
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID
Call this method with parameters and use this
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//set the notification date/time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:hours];
[dateComps setMinute:minutes];
[dateComps setSecond:seconds];
NSDate *notificationDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Set notification message
localNotif.alertBody = alertBody;
// Title for the action button
localNotif.alertAction = actionButtonTitle;
localNotif.soundName = (alertSoundName == nil) ? UILocalNotificationDefaultSoundName : alertSoundName;
//use custom sound name or default one - look here to find out more: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html%23//apple_ref/doc/uid/TP40008194-CH103-SW13
localNotif.applicationIconBadgeNumber += 1; //increases the icon badge number
// Custom data - we're using them to identify the notification. comes in handy, in case we want to delete a specific one later
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
You may need to change the style of the date picker to allow changing the time in addition to just the date.
You can try this
UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init];
[todolistLocalNotification setFireDate:[lodatepicker date]];
[todolistLocalNotification setAlertAction:#"Note list"];
[todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[todolistLocalNotification setAlertBody:text_todolist];
[todolistLocalNotification setHasAction:YES];