FIRIAMDisplayCheckOnAppForegroundFlow.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)
  18. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  19. #import "FirebaseInAppMessaging/Sources/FIRCore+InAppMessaging.h"
  20. #import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMDisplayCheckOnAppForegroundFlow.h"
  21. #import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMDisplayExecutor.h"
  22. @implementation FIRIAMDisplayCheckOnAppForegroundFlow
  23. - (void)start {
  24. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500002",
  25. @"Start observing app foreground notifications for rendering messages.");
  26. [[NSNotificationCenter defaultCenter]
  27. addObserver:self
  28. selector:@selector(checkAndDisplayNextAppForegroundMessageFromForeground:)
  29. name:UIApplicationWillEnterForegroundNotification
  30. object:nil];
  31. #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  32. if (@available(iOS 13.0, tvOS 13.0, *)) {
  33. [[NSNotificationCenter defaultCenter]
  34. addObserver:self
  35. selector:@selector(checkAndDisplayNextAppForegroundMessageFromForeground:)
  36. name:UISceneWillEnterForegroundNotification
  37. object:nil];
  38. }
  39. #endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  40. }
  41. - (void)checkAndDisplayNextAppForegroundMessageFromForeground:(NSNotification *)notification {
  42. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500001",
  43. @"App foregrounded, wake up to check in-app messaging.");
  44. // Show the message with 0.5 second delay so that the app's UI is more stable.
  45. // When messages are displayed, the UI operation will be dispatched back to main UI thread.
  46. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 500 * (int64_t)NSEC_PER_MSEC),
  47. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  48. [self.displayExecutor checkAndDisplayNextAppForegroundMessage];
  49. });
  50. }
  51. - (void)stop {
  52. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500004",
  53. @"Stop observing app foreground notifications.");
  54. [[NSNotificationCenter defaultCenter] removeObserver:self];
  55. }
  56. - (void)dealloc {
  57. [[NSNotificationCenter defaultCenter] removeObserver:self];
  58. }
  59. @end
  60. #endif // TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)