FIRIAMFetchOnAppForegroundFlow.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION
  18. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  19. #import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
  20. #import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMFetchOnAppForegroundFlow.h"
  21. @implementation FIRIAMFetchOnAppForegroundFlow
  22. - (void)start {
  23. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600002",
  24. @"Start observing app foreground notifications for message fetching.");
  25. [[NSNotificationCenter defaultCenter] addObserver:self
  26. selector:@selector(appWillEnterForeground:)
  27. name:UIApplicationWillEnterForegroundNotification
  28. object:nil];
  29. if (@available(iOS 13.0, tvOS 13.0, *)) {
  30. [[NSNotificationCenter defaultCenter] addObserver:self
  31. selector:@selector(appWillEnterForeground:)
  32. name:UISceneWillEnterForegroundNotification
  33. object:nil];
  34. }
  35. }
  36. - (void)appWillEnterForeground:(NSNotification *)notification {
  37. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600001",
  38. @"App foregrounded, wake up to see if we can fetch in-app messaging.");
  39. // for fetch operation, dispatch it to non main UI thread to avoid blocking. It's ok to dispatch
  40. // to a concurrent global queue instead of serial queue since app open event won't happen at
  41. // fast speed to cause race conditions
  42. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  43. [self checkAndFetchForInitialAppLaunch:NO];
  44. });
  45. }
  46. - (void)stop {
  47. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600003",
  48. @"Stop observing app foreground notifications.");
  49. [[NSNotificationCenter defaultCenter] removeObserver:self];
  50. }
  51. - (void)dealloc {
  52. [[NSNotificationCenter defaultCenter] removeObserver:self];
  53. }
  54. @end
  55. #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION