You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
8.5 KiB

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