我正在实施本地通知方法,但我收到了警告
Warning: Application delegate received call to -application:handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler: but the completion handler was never called.
这是我在didfinishlaunchmethod
中的代码if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIMutableUserNotificationAction *snooze=[[UIMutableUserNotificationAction alloc] init];
[snooze setActivationMode:UIUserNotificationActivationModeBackground];
[snooze setTitle:@"Snooze"];
[snooze setIdentifier:NotificationActionOneIdent];
[snooze setDestructive:NO];
[snooze setAuthenticationRequired:YES];
UIMutableUserNotificationAction *cancel=[[UIMutableUserNotificationAction alloc] init];
[cancel setActivationMode:UIUserNotificationActivationModeBackground];
[cancel setTitle:@"Cancel"];
[cancel setIdentifier:NotificationActionTwoIdent];
[cancel setDestructive:NO];
[cancel setAuthenticationRequired:YES];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:@[cancel, snooze]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
这里是完成处理程序的代码
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
NSLog(@"called");
if ([identifier isEqualToString:NotificationActionOneIdent]) {
NSLog(@"You choose snooze");
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = [[NSDate date] dateByAddingTimeInterval:900];
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Time to wake up";
localNotif.repeatInterval=kCFCalendarUnitMinute;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
else if ([identifier isEqualToString:NotificationActionTwoIdent]) {
NSLog(@"You choose cancel");
}
completionHandler();
}
但我一次又一次警告同样的警告..请帮我解决这个问题。
答案 0 :(得分:0)
你在handle函数中有一个退出点,它不会调用completionHandler()
if (localNotif == nil) return;
解决此问题,错误应该消失。
if (localNotif != nil) {
NSDate *fireTime = [[NSDate date] dateByAddingTimeInterval:900];
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Time to wake up";
localNotif.repeatInterval=kCFCalendarUnitMinute;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}