Ios 应用程序被UILocalNotification唤醒(活动)

Ios 应用程序被UILocalNotification唤醒(活动),ios,objective-c,uilocalnotification,Ios,Objective C,Uilocalnotification,我用UILocalNotification提示用户。当他从通知启动我的应用程序时,如果它在后台,我的应用程序将变为活动状态。我如何知道应用程序已从通知中激活?您必须处理两种情况 应用程序未运行 在非运行状态下,当应用程序收到通知时,将启动应用程序。在application didFinishLaunchingWithOptions中:您可以检查应用程序是否因通知而启动 UILocalNotification *notification = [launchOptions objectForKey:

我用UILocalNotification提示用户。当他从通知启动我的应用程序时,如果它在后台,我的应用程序将变为活动状态。我如何知道应用程序已从通知中激活?

您必须处理两种情况

应用程序未运行 在非运行状态下,当应用程序收到通知时,将启动应用程序。在application didFinishLaunchingWithOptions中:您可以检查应用程序是否因通知而启动

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if (notification)
{
  // application launched due to notification
}
在后台运行的应用程序 在这种情况下,如果您的appDelegate实现此方法,您将在application didReceiveLocalNotification上得到一个调用

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

}

有两件事你必须处理

应用程序未运行 在非运行状态下,当应用程序收到通知时,将启动应用程序。在application didFinishLaunchingWithOptions中:您可以检查应用程序是否因通知而启动

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if (notification)
{
  // application launched due to notification
}
在后台运行的应用程序 在这种情况下,如果您的appDelegate实现此方法,您将在application didReceiveLocalNotification上得到一个调用

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

}
视情况而定

如果您的应用程序根本没有运行,则将启动该应用程序。调用-application:didFinishLaunchingWithOptions:时,选项字典将包含UIApplicationLaunchActionSlocalNotificationKey,该值为UILocalNotification。因此,UIApplicationLaunchActionSlocalNotificationKey的存在与否可以告诉您应用程序是否是通过响应本地通知启动的

如果收到通知时应用程序已经在运行,则将调用-application:didReceiveLocalNotification:方法。注意,如果您的应用程序位于前台或后台,则会调用此函数。因此,检查应用程序状态:如果状态为UIApplicationStateActive,则该应用程序处于活动状态且位于前端;如果UIApplicationStateInactive,则用户点击操作按钮以响应通知。

视情况而定

如果您的应用程序根本没有运行,则将启动该应用程序。调用-application:didFinishLaunchingWithOptions:时,选项字典将包含UIApplicationLaunchActionSlocalNotificationKey,该值为UILocalNotification。因此,UIApplicationLaunchActionSlocalNotificationKey的存在与否可以告诉您应用程序是否是通过响应本地通知启动的

如果收到通知时应用程序已经在运行,则将调用-application:didReceiveLocalNotification:方法。注意,如果您的应用程序位于前台或后台,则会调用此函数。因此,检查应用程序状态:如果状态为UIApplicationStateActive,则该应用程序处于活动状态且位于前端;如果UIApplicationStateInactive,则用户点击操作按钮以响应通知