14

我希望我的应用程序在通过单击通知启动应用程序时执行特定操作。当应用程序已经在后台运行时,我想执行这些特定的事情,但是当应用程序通过单击通知从 SCRATCH (未运行到后台)启动时,我也想执行这些特定的操作。

通过单击通知从后台启动应用程序时,我通过以下方式收到通知:

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

=> 没问题!

通过单击通知从头开始启动应用程序时,我想通过以下方式获取通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

     UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

}

但launchOptions 始终为零!当应用程序通过单击应用程序图标(正常)从头开始启动时,以及当应用程序通过单击通知从头开始启动时(不正常),它为零。

有人知道如何解决这个问题吗?

谢谢 !!!

编辑 1

这是我的通知的创建方式(乔问题):

  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(回答我的问题!:))

这是我用来调试我的应用程序的过程,但是......这个过程是错误的!

  1. 我使用“编辑方案”菜单中的“等待 MyApp.app 启动”选项设置调试器
  2. 我第一次使用 XCode 启动了我的应用程序(从头开始启动)XCode 显示“等待我的 MyApp 启动”=> 我点击了我的应用程序图标以启动应用程序
  3. 应用程序启动 => 我点击了主页按钮 => 显示通知
  4. 我点击了 XCode 中的停止按钮以关闭应用程序我用 XCode 重新启动它 => XCode 再次显示“等待我的 MyApp 启动”消息 => 我点击状态栏中的通知以启动应用程序
  5. => launchOptions 为零!

launchOptions 等于 nil 是因为使用 XCode 重新启动应用程序(在这种情况下使用“等待我的 MyApp 启动”选项)会删除通知,即使它仍然显示在状态栏中...

为了能够通过单击通知从头开始重新启动应用程序后调试检查 launchOptions 的内容,似乎唯一的方法是在 UIAlert 中显示此内容,如 Tammo Freese 的回答中所述。因此,在这种特定情况下使用以下命令进行调试:

- (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;
}

感谢你的帮助 !!!!

4

4 回答 4

19

我在您共享的代码中找不到错误。通常这应该在模拟器和设备上都有效。这是一个对我有用的示例:首先生成一个新的 Single View iPhone 应用程序(ARC 和 Storyboards 上)。然后更改以下两个方法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:

iPhone 模拟器的屏幕截图显示了一个显示本地通知的警报视图

我的建议是:首先让我在上面发布的示例工作以确保您的设置没有出错,然后检查您在代码中所做的不同之处。

编辑

这适用于问题的编辑 2:本地通知在等待应用程序启动时似乎也适用于我(在模拟器中,而不是在设备上)。试试这个:

  1. 安装上述示例应用程序并在禁用“等待 MyApp.app 启动”的情况下启动它。
  2. 单击主页按钮,然后通过 Xcode 或任务栏停止应用程序。
  3. 启用“等待 MyApp.app 启动”。
  4. 如果您现在点击通知中心的通知,它会显示在警报视图中。
于 2012-11-25T17:16:02.687 回答
1

不知道这是否是您要寻找的,但是您是否缺少“返回是;”?因为我用它来从通知执行到新视图的转换,它工作正常

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navigation = (UINavigationController *) self.window.rootViewController;

    [navigation.visibleViewController performSegueWithIdentifier:@"Ident" sender:nil];

    return YES;
}
于 2012-11-25T08:43:28.313 回答
1

我的结论和建议是否可以帮助任何人,请参阅下文

每次您有新版本来测试您的应用程序时,您都必须使用最新应用程序生成的通知来测试通知点击操作。如果您继续使用旧版本生成的旧通知测试点击操作,那么它将表现出意外(意味着它能够以某种方式启动应用程序,但不会在 didFinishLaunchingWithOptions 中返回任何有效信息:)

于 2014-10-14T17:43:54.047 回答
0

请原谅我,当谈到 Apple 的推送通知服务时,我还是新手,但我阅读了他们的文档,我的猜测是在创建本地通知时可能存在导致问题的问题。

我会仔细检查您是如何使用 Apple 的示例创建本地通知的,如清单 2-1 所示只是为了排除这种可能性。由于用于创建本地通知的某些代码未在此线程中显示给我们,因此很难评估本地通知的实例化是否确实正确。它最终可能很简单,例如开火日期有问题或通知中的其他内容设置不正确。

于 2012-11-19T16:21:30.387 回答