我请求通过本地通知为应用程序提供一个提醒功能。用户将设置时间和重复间隔,应用程序将安排相应的通知如下:
UILocalNotification *notification = ...;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];为此,我需要像这样注册通知类型:
UIUserNotificationSettings *notificationSettings;
notificationSettings = [UIUserNotificationSettings
settingsForTypes:UIUserNotificationTypeAlert
categories:nil];
[[UIApplication sharedApplication]
registerUserNotificationSettings:notificationSettings];虽然通知的调度是正确的,但没有调用任何AppDelegate回调(didRegisterUserNotificationSettings、didReceiveLocalNotification)。但是,当应用程序处于前台时,我需要对通知做出反应。
注册通知设置应触发AppDelegate中的回调消息:
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
NSLog(@"Callback called.");
}它适用于一个快速测试项目(New,->,单视图应用程序,->,添加注册代码和回调->启动)。回调会被呼叫。
然而,在真正的应用程序中,它不起作用。
我完全剥夺了我的应用程序AppDelegate (删除自定义init代码,没有ui加载),以便它只是注册通知设置。一个新的应用程序安装会正确触发用户确认对话框。但回调是不会被调用的。
是否缺少构建设置/编译器标志?我不知道任何特别的设置,为应用程序指向那个方向。我看不出这个例子项目有什么不同。
Xcode 7.2,iPod5 iOS9.2.1
发布于 2016-01-27 18:09:08
唉哟。这是一个完美的理由.
为了检测整个应用程序中的某些ui事件,AppDelegate被更改为exdend UIApplication,因此我可以使用- (void)sendEvent:(UIEvent *)event回调。AppDelegate通常应该扩展UIResponder。
您可以在main.m中为这两种用途设置不同的类。
int main(int argc, char *argv[])
{
@autoreleasepool {
@try {
// Original AppDelegate now extending UIResponder
NSString *mainDelegate = NSStringFromClass([AppDelegate class]);
// New class extending UIApplication
NSString *mainClass = NSStringFromClass([AppMain class]);
return UIApplicationMain(argc, argv, mainClass, mainDelegate);
}
@catch (NSException *exception) {
NSLog(@"%@", [exception reason]);
}
}
}我感谢大家的建议。有时候你得后退一步。
https://stackoverflow.com/questions/35043392
复制相似问题