Ios 应用程序代表';s应用程序:didReceiveLocalNotification:未调用
在我的应用程序委托中,永远不会调用方法Ios 应用程序代表';s应用程序:didReceiveLocalNotification:未调用,ios,objective-c,uilocalnotification,appdelegate,Ios,Objective C,Uilocalnotification,Appdelegate,在我的应用程序委托中,永远不会调用方法-(void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification 以下是我创建通知的方式: UILocalNotification *notification = [[UILocalNotification alloc] init]; // set UUID, which we will store in
-(void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification
以下是我创建通知的方式:
UILocalNotification *notification = [[UILocalNotification alloc] init];
// set UUID, which we will store in userDefaults for later
NSMutableDictionary *myUserInfo = [[NSMutableDictionary alloc] init];
NSString *uuid = [[NSProcessInfo processInfo] globallyUniqueString];
[myUserInfo setValue:uuid forKey:KEY_UUID];
[myUserInfo setValue:@"month" forKey:KEY_UNIT];
[myUserInfo setObject:@YES forKey:KEY_RESCHEDULE];
NSInteger row = [_wurmProphylaxePickerView selectedRowInComponent:0];
switch (row) {
case 0:
[myUserInfo setValue:@2 forKey:KEY_FREQUENCY];
break;
case 1:
[myUserInfo setValue:@4 forKey:KEY_FREQUENCY];
break;
case 2:
[myUserInfo setValue:@6 forKey:KEY_FREQUENCY];
break;
default:
[myUserInfo setValue:@4 forKey:KEY_FREQUENCY];
break;
}
notification.userInfo = myUserInfo;
// calculate date for next notification, depends on the user's selection
NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *myComps = [[NSDateComponents alloc] init];
[myComps setMinute:1];
notification.fireDate = [calendar dateByAddingComponents:myComps toDate:today options:0];
notification.timeZone = [NSTimeZone localTimeZone];
notification.alertBody = @"My alertBody";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
这在我的应用程序代理中,但从未调用:
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
BOOL repeat = [[userInfo objectForKey:KEY_RESCHEDULE] boolValue];
if (repeat)
{
NSInteger frequency = (NSInteger)[userInfo objectForKey:KEY_FREQUENCY];
NSString *unit = (NSString *)[userInfo objectForKey:KEY_UNIT];
NSString *uuid = (NSString *)[userInfo objectForKey:KEY_UUID];
// calculate date for next notification
NSDate *today = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *myComps = [[NSDateComponents alloc] init];
if ([unit isEqualToString:@"month"]) {
//[myComps setMonth:frequency];
[myComps setMinute:frequency];
} else {
}
// create new notification
UILocalNotification *newNotification = [[UILocalNotification alloc] init];
newNotification.fireDate = [calendar dateByAddingComponents:myComps toDate:today options:0];
newNotification.timeZone = [NSTimeZone localTimeZone];
newNotification.alertAction = notification.alertAction;
newNotification.alertBody = notification.alertBody;
newNotification.userInfo = notification.userInfo;
// schedule it
[[UIApplication sharedApplication] scheduleLocalNotification:newNotification];
}
}
在iOS 8上测试,不确定iOS 7…如果应用程序在通知触发时未处于活动状态,您可以在
didFinishLaunchingWithOptions
中处理此问题,如下例所示:
如果启动通知时应用程序处于活动状态,则调用
didReceiveLocalNotification
。您是否先注册通知?请参阅。是的,我有,权限警报显示,通知显示,但委托方法未调用。。。当收到此通知时,应用程序位于前台?不,它位于后台OK。我懂了。如果你把它作为答案贴出来,我会接受的。tx Rob。但是,如果应用程序在后台,并且通知显示给用户,但用户没有点击它,则会发生什么情况?如果用户选择在应用程序未运行时不单击本地通知,则不会调用任何方法。从开发人员的角度来看,这有点烦人,但从最终用户的角度来看,这很有意义。因此,如果我想指出点击本地通知打开应用程序的事实,我应该将其放在“didFinishLaunchingWithOptions”中,对吗?是的,除非你有更新的自定义操作按钮,在这种情况下,将调用handleActionWithIdentifier
。如果应用程序已经在运行,则调用didReceiveLocalNotification。另外,请记住使用registerUserNotificationSettings
注册本地通知。我在回答中提供的链接中描述了所有这些。
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
[viewController displayItem:itemName]; // custom method
app.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}