FIRMessagingContextManagerService.m 8.1 KB

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