1 |
efrain |
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 |
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 || \
|
|
|
17 |
__MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_14 || __TV_OS_VERSION_MAX_ALLOWED >= __TV_10_0 || \
|
|
|
18 |
__WATCH_OS_VERSION_MAX_ALLOWED >= __WATCHOS_3_0 || TARGET_OS_MACCATALYST
|
|
|
19 |
#import <UserNotifications/UserNotifications.h>
|
|
|
20 |
#endif
|
|
|
21 |
|
|
|
22 |
#import "FirebaseMessaging/Sources/FIRMessagingContextManagerService.h"
|
|
|
23 |
|
|
|
24 |
#import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
|
|
|
25 |
#import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
|
|
|
26 |
#import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
|
|
|
27 |
|
|
|
28 |
#import <GoogleUtilities/GULAppDelegateSwizzler.h>
|
|
|
29 |
|
|
|
30 |
#define kFIRMessagingContextManagerPrefix @"gcm."
|
|
|
31 |
#define kFIRMessagingContextManagerPrefixKey @"google.c.cm."
|
|
|
32 |
#define kFIRMessagingContextManagerNotificationKeyPrefix @"gcm.notification."
|
|
|
33 |
|
|
|
34 |
static NSString *const kLogTag = @"FIRMessagingAnalytics";
|
|
|
35 |
|
|
|
36 |
static NSString *const kLocalTimeFormatString = @"yyyy-MM-dd HH:mm:ss";
|
|
|
37 |
|
|
|
38 |
static NSString *const kContextManagerPrefixKey = kFIRMessagingContextManagerPrefixKey;
|
|
|
39 |
|
|
|
40 |
// Local timed messages (format yyyy-mm-dd HH:mm:ss)
|
|
|
41 |
NSString *const kFIRMessagingContextManagerLocalTimeStart =
|
|
|
42 |
kFIRMessagingContextManagerPrefixKey @"lt_start";
|
|
|
43 |
NSString *const kFIRMessagingContextManagerLocalTimeEnd =
|
|
|
44 |
kFIRMessagingContextManagerPrefixKey @"lt_end";
|
|
|
45 |
|
|
|
46 |
// Local Notification Params
|
|
|
47 |
NSString *const kFIRMessagingContextManagerBodyKey =
|
|
|
48 |
kFIRMessagingContextManagerNotificationKeyPrefix @"body";
|
|
|
49 |
NSString *const kFIRMessagingContextManagerTitleKey =
|
|
|
50 |
kFIRMessagingContextManagerNotificationKeyPrefix @"title";
|
|
|
51 |
NSString *const kFIRMessagingContextManagerBadgeKey =
|
|
|
52 |
kFIRMessagingContextManagerNotificationKeyPrefix @"badge";
|
|
|
53 |
NSString *const kFIRMessagingContextManagerCategoryKey =
|
|
|
54 |
kFIRMessagingContextManagerNotificationKeyPrefix @"click_action";
|
|
|
55 |
NSString *const kFIRMessagingContextManagerSoundKey =
|
|
|
56 |
kFIRMessagingContextManagerNotificationKeyPrefix @"sound";
|
|
|
57 |
NSString *const kFIRMessagingContextManagerContentAvailableKey =
|
|
|
58 |
kFIRMessagingContextManagerNotificationKeyPrefix @"content-available";
|
|
|
59 |
static NSString *const kFIRMessagingID = kFIRMessagingContextManagerPrefix @"message_id";
|
|
|
60 |
static NSString *const kFIRMessagingAPNSPayloadKey = @"aps";
|
|
|
61 |
|
|
|
62 |
typedef NS_ENUM(NSUInteger, FIRMessagingContextManagerMessageType) {
|
|
|
63 |
FIRMessagingContextManagerMessageTypeNone,
|
|
|
64 |
FIRMessagingContextManagerMessageTypeLocalTime,
|
|
|
65 |
};
|
|
|
66 |
|
|
|
67 |
@implementation FIRMessagingContextManagerService
|
|
|
68 |
|
|
|
69 |
+ (BOOL)isContextManagerMessage:(NSDictionary *)message {
|
|
|
70 |
// For now we only support local time in ContextManager.
|
|
|
71 |
if (![message[kFIRMessagingContextManagerLocalTimeStart] length]) {
|
|
|
72 |
FIRMessagingLoggerDebug(
|
|
|
73 |
kFIRMessagingMessageCodeContextManagerService000,
|
|
|
74 |
@"Received message missing local start time, not a contextual message.");
|
|
|
75 |
return NO;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return YES;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
+ (BOOL)handleContextManagerMessage:(NSDictionary *)message {
|
|
|
82 |
NSString *startTimeString = message[kFIRMessagingContextManagerLocalTimeStart];
|
|
|
83 |
if (startTimeString.length) {
|
|
|
84 |
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeContextManagerService001,
|
|
|
85 |
@"%@ Received context manager message with local time %@", kLogTag,
|
|
|
86 |
startTimeString);
|
|
|
87 |
return [self handleContextManagerLocalTimeMessage:message];
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
return NO;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
+ (BOOL)handleContextManagerLocalTimeMessage:(NSDictionary *)message {
|
|
|
94 |
NSString *startTimeString = message[kFIRMessagingContextManagerLocalTimeStart];
|
|
|
95 |
if (!startTimeString) {
|
|
|
96 |
FIRMessagingLoggerError(kFIRMessagingMessageCodeContextManagerService002,
|
|
|
97 |
@"Invalid local start date format %@. Message dropped",
|
|
|
98 |
startTimeString);
|
|
|
99 |
return NO;
|
|
|
100 |
}
|
|
|
101 |
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
|
|
102 |
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
|
|
|
103 |
[dateFormatter setDateFormat:kLocalTimeFormatString];
|
|
|
104 |
NSDate *startDate = [dateFormatter dateFromString:startTimeString];
|
|
|
105 |
NSDate *currentDate = [NSDate date];
|
|
|
106 |
|
|
|
107 |
if ([currentDate compare:startDate] == NSOrderedAscending) {
|
|
|
108 |
[self scheduleLocalNotificationForMessage:message atDate:startDate];
|
|
|
109 |
} else {
|
|
|
110 |
// check end time has not passed
|
|
|
111 |
NSString *endTimeString = message[kFIRMessagingContextManagerLocalTimeEnd];
|
|
|
112 |
if (!endTimeString) {
|
|
|
113 |
FIRMessagingLoggerInfo(
|
|
|
114 |
kFIRMessagingMessageCodeContextManagerService003,
|
|
|
115 |
@"No end date specified for message, start date elapsed. Message dropped.");
|
|
|
116 |
return YES;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
NSDate *endDate = [dateFormatter dateFromString:endTimeString];
|
|
|
120 |
if (!endTimeString) {
|
|
|
121 |
FIRMessagingLoggerError(kFIRMessagingMessageCodeContextManagerService004,
|
|
|
122 |
@"Invalid local end date format %@. Message dropped", endTimeString);
|
|
|
123 |
return NO;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
if ([endDate compare:currentDate] == NSOrderedAscending) {
|
|
|
127 |
// end date has already passed drop the message
|
|
|
128 |
FIRMessagingLoggerInfo(kFIRMessagingMessageCodeContextManagerService005,
|
|
|
129 |
@"End date %@ has already passed. Message dropped.", endTimeString);
|
|
|
130 |
return YES;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
// schedule message right now (buffer 10s)
|
|
|
134 |
[self scheduleLocalNotificationForMessage:message
|
|
|
135 |
atDate:[currentDate dateByAddingTimeInterval:10]];
|
|
|
136 |
}
|
|
|
137 |
return YES;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
+ (void)scheduleiOS10LocalNotificationForMessage:(NSDictionary *)message
|
|
|
141 |
atDate:(NSDate *)date
|
|
|
142 |
API_AVAILABLE(macosx(10.14), ios(10.0), watchos(3.0), tvos(10.0)) {
|
|
|
143 |
NSCalendar *calendar = [NSCalendar currentCalendar];
|
|
|
144 |
NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay |
|
|
|
145 |
NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
|
|
|
146 |
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnit)unit fromDate:date];
|
|
|
147 |
UNCalendarNotificationTrigger *trigger =
|
|
|
148 |
[UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:NO];
|
|
|
149 |
|
|
|
150 |
UNMutableNotificationContent *content = [self contentFromContextualMessage:message];
|
|
|
151 |
NSString *identifier = message[kFIRMessagingID];
|
|
|
152 |
if (!identifier) {
|
|
|
153 |
identifier = [NSUUID UUID].UUIDString;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
|
|
|
157 |
content:content
|
|
|
158 |
trigger:trigger];
|
|
|
159 |
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
|
160 |
[center
|
|
|
161 |
addNotificationRequest:request
|
|
|
162 |
withCompletionHandler:^(NSError *_Nullable error) {
|
|
|
163 |
if (error) {
|
|
|
164 |
FIRMessagingLoggerError(kFIRMessagingMessageCodeContextManagerServiceFailedLocalSchedule,
|
|
|
165 |
@"Failed scheduling local timezone notification: %@.", error);
|
|
|
166 |
}
|
|
|
167 |
}];
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
+ (UNMutableNotificationContent *)contentFromContextualMessage:(NSDictionary *)message
|
|
|
171 |
API_AVAILABLE(macosx(10.14), ios(10.0), watchos(3.0), tvos(10.0)) {
|
|
|
172 |
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
|
|
|
173 |
NSDictionary *apsDictionary = message;
|
|
|
174 |
|
|
|
175 |
// Badge is universal
|
|
|
176 |
if (apsDictionary[kFIRMessagingContextManagerBadgeKey]) {
|
|
|
177 |
content.badge = apsDictionary[kFIRMessagingContextManagerBadgeKey];
|
|
|
178 |
}
|
|
|
179 |
#if TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_WATCH
|
|
|
180 |
// The following fields are not available on tvOS
|
|
|
181 |
if ([apsDictionary[kFIRMessagingContextManagerBodyKey] length]) {
|
|
|
182 |
content.body = apsDictionary[kFIRMessagingContextManagerBodyKey];
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
if ([apsDictionary[kFIRMessagingContextManagerTitleKey] length]) {
|
|
|
186 |
content.title = apsDictionary[kFIRMessagingContextManagerTitleKey];
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
if (apsDictionary[kFIRMessagingContextManagerSoundKey]) {
|
|
|
190 |
#if !TARGET_OS_WATCH
|
|
|
191 |
// UNNotificationSound soundNamded: is not available in watchOS
|
|
|
192 |
content.sound =
|
|
|
193 |
[UNNotificationSound soundNamed:apsDictionary[kFIRMessagingContextManagerSoundKey]];
|
|
|
194 |
#else // !TARGET_OS_WATCH
|
|
|
195 |
content.sound = [UNNotificationSound defaultSound];
|
|
|
196 |
#endif // !TARGET_OS_WATCH
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
if (apsDictionary[kFIRMessagingContextManagerCategoryKey]) {
|
|
|
200 |
content.categoryIdentifier = apsDictionary[kFIRMessagingContextManagerCategoryKey];
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
NSDictionary *userInfo = [self parseDataFromMessage:message];
|
|
|
204 |
if (userInfo.count) {
|
|
|
205 |
content.userInfo = userInfo;
|
|
|
206 |
}
|
|
|
207 |
#endif // TARGET_OS_IOS || TARGET_OS_OSX || TARGET_OS_WATCH
|
|
|
208 |
return content;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
+ (void)scheduleLocalNotificationForMessage:(NSDictionary *)message atDate:(NSDate *)date {
|
|
|
212 |
if (@available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *)) {
|
|
|
213 |
[self scheduleiOS10LocalNotificationForMessage:message atDate:date];
|
|
|
214 |
return;
|
|
|
215 |
}
|
|
|
216 |
#if TARGET_OS_IOS
|
|
|
217 |
NSDictionary *apsDictionary = message;
|
|
|
218 |
#pragma clang diagnostic push
|
|
|
219 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
220 |
UILocalNotification *notification = [[UILocalNotification alloc] init];
|
|
|
221 |
#pragma clang diagnostic pop
|
|
|
222 |
|
|
|
223 |
// A great way to understand timezones and UILocalNotifications
|
|
|
224 |
// http://stackoverflow.com/questions/18424569/understanding-uilocalnotification-timezone
|
|
|
225 |
notification.timeZone = [NSTimeZone defaultTimeZone];
|
|
|
226 |
notification.fireDate = date;
|
|
|
227 |
|
|
|
228 |
// In the current solution all of the display stuff goes into a special "aps" dictionary
|
|
|
229 |
// being sent in the message.
|
|
|
230 |
if ([apsDictionary[kFIRMessagingContextManagerBodyKey] length]) {
|
|
|
231 |
notification.alertBody = apsDictionary[kFIRMessagingContextManagerBodyKey];
|
|
|
232 |
}
|
|
|
233 |
if (@available(iOS 8.2, *)) {
|
|
|
234 |
if ([apsDictionary[kFIRMessagingContextManagerTitleKey] length]) {
|
|
|
235 |
notification.alertTitle = apsDictionary[kFIRMessagingContextManagerTitleKey];
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
if (apsDictionary[kFIRMessagingContextManagerSoundKey]) {
|
|
|
240 |
notification.soundName = apsDictionary[kFIRMessagingContextManagerSoundKey];
|
|
|
241 |
}
|
|
|
242 |
if (apsDictionary[kFIRMessagingContextManagerBadgeKey]) {
|
|
|
243 |
notification.applicationIconBadgeNumber =
|
|
|
244 |
[apsDictionary[kFIRMessagingContextManagerBadgeKey] integerValue];
|
|
|
245 |
}
|
|
|
246 |
if (apsDictionary[kFIRMessagingContextManagerCategoryKey]) {
|
|
|
247 |
notification.category = apsDictionary[kFIRMessagingContextManagerCategoryKey];
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
NSDictionary *userInfo = [self parseDataFromMessage:message];
|
|
|
251 |
if (userInfo.count) {
|
|
|
252 |
notification.userInfo = userInfo;
|
|
|
253 |
}
|
|
|
254 |
UIApplication *application = [GULAppDelegateSwizzler sharedApplication];
|
|
|
255 |
if (!application) {
|
|
|
256 |
return;
|
|
|
257 |
}
|
|
|
258 |
#pragma clang diagnostic push
|
|
|
259 |
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
260 |
[application scheduleLocalNotification:notification];
|
|
|
261 |
#pragma clang diagnostic pop
|
|
|
262 |
#endif
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
+ (NSDictionary *)parseDataFromMessage:(NSDictionary *)message {
|
|
|
266 |
NSMutableDictionary *data = [NSMutableDictionary dictionary];
|
|
|
267 |
for (NSObject<NSCopying> *key in message) {
|
|
|
268 |
if ([key isKindOfClass:[NSString class]]) {
|
|
|
269 |
NSString *keyString = (NSString *)key;
|
|
|
270 |
if ([keyString isEqualToString:kFIRMessagingContextManagerContentAvailableKey]) {
|
|
|
271 |
continue;
|
|
|
272 |
} else if ([keyString hasPrefix:kContextManagerPrefixKey]) {
|
|
|
273 |
continue;
|
|
|
274 |
} else if ([keyString isEqualToString:kFIRMessagingAPNSPayloadKey]) {
|
|
|
275 |
// Local timezone message is scheduled with FCM payload. APNS payload with
|
|
|
276 |
// content_available should be ignored and not passed to the scheduled
|
|
|
277 |
// messages.
|
|
|
278 |
continue;
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
data[[key copy]] = message[key];
|
|
|
282 |
}
|
|
|
283 |
return [data copy];
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
@end
|