FIRIAMFetchOnAppForegroundFlow.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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
  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 defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  30. if (@available(iOS 13.0, tvOS 13.0, *)) {
  31. [[NSNotificationCenter defaultCenter] addObserver:self
  32. selector:@selector(appWillEnterForeground:)
  33. name:UISceneWillEnterForegroundNotification
  34. object:nil];
  35. }
  36. #endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  37. }
  38. - (void)appWillEnterForeground:(NSNotification *)notification {
  39. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600001",
  40. @"App foregrounded, wake up to see if we can fetch in-app messaging.");
  41. // for fetch operation, dispatch it to non main UI thread to avoid blocking. It's ok to dispatch
  42. // to a concurrent global queue instead of serial queue since app open event won't happen at
  43. // fast speed to cause race conditions
  44. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  45. [self checkAndFetchForInitialAppLaunch:NO];
  46. });
  47. }
  48. - (void)stop {
  49. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600003",
  50. @"Stop observing app foreground notifications.");
  51. [[NSNotificationCenter defaultCenter] removeObserver:self];
  52. }
  53. - (void)dealloc {
  54. [[NSNotificationCenter defaultCenter] removeObserver:self];
  55. }
  56. @end
  57. #endif // TARGET_OS_IOS || TARGET_OS_TV