我正在通过本地通知为应用程序实现提醒功能。 用户将设置时间和重复间隔,应用程序将安排相应的通知,如下所示:
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.");
}
它适用于快速测试项目(新项目 -> 单 View 应用 -> 添加注册代码和回调 -> 启动)。回调被调用。
然而,在真实的应用程序中它不起作用。
我完全剥离了我的应用程序 AppDelegate(删除了自定义初始化代码,没有 ui 加载),以便它只注册通知设置。全新的应用程序安装会正确触发用户确认对话框。但是回调不会被调用。
我是否缺少build设置/编译器标志?我不知道该应用程序指向该方向的任何特殊设置。我看不出示例项目有任何不同。
Xcode 7.2 , iPod5 iOS9.2.1
最佳答案
哎呀。这是捏脸的完美理由...
为了检测整个应用程序中的某些 ui 事件,AppDelegate 被更改为扩展 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]);
}
}
}
感谢大家的建议。 有时您必须退后一步。
关于iOS 通知 : callbacks not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35043392/