FIRMessagingContextManagerService.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 "FirebaseMessaging/Sources/FIRMessagingContextManagerService.h"
  17. #import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
  18. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  19. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  20. #import <GoogleUtilities/GULAppDelegateSwizzler.h>
  21. #define kFIRMessagingContextManagerPrefixKey @"google.c.cm."
  22. #define kFIRMessagingContextManagerNotificationKeyPrefix @"gcm.notification."
  23. static NSString *const kLogTag = @"FIRMessagingAnalytics";
  24. static NSString *const kLocalTimeFormatString = @"yyyy-MM-dd HH:mm:ss";
  25. static NSString *const kContextManagerPrefixKey = kFIRMessagingContextManagerPrefixKey;
  26. // Local timed messages (format yyyy-mm-dd HH:mm:ss)
  27. NSString *const kFIRMessagingContextManagerLocalTimeStart =
  28. kFIRMessagingContextManagerPrefixKey @"lt_start";
  29. NSString *const kFIRMessagingContextManagerLocalTimeEnd =
  30. kFIRMessagingContextManagerPrefixKey @"lt_end";
  31. // Local Notification Params
  32. NSString *const kFIRMessagingContextManagerBodyKey =
  33. kFIRMessagingContextManagerNotificationKeyPrefix @"body";
  34. NSString *const kFIRMessagingContextManagerTitleKey =
  35. kFIRMessagingContextManagerNotificationKeyPrefix @"title";
  36. NSString *const kFIRMessagingContextManagerBadgeKey =
  37. kFIRMessagingContextManagerNotificationKeyPrefix @"badge";
  38. NSString *const kFIRMessagingContextManagerCategoryKey =
  39. kFIRMessagingContextManagerNotificationKeyPrefix @"click_action";
  40. NSString *const kFIRMessagingContextManagerSoundKey =
  41. kFIRMessagingContextManagerNotificationKeyPrefix @"sound";
  42. NSString *const kFIRMessagingContextManagerContentAvailableKey =
  43. kFIRMessagingContextManagerNotificationKeyPrefix @"content-available";
  44. static NSString *const kFIRMessagingAPNSPayloadKey = @"aps";
  45. typedef NS_ENUM(NSUInteger, FIRMessagingContextManagerMessageType) {
  46. FIRMessagingContextManagerMessageTypeNone,
  47. FIRMessagingContextManagerMessageTypeLocalTime,
  48. };
  49. @implementation FIRMessagingContextManagerService
  50. + (BOOL)isContextManagerMessage:(NSDictionary *)message {
  51. // For now we only support local time in ContextManager.
  52. if (![message[kFIRMessagingContextManagerLocalTimeStart] length]) {
  53. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeContextManagerService000,
  54. @"Received message missing local start time, dropped.");
  55. return NO;
  56. }
  57. return YES;
  58. }
  59. + (BOOL)handleContextManagerMessage:(NSDictionary *)message {
  60. NSString *startTimeString = message[kFIRMessagingContextManagerLocalTimeStart];
  61. if (startTimeString.length) {
  62. FIRMessagingLoggerDebug(kFIRMessagingMessageCodeContextManagerService001,
  63. @"%@ Received context manager message with local time %@", kLogTag,
  64. startTimeString);
  65. return [self handleContextManagerLocalTimeMessage:message];
  66. }
  67. return NO;
  68. }
  69. + (BOOL)handleContextManagerLocalTimeMessage:(NSDictionary *)message {
  70. NSString *startTimeString = message[kFIRMessagingContextManagerLocalTimeStart];
  71. if (!startTimeString) {
  72. FIRMessagingLoggerError(kFIRMessagingMessageCodeContextManagerService002,
  73. @"Invalid local start date format %@. Message dropped",
  74. startTimeString);
  75. return NO;
  76. }
  77. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  78. dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  79. [dateFormatter setDateFormat:kLocalTimeFormatString];
  80. NSDate *startDate = [dateFormatter dateFromString:startTimeString];
  81. NSDate *currentDate = [NSDate date];
  82. if ([currentDate compare:startDate] == NSOrderedAscending) {
  83. [self scheduleLocalNotificationForMessage:message atDate:startDate];
  84. } else {
  85. // check end time has not passed
  86. NSString *endTimeString = message[kFIRMessagingContextManagerLocalTimeEnd];
  87. if (!endTimeString) {
  88. FIRMessagingLoggerInfo(
  89. kFIRMessagingMessageCodeContextManagerService003,
  90. @"No end date specified for message, start date elapsed. Message dropped.");
  91. return YES;
  92. }
  93. NSDate *endDate = [dateFormatter dateFromString:endTimeString];
  94. if (!endTimeString) {
  95. FIRMessagingLoggerError(kFIRMessagingMessageCodeContextManagerService004,
  96. @"Invalid local end date format %@. Message dropped", endTimeString);
  97. return NO;
  98. }
  99. if ([endDate compare:currentDate] == NSOrderedAscending) {
  100. // end date has already passed drop the message
  101. FIRMessagingLoggerInfo(kFIRMessagingMessageCodeContextManagerService005,
  102. @"End date %@ has already passed. Message dropped.", endTimeString);
  103. return YES;
  104. }
  105. // schedule message right now (buffer 10s)
  106. [self scheduleLocalNotificationForMessage:message
  107. atDate:[currentDate dateByAddingTimeInterval:10]];
  108. }
  109. return YES;
  110. }
  111. + (void)scheduleLocalNotificationForMessage:(NSDictionary *)message atDate:(NSDate *)date {
  112. #if TARGET_OS_IOS
  113. NSDictionary *apsDictionary = message;
  114. #pragma clang diagnostic push
  115. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  116. UILocalNotification *notification = [[UILocalNotification alloc] init];
  117. #pragma clang diagnostic pop
  118. // A great way to understand timezones and UILocalNotifications
  119. // http://stackoverflow.com/questions/18424569/understanding-uilocalnotification-timezone
  120. notification.timeZone = [NSTimeZone defaultTimeZone];
  121. notification.fireDate = date;
  122. // In the current solution all of the display stuff goes into a special "aps" dictionary
  123. // being sent in the message.
  124. if ([apsDictionary[kFIRMessagingContextManagerBodyKey] length]) {
  125. notification.alertBody = apsDictionary[kFIRMessagingContextManagerBodyKey];
  126. }
  127. if ([apsDictionary[kFIRMessagingContextManagerTitleKey] length]) {
  128. // |alertTitle| is iOS 8.2+, so check if we can set it
  129. if ([notification respondsToSelector:@selector(setAlertTitle:)]) {
  130. #pragma clang diagnostic push
  131. #pragma clang diagnostic ignored "-Wunguarded-availability"
  132. notification.alertTitle = apsDictionary[kFIRMessagingContextManagerTitleKey];
  133. #pragma clang diagnostic pop
  134. }
  135. }
  136. if (apsDictionary[kFIRMessagingContextManagerSoundKey]) {
  137. notification.soundName = apsDictionary[kFIRMessagingContextManagerSoundKey];
  138. }
  139. if (apsDictionary[kFIRMessagingContextManagerBadgeKey]) {
  140. notification.applicationIconBadgeNumber =
  141. [apsDictionary[kFIRMessagingContextManagerBadgeKey] integerValue];
  142. }
  143. if (apsDictionary[kFIRMessagingContextManagerCategoryKey]) {
  144. // |category| is iOS 8.0+, so check if we can set it
  145. if ([notification respondsToSelector:@selector(setCategory:)]) {
  146. notification.category = apsDictionary[kFIRMessagingContextManagerCategoryKey];
  147. }
  148. }
  149. NSDictionary *userInfo = [self parseDataFromMessage:message];
  150. if (userInfo.count) {
  151. notification.userInfo = userInfo;
  152. }
  153. UIApplication *application = [GULAppDelegateSwizzler sharedApplication];
  154. if (!application) {
  155. return;
  156. }
  157. #pragma clang diagnostic push
  158. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  159. [application scheduleLocalNotification:notification];
  160. #pragma clang diagnostic pop
  161. #endif
  162. }
  163. + (NSDictionary *)parseDataFromMessage:(NSDictionary *)message {
  164. NSMutableDictionary *data = [NSMutableDictionary dictionary];
  165. for (NSObject<NSCopying> *key in message) {
  166. if ([key isKindOfClass:[NSString class]]) {
  167. NSString *keyString = (NSString *)key;
  168. if ([keyString isEqualToString:kFIRMessagingContextManagerContentAvailableKey]) {
  169. continue;
  170. } else if ([keyString hasPrefix:kContextManagerPrefixKey]) {
  171. continue;
  172. } else if ([keyString isEqualToString:kFIRMessagingAPNSPayloadKey]) {
  173. // Local timezone message is scheduled with FCM payload. APNS payload with
  174. // content_available should be ignored and not passed to the scheduled
  175. // messages.
  176. continue;
  177. }
  178. }
  179. data[[key copy]] = message[key];
  180. }
  181. return [data copy];
  182. }
  183. @end