FIRIAMDisplayCheckOnAppForegroundFlow.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/FIRLogger.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] addObserver:self
  25. selector:@selector(appWillEnterForeground:)
  26. name:UIApplicationWillEnterForegroundNotification
  27. object:nil];
  28. }
  29. - (void)appWillEnterForeground:(UIApplication *)application {
  30. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500001",
  31. @"App foregrounded, wake up to check in-app messaging.");
  32. // Show the message with 0.5 second delay so that the app's UI is more stable.
  33. // When messages are displayed, the UI operation will be dispatched back to main UI thread.
  34. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 500 * (int64_t)NSEC_PER_MSEC),
  35. dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  36. [self.displayExecutor checkAndDisplayNextAppForegroundMessage];
  37. });
  38. }
  39. - (void)stop {
  40. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM500004",
  41. @"Stop observing app foreground notifications.");
  42. [[NSNotificationCenter defaultCenter] removeObserver:self];
  43. }
  44. - (void)dealloc {
  45. [[NSNotificationCenter defaultCenter] removeObserver:self];
  46. }
  47. @end