Mọi người đều có khả năng kể từ khi tiếp tục, nhưng tôi muốn chia sẻ giải pháp của mình cho vấn đề này. (Xin lỗi về các tên biến dài ...)
Ý tưởng rất đơn giản: luôn giữ cho fireDate trong tương lai.
didFinishLaunchingWithOptions thời gian -every hoặc didReceiveLocalNotification được gọi, chỉ cần hủy bỏ thông báo hiện tại của bạn và sắp xếp lại một cái mới với một fireDate một đơn vị khoảng thời gian trong tương lai
-Khi ra mắt ứng dụng của bạn lặp qua tất cả các thông báo lịch trình, nếu fireDate không phải trong tương lai bạn biết rằng nó đã bị bỏ qua
Trong trường hợp của tôi, thông báo có khoảng thời gian lặp lại hàng tuần.đầu tiên tôi sắp xếp lại bất kỳ thông báo được thừa nhận trong didFinishLaunchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification* localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif != nil)
{
[NotificationsHelper rescheduleNotification:localNotif];
}
}
Và cũng trong didReceiveLocalNotification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *) notification
{
[NotificationsHelper rescheduleNotification:notification];
}
Tại App Launch tôi rà soát tất cả các thông báo cho bất kỳ với một fireDate trong quá khứ:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self checkLocalNotifications:application];
}
Mã cho hàm "checkLocalNotifications" của tôi:
- (void) checkLocalNotifications:(UIApplication *) application
{
UIApplication* app = [UIApplication sharedApplication];
NSArray* eventArray = [app scheduledLocalNotifications];
for (int i = 0; i < [eventArray count]; i++)
{
UILocalNotification* notification = [eventArray objectAtIndex:i];
if ([NotificationsHelper wasWeeklyRepeatingNotificationIgnored:notification])
{
[NotificationsHelper rescheduleNotification:notification];
NSLog(@"NotificationWasIgnored: %@ %@",notification.alertAction, notification.alertBody);
}
}
}
Mã cho tôi chức năng "wasWeeklyRepeatingNotificationIgnored":
+ (BOOL) wasWeeklyRepeatingNotificationIgnored:(UILocalNotification*) the_notification
{
BOOL result;
NSDate* now = [NSDate date];
// FireDate is earlier than now
if ([the_notification.fireDate compare:now] == NSOrderedAscending)
{
result = TRUE;
}
else
{
result = FALSE;
}
return result;
}
Mã cho tôi chức năng "rescheduleNotification":
+ (void) rescheduleNotification:(UILocalNotification*) the_notification
{
UILocalNotification* new_notification = [[UILocalNotification alloc] init];
NSMutableDictionary* userinfo = [[NSMutableDictionary alloc] init];
[new_notification setUserInfo:userinfo];
[new_notification setRepeatInterval:the_notification.repeatInterval];
[new_notification setSoundName:UILocalNotificationDefaultSoundName];
[new_notification setTimeZone:[NSTimeZone defaultTimeZone]];
[new_notification setAlertAction:the_notification.alertAction];
[new_notification setAlertBody:the_notification.alertBody];
[new_notification setRepeatCalendar:[NSCalendar currentCalendar]];
[new_notification setApplicationIconBadgeNumber:the_notification.applicationIconBadgeNumber];
NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents* weekdayComponents = [gregorian components:NSWeekdayCalendarUnit
fromDate:the_notification.fireDate];
NSInteger weekday = [weekdayComponents weekday];
NSDate* next_week = [self addDay:weekday toHourMinute:the_notification.fireDate];
[new_notification setFireDate:next_week];
[[UIApplication sharedApplication] scheduleLocalNotification:new_notification];
[[UIApplication sharedApplication] cancelLocalNotification:the_notification];
}
Nếu thông báo của bạn là * không * trong 'scheduledLocalNotifications', nó không còn được lên lịch vì vậy nó phải được kích hoạt. – matt
'vừa chỉnh sửa câu hỏi. Tôi thực sự đã có văn bản trong đó nhưng muốn giữ mọi thứ đơn giản như tôi có thể (nghĩa là ít văn bản hơn). –
Tôi có cảm giác rằng những gì bạn muốn không tồn tại, nhưng điều này sẽ tạo ra một yêu cầu tính năng * tuyệt vời * với phóng viên lỗi của Apple. – matt