FIRIAMFetchOnAppForegroundFlow.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 "FIRIAMFetchOnAppForegroundFlow.h"
  19. @implementation FIRIAMFetchOnAppForegroundFlow
  20. - (void)start {
  21. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600002",
  22. @"Start observing app foreground notifications for message fetching.");
  23. [[NSNotificationCenter defaultCenter] addObserver:self
  24. selector:@selector(appWillEnterForeground:)
  25. name:UIApplicationWillEnterForegroundNotification
  26. object:nil];
  27. }
  28. - (void)appWillEnterForeground:(UIApplication *)application {
  29. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600001",
  30. @"App foregrounded, wake up to see if we can fetch in-app messaging.");
  31. // for fetch operation, dispatch it to non main UI thread to avoid blocking. It's ok to dispatch
  32. // to a concurrent global queue instead of serial queue since app open event won't happen at
  33. // fast speed to cause race conditions
  34. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  35. [self checkAndFetchForInitialAppLaunch:NO];
  36. });
  37. }
  38. - (void)stop {
  39. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600003",
  40. @"Stop observing app foreground notifications.");
  41. [[NSNotificationCenter defaultCenter] removeObserver:self];
  42. }
  43. - (void)dealloc {
  44. [[NSNotificationCenter defaultCenter] removeObserver:self];
  45. }
  46. @end