iOS UnuseNotificationCenter didReceiveNotificationResponse仅在后台打开应用程序时运行代码

iOS UnuseNotificationCenter didReceiveNotificationResponse仅在后台打开应用程序时运行代码,ios,objective-c,iphone,push-notification,apple-push-notifications,Ios,Objective C,Iphone,Push Notification,Apple Push Notifications,如果应用程序处于非活动状态或处于后台,是否可以有条件地运行代码 以下是两种情况: 如果应用程序处于后台或非活动状态,我希望运行此代码块 如果应用程序被终止并按通知启动,则应用程序I不想运行此代码块 这是我的密码: // Push notification received in background - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:

如果应用程序处于
非活动状态
或处于
后台
,是否可以有条件地运行代码

以下是两种情况:

  • 如果应用程序处于后台或非活动状态,我希望运行此代码块
  • 如果应用程序被终止并按通知启动,则应用程序I不想运行此代码块
这是我的密码:

// Push notification received in background
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  NSDictionary *userInfo = response.notification.request.content.userInfo;

  [[SEGAnalytics sharedAnalytics] receivedRemoteNotification:userInfo];

  if ([HSBeacon isBeaconPushNotification:userInfo]) {
    UIApplication *applicaiton = [UIApplication sharedApplication];

    // @todo Do not run if app was killed
    if (application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground) {
      NSString *helpScoutBeaconID = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"HelpScoutBeaconID"];
      HSBeaconSettings *settings = [[HSBeaconSettings alloc] initWithBeaconId:helpScoutBeaconID];
      [HSBeacon handlePushNotification:userInfo beaconSettings:settings];
    }
    // End of conditional code
  }

  completionHandler();
}
我试过使用:

    UIApplication *applicaiton = [UIApplication sharedApplication];

    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground) {
      ...
    }
但是,当应用程序被关闭、处于非活动状态或处于后台时,就会触发此命令


谢谢

感觉一定有更好的方法可以做到这一点,但我认为一种可行的方法是保留一个您设置的局部变量:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler;
仅当应用程序当前位于前台时,才会调用该委托,因此如果您具有类似

@property (nonatomic, assign) BOOL receivedWillPresentNotification;
然后,您可能会像这样实现它

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
  // Whatever other logic you have here
  self.receivedWillPresentNotification = YES;
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  NSDictionary *userInfo = response.notification.request.content.userInfo;

  [[SEGAnalytics sharedAnalytics] receivedRemoteNotification:userInfo];

  if ([HSBeacon isBeaconPushNotification:userInfo] && self.receivedWillPresentNotification) {
      NSString *helpScoutBeaconID = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"HelpScoutBeaconID"];
      HSBeaconSettings *settings = [[HSBeaconSettings alloc] initWithBeaconId:helpScoutBeaconID];
      [HSBeacon handlePushNotification:userInfo beaconSettings:settings];
  }

  self.receivedWillPresentNotification = NO;
  completionHandler();
}

还有一个可能更好的解决方案,就是在
applicationdFinishLaunching
中检查通知,然后检查应用程序的状态,以便存储,但我认为您可能会使用其他
UNUserNotificationCenterDelegate
方法作为代理。

谢谢@ScottPetit的回答!我不得不改变一些事情,但我还是能做到:

AppDelegate.h

// Use a local variable to determine if we deeplink to help scout chat
@property (nonatomic, assign) BOOL launchedFromPushNotification;
AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  if (notification) {
    self.launchedFromPushNotification = YES;
  } else {
    self.launchedFromPushNotification = NO;
  }

  ...
}


// Push notification received in foreground
 - (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
  self.launchedFromPushNotification = NO;
}

// Push notification received in background
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  NSDictionary *userInfo = response.notification.request.content.userInfo;

  if ([HSBeacon isBeaconPushNotification:userInfo] && !self.launchedFromPushNotification) {
    NSString *helpScoutBeaconID = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"HelpScoutBeaconID"];
    HSBeaconSettings *settings = [[HSBeaconSettings alloc] initWithBeaconId:helpScoutBeaconID];
    [HSBeacon handlePushNotification:userInfo beaconSettings:settings];

    self.launchedFromPushNotification = NO;
  }

  completionHandler();
}
谢谢你的帮助