FIRMessagingReceiver.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "FIRMessagingReceiver.h"
  17. #import <UIKit/UIKit.h>
  18. #import "FIRMessaging.h"
  19. #import "FIRMessagingLogger.h"
  20. #import "FIRMessagingUtilities.h"
  21. #import "FIRMessaging_Private.h"
  22. static NSString *const kUpstreamMessageIDUserInfoKey = @"messageID";
  23. static NSString *const kUpstreamErrorUserInfoKey = @"error";
  24. // Copied from Apple's header in case it is missing in some cases.
  25. #ifndef NSFoundationVersionNumber_iOS_9_x_Max
  26. #define NSFoundationVersionNumber_iOS_9_x_Max 1299
  27. #endif
  28. static int downstreamMessageID = 0;
  29. @implementation FIRMessagingReceiver
  30. #pragma mark - FIRMessagingDataMessageManager protocol
  31. - (void)didReceiveMessage:(NSDictionary *)message withIdentifier:(nullable NSString *)messageID {
  32. if (![messageID length]) {
  33. messageID = [[self class] nextMessageID];
  34. }
  35. if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max) {
  36. // Use delegate method for iOS 10
  37. [self scheduleIos10NotificationForMessage:message withIdentifier:messageID];
  38. } else {
  39. // Post notification directly to AppDelegate handlers. This is valid pre-iOS 10.
  40. [self scheduleNotificationForMessage:message];
  41. }
  42. }
  43. - (void)willSendDataMessageWithID:(NSString *)messageID error:(NSError *)error {
  44. NSNotification *notification;
  45. if (error) {
  46. NSDictionary *userInfo = @{
  47. kUpstreamMessageIDUserInfoKey : [messageID copy],
  48. kUpstreamErrorUserInfoKey : error
  49. };
  50. notification = [NSNotification notificationWithName:FIRMessagingSendErrorNotification
  51. object:nil
  52. userInfo:userInfo];
  53. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  54. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver000,
  55. @"Fail to send upstream message: %@ error: %@", messageID, error);
  56. } else {
  57. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver001, @"Will send upstream message: %@",
  58. messageID);
  59. }
  60. }
  61. - (void)didSendDataMessageWithID:(NSString *)messageID {
  62. // invoke the callbacks asynchronously
  63. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver002, @"Did send upstream message: %@",
  64. messageID);
  65. NSNotification * notification =
  66. [NSNotification notificationWithName:FIRMessagingSendSuccessNotification
  67. object:nil
  68. userInfo:@{ kUpstreamMessageIDUserInfoKey : [messageID copy] }];
  69. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  70. }
  71. - (void)didDeleteMessagesOnServer {
  72. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeReceiver003,
  73. @"Will send deleted messages notification");
  74. NSNotification * notification =
  75. [NSNotification notificationWithName:FIRMessagingMessagesDeletedNotification
  76. object:nil];
  77. [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostASAP];
  78. }
  79. #pragma mark - Private Helpers
  80. // As the new UserNotifications framework in iOS 10 doesn't support constructor/mutation for
  81. // UNNotification object, FCM can't inject the message to the app with UserNotifications framework.
  82. // Define our own protocol, which means app developers need to implement two interfaces to receive
  83. // display notifications and data messages respectively for devices running iOS 10 or above. Devices
  84. // running iOS 9 or below are not affected.
  85. - (void)scheduleIos10NotificationForMessage:(NSDictionary *)message
  86. withIdentifier:(NSString *)messageID {
  87. FIRMessagingRemoteMessage *wrappedMessage = [[FIRMessagingRemoteMessage alloc] init];
  88. // TODO: wrap title, body, badge and other fields
  89. wrappedMessage.appData = [message copy];
  90. [self.delegate receiver:self receivedRemoteMessage:wrappedMessage];
  91. }
  92. - (void)scheduleNotificationForMessage:(NSDictionary *)message {
  93. SEL newNotificationSelector =
  94. @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:);
  95. SEL oldNotificationSelector = @selector(application:didReceiveRemoteNotification:);
  96. dispatch_async(dispatch_get_main_queue(), ^{
  97. UIApplication *application = FIRMessagingUIApplication();
  98. if (!application) {
  99. return;
  100. }
  101. id<UIApplicationDelegate> appDelegate = [application delegate];
  102. if ([appDelegate respondsToSelector:newNotificationSelector]) {
  103. // Try the new remote notification callback
  104. [appDelegate application:application
  105. didReceiveRemoteNotification:message
  106. fetchCompletionHandler:^(UIBackgroundFetchResult result) {
  107. }];
  108. } else if ([appDelegate respondsToSelector:oldNotificationSelector]) {
  109. // Try the old remote notification callback
  110. #pragma clang diagnostic push
  111. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  112. [appDelegate application:application didReceiveRemoteNotification:message];
  113. #pragma clang diagnostic pop
  114. } else {
  115. FIRMessagingLoggerError(kFIRMessagingMessageCodeReceiver005,
  116. @"None of the remote notification callbacks implemented by "
  117. @"UIApplicationDelegate");
  118. }
  119. });
  120. }
  121. + (NSString *)nextMessageID {
  122. @synchronized (self) {
  123. ++downstreamMessageID;
  124. return [NSString stringWithFormat:@"gcm-%d", downstreamMessageID];
  125. }
  126. }
  127. @end