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.

195 lines
7.8 KiB

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