FIRIAMDisplayCheckOnFetchDoneNotificationFlow.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2018 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/Flows/FIRIAMDisplayCheckOnFetchDoneNotificationFlow.h"
  21. #import "FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMDisplayExecutor.h"
  22. extern NSString *const kFIRIAMFetchIsDoneNotification;
  23. @implementation FIRIAMDisplayCheckOnFetchDoneNotificationFlow
  24. - (void)start {
  25. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM240001",
  26. @"Start observing fetch done notifications for rendering messages.");
  27. [[NSNotificationCenter defaultCenter] addObserver:self
  28. selector:@selector(fetchIsDone)
  29. name:kFIRIAMFetchIsDoneNotification
  30. object:nil];
  31. }
  32. - (void)checkAndRenderMessage {
  33. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  34. [self.displayExecutor checkAndDisplayNextAppForegroundMessage];
  35. });
  36. }
  37. - (void)fetchIsDone {
  38. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM240002",
  39. @"Fetch is done. Start message rendering flow.");
  40. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 500 * (int64_t)NSEC_PER_MSEC),
  41. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  42. [self checkAndRenderMessage];
  43. });
  44. }
  45. - (void)stop {
  46. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM240003",
  47. @"Stop observing fetch is done notifications.");
  48. [[NSNotificationCenter defaultCenter] removeObserver:self];
  49. }
  50. - (void)dealloc {
  51. [[NSNotificationCenter defaultCenter] removeObserver:self];
  52. }
  53. @end
  54. #endif // TARGET_OS_IOS || TARGET_OS_TV || (defined(TARGET_OS_VISION) && TARGET_OS_VISION)