我希望我的应用程序在通过单击通知启动应用程序时执行特定操作。我想在应用程序已经进入后台运行时执行这些特定操作,但也希望在通过单击通知从头开始(未运行到后台)启动应用程序时执行这些特定操作。
当应用程序通过点击通知从后台启动时,我通过以下方式收到通知:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
=> 没问题!
当通过点击通知从头开始应用程序时,我想通过以下方式获取通知:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
}
但是 launchOptions 总是 nil !!当应用程序通过点击应用程序图标(正常)从头开始时为零,当应用程序通过点击通知从头开始(不正常)时也为零。
有人知道如何解决这个问题吗?
谢谢!!!
编辑 1
以下是我的通知是如何创建的(Joe 问题):
NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:notifText,latitudeString,longitudeString,nil]
forKeys:[NSArray arrayWithObjects:@"notifText",@"latitude",@"longitude", nil]];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody =msg;
localNotif.alertAction = @"Ok";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.userInfo = userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
编辑 2(回答我的问题!:))
这是我用来调试我的应用程序的过程,但是...这个过程是错误的!!
- 我使用“编辑方案”菜单中的“等待 MyApp.app 启动”选项设置我的调试器
- 我第一次使用 XCode 启动了我的应用程序(从头开始启动)XCode 显示“正在等待我的应用程序启动”=> 我点击了我的应用程序图标以启动该应用程序
- 应用程序已启动 => 我点击了主页按钮 => 显示了通知
- 我单击了 XCode 中的停止按钮以关闭应用程序 我使用 XCode 重新启动它 => XCode 再次显示“等待我的 MyApp 启动”消息 => 我单击了 状态栏中的通知以启动应用程序
- => launchOptions 为零!
launchOptions 等于 nil 是因为使用 XCode 重新启动应用程序(在本例中使用“等待我的 MyApp 启动”选项)会删除通知,即使它仍然显示在状态栏中......
为了能够在通过单击通知从头开始重新启动应用程序后调试检查 launchOptions 的内容,似乎唯一的方法是在 UIAlert 中显示此内容,如 Tammo 的回答中所述免费。因此,在这种特定情况下使用以下内容进行调试:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return YES;
}
感谢大家的帮助!!!!
最佳答案
我在您分享的代码中找不到错误。通常这应该在模拟器和设备上都有效。这是一个对我有用的示例:首先生成一个新的单 View iPhone 应用程序(打开 ARC 和 Storyboard)。然后修改AppDelegate
中的两个方法如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate dateWithTimeInterval:10.0 sinceDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Just some text";
localNotif.alertAction = @"OK";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.userInfo = @{@"test": @YES};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
然后启动这个应用程序,按主页按钮,然后停止应用程序。如果您点击在按下主页按钮 10 秒后出现的本地通知,您应该看到类似这样的内容,这表明本地通知已传递给 -application:didFinishLaunchingWithOptions:
:
我的建议是:首先使用我在上面发布的示例来确保您的设置没有出现任何问题,然后检查您在代码中所做的不同之处。
编辑
这适用于问题的编辑 2:在等待应用程序启动时(在模拟器中,而不是在设备上),本地通知似乎也对我有用。试试这个:
- 安装上述示例应用并在禁用“等待 MyApp.app 启动”的情况下启动它。
- 点击主页按钮,然后通过 Xcode 或任务栏停止应用。
- 启用“等待 MyApp.app 启动”。
- 如果您现在点击通知中心的通知,它会显示在提醒 View 中。
关于ios - 代码 : why launchOptions in didFinishLaunchingWithOptions always nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13396630/