App Delegate 的应用程序:didReceiveLocalNotification:未调用
Posted
技术标签:
【中文标题】App Delegate 的应用程序:didReceiveLocalNotification:未调用【英文标题】:App Delegate's application:didReceiveLocalNotification: not called 【发布时间】:2015-01-21 17:20:31 【问题描述】:在我的应用委托中,永远不会调用方法 - (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...
【问题讨论】:
您是否先注册了通知?见Registering for Notification Types in iOS。 是的,我愿意,并且显示权限警报,并显示通知,但没有调用委托方法 ...当这个通知进来时,应用程序在前台? 不,它在后台 好的。我知道了。如果您将其发布为答案,我会接受。 TX Rob。 【参考方案1】:如果在通知触发时应用程序未处于活动状态,您将在didFinishLaunchingWithOptions
中处理此问题,如Local and Push Notifications Programming Guide 的处理本地和远程通知 部分中的此示例所示: p>
- (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;
如果在触发通知时应用处于活动状态,则调用 didReceiveLocalNotification
。
【讨论】:
但是会发生什么,如果应用程序在后台,并且通知显示给用户,但用户没有点击它,应用程序委托中没有调用方法?跨度> 如果用户选择在应用未运行时不点击本地通知,则不会调用任何方法。从开发人员的角度来看,这有点烦人,但从最终用户的角度来看,这很有意义。 因此,如果我想检测单击 localNotification 以打开应用程序的事实,我应该将其放在“didFinishLaunchingWithOptions”中,对吗? 是的,除非您有更新的自定义操作按钮,在这种情况下会调用handleActionWithIdentifier
。如果应用程序已经在运行,则调用 didReceiveLocalNotification
。另外,请记住使用registerUserNotificationSettings
注册本地通知。所有这些都在我的答案中提供的链接中进行了描述。以上是关于App Delegate 的应用程序:didReceiveLocalNotification:未调用的主要内容,如果未能解决你的问题,请参考以下文章
Cocoa 应用程序 - XCode 8 和 App Delegate
从 Xcode 11 更新到 12 后的 App Delegate/Scene Delegate 和 Firebase 问题 [关闭]