从锁定屏幕处理“滑动以查看”本地通知

时间:2013-12-30 22:38:40

标签: ios objective-c notifications

我正在尝试在iOS应用程序中处理本地通知。我已经阅读了以下文件:

Scheduling, Registering, and Handling Notifications

以下是生成通知的代码:

UILocalNotification *notify = [[UILocalNotification alloc] init];
notify.alertBody = @"Alert Title";
notify.alertAction = @"view";
notify.repeatInterval = 0;
notify.soundName = UILocalNotificationDefaultSoundName;
notify.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] presentLocalNotificationNow:notify];

当应用程序在后台时,即在调用applicationDidEnterBackground:函数之后执行此代码。通知创建得很好,我可以在锁定屏幕上看到它,但是执行操作 - slide to view似乎没有像我期望的那样工作。

根据文件处理,通知应致电application:didFinishLaunchingWithOptions:。但是,当我滑动查看时,实际上会调用applicationWillEnterForeground:

有人可以指出我正确的方向吗?我显然做错了什么,我似乎无法弄清楚是什么。

2 个答案:

答案 0 :(得分:2)

仅当您的应用尚未启动时,才会调用

application:didFinishLaunchingWithOptions:。虽然options确实会包含有关通知的信息,但最终会启动应用,但您想要的是在此处理本地通知逻辑:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

来源:Apple docs

答案 1 :(得分:0)

看起来执行状态applicationWillEnterForeground被调用。我认为你不会再看到application:didFinishLaunchingWithOptions:

关闭设备的App负载:

application:didFinishLaunchingWithOptions:
applicationDidBecomeActive:

按下主页按钮:

applicationWillResignActive:
applicationDidEnterBackground:

重新输入app:

applicationWillEnterForeground:
applicationDidBecomeActive: