FIRIAMDisplayCheckOnAppForegroundFlow.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  17. #import "FIRCore+InAppMessaging.h"
  18. #import "FIRIAMDisplayCheckOnAppForegroundFlow.h"
  19. #import "FIRIAMDisplayExecutor.h"
  20. @implementation FIRIAMDisplayCheckOnAppForegroundFlow
  21. - (void)start {
  22. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500002",
  23. @"Start observing app foreground notifications for rendering messages.");
  24. [[NSNotificationCenter defaultCenter]
  25. addObserver:self
  26. selector:@selector(checkAndDisplayNextAppForegroundMessageFromForeground:)
  27. name:UIApplicationWillEnterForegroundNotification
  28. object:nil];
  29. #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  30. if (@available(iOS 13.0, *)) {
  31. [[NSNotificationCenter defaultCenter]
  32. addObserver:self
  33. selector:@selector(checkAndDisplayNextAppForegroundMessageFromForeground:)
  34. name:UISceneWillEnterForegroundNotification
  35. object:nil];
  36. }
  37. #endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  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