If I schedule two UILocalNotifications and set them both to fire at the exact same fireDate. Then on the device (this is not the simulator bug) on the fireDate the application:didReceiveLocalNotification: will fire 4 times (2 times for each notification). Is this a known bug? Because I have not been able to find any information about it.
Please report the bug to http://bugreport.apple.com.
Having said that, it has been noticed before that while there is the bug in the simulator there also appears to be a bug on the device.
See the comments and answers on this SO question: local notification "didReceiveLocalNotification" calls twice
try this it's work in my application :
-(IBAction)setRemind:(id)sender{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
//Gets our picker
NSDate *selectedTime = [datePicker date];
strDate2 = [dateFormatter2 stringFromDate:selectedTime];
NSDate *Date=[dateFormatter2 dateFromString:strDate2];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:Date];
// Set up the fire time
NSDateComponents *dateComp = [[NSDateComponents alloc] init];
[dateComp setDay:[dateComponents day]];
[dateComp setMonth:[dateComponents month]];
[dateComp setYear:[dateComponents year]];
[dateComp setHour:9];
[dateComp setMinute:00];
[dateComp setSecond:00];
[dateComp release];
NSDate *date = [calendar dateFromComponents:dateComp];
[self scheduleAlarmForDate:date message:txtDescri.text];
}
-(IBAction)scheduleAlarmForDate:(NSDate*)date message:(NSString*)msg
{
//====== TO SEE OLD NOTIFI=======
UIApplication *Ap = [UIApplication sharedApplication];
NSArray *arr = [Ap scheduledLocalNotifications];
NSLog(#"Old Notifications :>> %#",arr);
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification *alarm = [[UILocalNotification alloc] init];
// Create a new notification
alarm.fireDate = date;
NSLog(#"fireDate IS >> %#", alarm.fireDate);
alarm.timeZone = [NSTimeZone localTimeZone];
alarm.alertBody = msg;
NSLog(#"msg IS >> %#",msg);
alarm.alertAction = #"Show";
alarm.repeatInterval = 0;
alarm.soundName = UILocalNotificationDefaultSoundName;
alarm.applicationIconBadgeNumber = 1;
[app scheduleLocalNotification:alarm];
[alarm release];
}
i Hope it's helpful to you.
Related
I have a piece of code that allows me to update my app at a specific time:
MainViewController.m:
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
[components setHour:14];
[components setMinute:5];
[components setSecond:0];
NSDate *nextAlarm = [calendar dateFromComponents:components];
if ([nextAlarm timeIntervalSinceNow] < 0)
{
nextAlarm = [nextAlarm dateByAddingTimeInterval:60*60*24];
}
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setRepeatInterval: NSCalendarUnitDay];
[localNotification setFireDate:nextAlarm];
[localNotification setAlertBody:#"It's been one day"];
[localNotification setValue:nextAlarm forKey:#"fireDate"];
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
AppDelegate.m
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *synchroDate = notification.fireDate;
NSDate *currentDate= [NSDate date];
NSLog(#"difference : %f", [synchroDate timeIntervalSinceDate:currentDate]);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger comps = (NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute);
NSDateComponents *date1Components = [calendar components:comps
fromDate: currentDate];
NSDateComponents *date2Components = [calendar components:comps
fromDate: synchroDate];
currentDate = [calendar dateFromComponents:date1Components];
synchroDate = [calendar dateFromComponents:date2Components];
BOOL result = [currentDate isEqual:synchroDate];
if (result)
{
[self autoSynchro];
}
My problem is that I would like that code be launched when my device is off. I can see that the notification is well launched on the ipad main screen, but the function is not launched...
May be I have to use that:
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateInactive)
{
//Do checking here.
}
But how to launch it if it is in standby mode?
Any ideas?
i want to show local notification on every saturday like this,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat=#"EEEE";
NSString *dayString = [[dateFormatter stringFromDate:[NSDate date]] capitalizedString];
NSLog(#"day: %#", dayString);
if([dayString isEqualToString:#"Saturday"])
{
NSLog(#"Success");
[self PushNotification];
}
-(void)PushNotification
{
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertTitle = #"Test";
localNotification.alertBody =#"test of notification";
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [NSDate date]];
[componentsForFireDate setWeekday: 7]; //for fixing Saturday
[componentsForFireDate setHour: 17]; //for fixing 5PM hour
[componentsForFireDate setMinute:0];
[componentsForFireDate setSecond:0];
localNotification.repeatInterval = NSCalendarUnitWeekOfMonth;
}
but my local notification is display in every minit then how can i display notification on every Saturday of the week.
thanks.
Try to declare fireData property in localNotification and schedule your notification
localNotification.fireDate = componentsForFireDate.date;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Hope this help
I'd like to create local notification with custom interval repeat (every 20 days for example).
I know we have NSDayCalendarUnit, kCFCalendarUnitMonth ... but I hope to set repeat interval at 20 days.
I don't want to create a notification for every day.
My real need is to repeat a notification for consecutive 21 days, then don't launch it for 7 days later, then a new 21 days with notification and 7 days without ... etc.
I should schedule all these days even if application is inactive.
To do this I decide to create a 21 notifications since a fire date with repeatInterval = 28days (here is the problem)
Try this, you can change the intervall by selecting setDay, setMonth, .... :
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:3];
NSDate *date3Days = [calendar dateByAddingComponents:components
toDate:[NSDate date]
options:0];
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
if ( oldNotifications ) {
[app cancelAllLocalNotifications];
app.applicationIconBadgeNumber = 0;
}
UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
if (notifyAlarm) {
notifyAlarm.fireDate = date3Days;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.alertBody = NSLocalizedString( #"Push message", #"");
notifyAlarm.soundName = #"sound.wav";
[app scheduleLocalNotification:notifyAlarm];
}
If you want to set a specific time after which the UILocalNotifications should appear you can create a method of the above solution and loop over an array which indicates the days you like to show a notification:
NSArray *arrayNumbers = #[#5, #7, #14, #21, #30, #60, #90, #120];
NSDictionary *dictNotifications =
[[NSUserDefaults standardUserDefaults] objectForKey:kUserDefAppStarts];
for ( NSNumber *bla in arrayNumbers ){
NSString *strKey = [NSString stringWithFormat:#"%#%#", kUserDefAppStarts, bla];
NSDictionary *dict = dictNotifications[strKey];
NSString *strMessageQuestion = dict[kKeyMessage];
[self createNotificationWithNumberOfDays:[bla integerValue]
andMessage:strMessageQuestion
userInfo:dict];
}
And here is the method you have to call
+ (void)createNotificationWithNumberOfDays:(int)days
andMessage:(NSString *)message
userInfo:(NSDictionary *)dict{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:days];
NSDate *dateAlert = [gregorian dateByAddingComponents:components toDate:[NSDate date] options:0];
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];
if( notifyAlarm ){
[notifyAlarm setFireDate:dateAlert];
[notifyAlarm setTimeZone:[NSTimeZone defaultTimeZone]];
[notifyAlarm setSoundName:#"soundname"];
[notifyAlarm setAlertBody:message];
[notifyAlarm setUserInfo:dict];
[app scheduleLocalNotification:notifyAlarm];
}
}
If you wanted to schedule a UILocalNotification for 20 days in the future from the current time you would do this:
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *dateComp = [[NSDateComponents alloc] init];
int desiredAmountOfMonths = 4;
for (int month = 0; month < desiredAmountOfMonths; month++)
{
dateComp.month = month;
dateComp.day = 20;
NSDate *fireDate = [currentCalendar dateByAddingComponents:dateComp toDate:[NSDate date] options:NSCalendarMatchNextTimePreservingSmallerUnits];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
}
You will need to modify the UILocalNotification for what message, sound, etc and then fire the notification once done customizing.
I am running application for local notification on iPod Touch (iOS 7). Notification is coming but sound for the notification is not playing. I have placed sound file in folder and also checked sound settings on device. I tried with many sound files but nothing worked. Below is the code :-
- (IBAction) scheduleAlarm:(id) sender {
[eventText resignFirstResponder];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = [self.datePicker date];
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = [eventText text];
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = #"alarm.wav"; ///UILocalNotificationDefaultSoundName;
// notify.soundName = #"../Documents/blabla.caf"
localNotif.applicationIconBadgeNumber = 1;
localNotif.alertBody = #"Staff meeting in 30 minutes";
// Specify custom data for the notification
// NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
// localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
[self.tableview reloadData];
}
Some times this may occur because of the device. And try to clear the previous notification:
UIApplication* app = [UIApplication sharedApplication];
[app cancelAllLocalNotifications];
Check this answer. This may help you solving the problem.
Please try first whether setting->notificationcenter->sounds is not off.Then check whether in setting->sounds->slider is not on zero.That means if u have sounds on zero that will not allow sound to come up.
Hope that helps.
I wrote an app that allows the user to enter a date on a date picker, and then when they tap a button it will schedule a local notification. The only issue is, the notification fires right when I tap the button. Any help is much appreciated! Here is my code:
- (IBAction)scheduleNotifButton:(id)sender {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [self.datePicker date];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:-3];
NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = targetDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Event is in 3 days!";
localNotif.alertAction = nil;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
You need to use [NSCalendar dateByAddingComponents:toDate:options:] instead:
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [self.datePicker date];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:-3];
NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
[dateComponents release];
...
If you're looking for complete code on how to schedule a notification a bit later (like say 3 seconds) here's the complete code :
NOTE : If you're inside the app you won't see a message box at the top of the screen, that might have to be handled through the UIApplication delegate.
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [[NSDate alloc] init];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setSecond: 3 ];
NSDate *targetDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
localNotification.fireDate = targetDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"Notified";
localNotification.alertAction = #"Show";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];