如果应用在后台打开,iOS UNUserNotificationCenter didReceiveNotificationResponse仅运行代码

问题描述

如果应用是inactive或在background中,是否可以有条件地运行代码

这是两种情况:

  • 如果该应用处于后台或处于非活动状态,我运行此代码块。
  • 如果该应用被终止并按通知启动了该应用,我不想要运行此代码块。

这是我的代码

// 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) {
      ...
    }

但是,当应用程序被杀死,处于非活动状态或在backgorund中时,就会触发该事件。

谢谢

解决方法

感觉必须要有更好的方法,但是我认为可行的一种方法是保留您在其中设置的局部变量:

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

同样,可能存在一个更好的解决方案,涉及检查applicationDidFinishLaunching中的通知并检查在那里可以存储的应用程序的状态,但是我认为您可以使用其他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();
}

感谢您的所有帮助!