FIRIAMFetchOnAppForegroundFlow.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #if defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  28. if (@available(iOS 13.0, *)) {
  29. [[NSNotificationCenter defaultCenter] addObserver:self
  30. selector:@selector(appWillEnterForeground:)
  31. name:UISceneWillEnterForegroundNotification
  32. object:nil];
  33. }
  34. #endif // defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  35. }
  36. - (void)appWillEnterForeground:(NSNotification *)notification {
  37. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600001",
  38. @"App foregrounded, wake up to see if we can fetch in-app messaging.");
  39. // for fetch operation, dispatch it to non main UI thread to avoid blocking. It's ok to dispatch
  40. // to a concurrent global queue instead of serial queue since app open event won't happen at
  41. // fast speed to cause race conditions
  42. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
  43. [self checkAndFetchForInitialAppLaunch:NO];
  44. });
  45. }
  46. - (void)stop {
  47. FIRLogDebug(kFIRLoggerInAppMessaging, @"I-IAM600003",
  48. @"Stop observing app foreground notifications.");
  49. [[NSNotificationCenter defaultCenter] removeObserver:self];
  50. }
  51. - (void)dealloc {
  52. [[NSNotificationCenter defaultCenter] removeObserver:self];
  53. }
  54. @end