iOS – UILocalNotification fired twice for same notification

余生长醉 提交于 2019-12-07 13:48:03

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/11430669/ios-uilocalnotification-fired-twice-for-same-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!