FIRIAMDisplayCheckOnAppForegroundFlow.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/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 (@available(iOS 13.0, tvOS 13.0, *)) {
  32. [[NSNotificationCenter defaultCenter]
  33. addObserver:self
  34. selector:@selector(checkAndDisplayNextAppForegroundMessageFromForeground:)
  35. name:UISceneWillEnterForegroundNotification
  36. object:nil];
  37. }
  38. }
  39. - (void)checkAndDisplayNextAppForegroundMessageFromForeground:(NSNotification *)notification {
  40. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500001",
  41. @"App foregrounded, wake up to check in-app messaging.");
  42. // Show the message with 0.5 second delay so that the app's UI is more stable.
  43. // When messages are displayed, the UI operation will be dispatched back to main UI thread.
  44. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 500 * (int64_t)NSEC_PER_MSEC),
  45. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  46. [self.displayExecutor checkAndDisplayNextAppForegroundMessage];
  47. });
  48. }
  49. - (void)stop {
  50. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500004",
  51. @"Stop observing app foreground notifications.");
  52. [[NSNotificationCenter defaultCenter] removeObserver:self];
  53. }
  54. - (void)dealloc {
  55. [[NSNotificationCenter defaultCenter] removeObserver:self];
  56. }
  57. @end
  58. #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION