当app状态为UIApplicationStateInactive时,如何处理通知

时间:2016-07-08 06:03:15

标签: ios objective-c uilocalnotification

我正在使用以下代码来处理推送通知。

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{               
     if(application.applicationState == UIApplicationStateInactive) 
     {
          //************************************************************
          // I only want this called when user click on notification.
          //************************************************************

          NSLog(@"Inactive");
          if ([[userInfo valueForKey:@"noty_type"] isEqualToString:@"web"])
          {
                if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:[userInfo valueForKey:@"url"]]])
                {
                      dispatch_async(dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[userInfo valueForKey:@"url"]]];
                                    });
                }
          }
     }

     if((application.applicationState == UIApplicationStateActive)
     {
          // I am useing local notification when app in Forground
          UILocalNotification* localNotification = [[UILocalNotification alloc] init];
          localNotification.soundName =  @"Default";
          localNotification.alertBody = [userInfo valueForKey:@"msg"];
          localNotification.userInfo = userInfo;
          [[UIApplication sharedApplication] scheduleLocalNotification: localNotification];
     }
}

此代码在safari浏览器中打开url。此代码正常工作当我的应用程序在后台和通知中来,我点击通知检查applicationState并打开带有[[UIApplication sharedApplication] openURL的网址。

现在是产生问题的方案。

  1. 我打开我的应用程序。

  2. 并关闭通知用户界面

  3. enter image description here

    1. 现在从服务器发送通知

    2. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler方法被调用。

    3. 现在我得到ApplicationState == UIApplicationStateInactive

    4. “它在没有任何用户交互的情况下打开Safari浏览器”。

      I only want this called when user click on notification.
      

      那么,我该如何处理这种情况?

3 个答案:

答案 0 :(得分:0)

试试这个。

if (application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground)
{
    // do something when app open with notification
}

答案 1 :(得分:0)

您必须知道该应用为何无效。两种可能性是应用程序处于后台并且用户将其带到前台(通过点击通知横幅),或者通知中心被拉下或用户获得系统警报。

要说明差异,请跟踪用户何时进入后台。只需在applicationDidEnterBackground:委托方法中设置一个布尔值,并在应用程序重新启动时清除它。

static BOOL didEnterBackground;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Launching the app is kind of like coming from background.
    didEnterEnterBackground = YES;

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    didEnterBackground = NO;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    didEnterBackground = YES;
}

现在,您可以检查变量didEnterBackground,以确定在处理通知时应用如何进入非活动状态。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    if (application.applicationState == UIApplicationStateInactive) {
        if (didEnterBackground) {
            // The user tapped the notification
        }
        else {
            // The app was inactive, but not in the background.
            // Ignore notification, or do whatever the app does
            // when receiving a notification while active.
        }
    }
}

答案 2 :(得分:0)

希望这能帮到你

-(void)application:(UIApplication *)application
 didReceiveRemoteNotification:(NSDictionary *)userInfo 
 fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
 {
 }//iOS below 9

- (void)application:(UIApplication *)application 
  handleActionWithIdentifier:(nullable NSString *)identifier 
  forLocalNotification:(nonnull UILocalNotification *)notification 
  completionHandler:(nonnull void (^)())completionHandler {


// handling local and interactive notifcation 

 } // iOS 9 and above

用于在后台状态下触发通知。