FIRIAMDisplayCheckOnFetchDoneNotificationFlow.m 2.1 KB

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