Reputation: 368
I have read many post of notification but somehow i am going wrong somewhere so that's why i post this question. I want to get daily notification at 9.00 am in my app. I get it properly at 9.00 am with no problem what so ever but the difficulty is that i also get same notification at 2.00 am. I try with following code. Can anyone tell me where i am getting wrong. Or is it problem of ios6. Any kind of help will be appreciated. Thank you.
NSString *day =@"9:00 AM";
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease];
[dateFormat setDateFormat:@"hh:mm a"];
//NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
//[dateFormat setTimeZone:gmt];
NSDate *today=[dateFormat dateFromString:day];
NSLog(@"string %@ & date %@",day,today);
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil)
{
// delObj.QCouter=delObj.QCouter+1;
//[[UIApplication sharedApplication] cancelAllLocalNotifications];
notif = [[cls alloc] init];
notif.fireDate =today;
notif.timeZone = [NSTimeZone systemTimeZone];
NSLog(@"timeZone %@ ",[NSTimeZone systemTimeZone]);
notif.alertBody = @"You have a new letter ";
notif.alertAction = NSLocalizedString(@"View", nil);;
notif.soundName = @"Ding3.wav";
notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSDayCalendarUnit;
[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"Status"];
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"You have a notifiaction"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
// NSLog(@"userInfo %@",notif.userInfo);
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
[[NSUserDefaults standardUserDefaults] setObject:@"CurrentDay" forKey:@"DayChange"];
}
Upvotes: 2
Views: 142
Reputation: 1993
Hi try following code:-
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date];
[components setHour: 9];
[components setMinute: 0];
[components setSecond: 0];
NSDate *today = [gregorian dateFromComponents: components];
[gregorian release];
Class cls = NSClassFromString(@"UILocalNotification");
if (cls != nil)
{
notif = [[cls alloc] init];
notif.fireDate =today;
notif.alertBody = @"You have a new letter ";
notif.alertAction = NSLocalizedString(@"View", nil);;
notif.soundName = @"Ding3.wav";
notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSDayCalendarUnit;
[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"Status"];
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"You have a notifiaction"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
// NSLog(@"userInfo %@",notif.userInfo);
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
[[NSUserDefaults standardUserDefaults] setObject:@"CurrentDay" forKey:@"DayChange"];
}
Upvotes: 1
Reputation: 2558
Simulator or real device?
Simulator has a known bug where it generates two "fires" of a single notification. If that's what's happening, try it on a physical device and see if the same behavior occurs. It's annoying, but not an actual problem with your app. (Assuming it's the simulator, of course!)
See this question: iOS – UILocalNotification fired twice for same notification
Edit based on "not in the simulator":
try adding a call to this after you schedule the notification, and see if some other part of your code is slipping in another scheduled item you're not aware of:
- (void) _debug_logExistingToConsole
{
if (LOG) NSLog(@"Notifications set is now: \n");
UIApplication *Ap = [UIApplication sharedApplication];
NSArray* arr = [Ap scheduledLocalNotifications];
if (LOG) NSLog(@"%@", arr);
}
Upvotes: 1