Changeset View
Changeset View
Standalone View
Standalone View
native/ios/NotificationService/NotificationService.mm
Show All 18 Lines | |||||
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request | - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request | ||||
withContentHandler: | withContentHandler: | ||||
(void (^)(UNNotificationContent *_Nonnull)) | (void (^)(UNNotificationContent *_Nonnull)) | ||||
contentHandler { | contentHandler { | ||||
self.contentHandler = contentHandler; | self.contentHandler = contentHandler; | ||||
self.bestAttemptContent = [request.content mutableCopy]; | self.bestAttemptContent = [request.content mutableCopy]; | ||||
[self persistMessagePayload:self.bestAttemptContent.userInfo]; | |||||
// Message payload persistence is a higher priority task, so it has | |||||
// to happen prior to potential notification center clearing. | |||||
if ([self isRescind:self.bestAttemptContent.userInfo]) { | if ([self isRescind:self.bestAttemptContent.userInfo]) { | ||||
[self removeNotificationWithIdentifier:self.bestAttemptContent | [self removeNotificationWithIdentifier:self.bestAttemptContent | ||||
.userInfo[@"notificationId"]]; | .userInfo[@"notificationId"]]; | ||||
self.contentHandler([[UNNotificationContent alloc] init]); | self.contentHandler([[UNNotificationContent alloc] init]); | ||||
return; | return; | ||||
} | } | ||||
NSString *message = self.bestAttemptContent.userInfo[@"messageInfos"]; | |||||
if (message) { | |||||
TemporaryMessageStorage *temporaryStorage = | |||||
[[TemporaryMessageStorage alloc] init]; | |||||
[temporaryStorage writeMessage:message]; | |||||
} | |||||
// TODO modify self.bestAttemptContent here | // TODO modify self.bestAttemptContent here | ||||
self.contentHandler(self.bestAttemptContent); | self.contentHandler(self.bestAttemptContent); | ||||
} | } | ||||
- (void)serviceExtensionTimeWillExpire { | - (void)serviceExtensionTimeWillExpire { | ||||
// Called just before the extension will be terminated by the system. | // Called just before the extension will be terminated by the system. | ||||
// Use this as an opportunity to deliver your "best attempt" at modified | // Use this as an opportunity to deliver your "best attempt" at modified | ||||
Show All 32 Lines | [UNUserNotificationCenter.currentNotificationCenter | ||||
} | } | ||||
} | } | ||||
delayedSemaphorePostCallback(); | delayedSemaphorePostCallback(); | ||||
}]; | }]; | ||||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | ||||
} | } | ||||
- (void)persistMessagePayload:(NSDictionary *)payload { | |||||
if (payload[@"messageInfos"]) { | |||||
TemporaryMessageStorage *temporaryStorage = | |||||
[[TemporaryMessageStorage alloc] init]; | |||||
[temporaryStorage writeMessage:payload[@"messageInfos"]]; | |||||
return; | |||||
} | |||||
if (![self isRescind:payload]) { | |||||
return; | |||||
} | |||||
NSError *jsonError = nil; | |||||
NSData *binarySerializedRescindPayload = | |||||
[NSJSONSerialization dataWithJSONObject:payload | |||||
options:0 | |||||
error:&jsonError]; | |||||
if (jsonError) { | |||||
comm::Logger::log( | |||||
"NSE: Failed to serialize rescind payload. Details: " + | |||||
std::string([jsonError.localizedDescription UTF8String])); | |||||
return; | |||||
} | |||||
NSString *serializedRescindPayload = | |||||
[[NSString alloc] initWithData:binarySerializedRescindPayload | |||||
encoding:NSUTF8StringEncoding]; | |||||
TemporaryMessageStorage *temporaryRescindsStorage = | |||||
[[TemporaryMessageStorage alloc] initForRescinds]; | |||||
[temporaryRescindsStorage writeMessage:serializedRescindPayload]; | |||||
} | |||||
- (BOOL)isRescind:(NSDictionary *)payload { | - (BOOL)isRescind:(NSDictionary *)payload { | ||||
return payload[backgroundNotificationTypeKey] && | return payload[backgroundNotificationTypeKey] && | ||||
[payload[backgroundNotificationTypeKey] isEqualToString:@"CLEAR"]; | [payload[backgroundNotificationTypeKey] isEqualToString:@"CLEAR"]; | ||||
} | } | ||||
@end | @end |