Changeset View
Changeset View
Standalone View
Standalone View
native/ios/NotificationService/NotificationService.mm
#import "NotificationService.h" | #import "NotificationService.h" | ||||
#import "Logger.h" | |||||
#import "TemporaryMessageStorage.h" | #import "TemporaryMessageStorage.h" | ||||
NSString *const backgroundNotificationTypeKey = @"backgroundNotifType"; | |||||
@interface NotificationService () | @interface NotificationService () | ||||
@property(nonatomic, strong) void (^contentHandler) | @property(nonatomic, strong) void (^contentHandler) | ||||
(UNNotificationContent *contentToDeliver); | (UNNotificationContent *contentToDeliver); | ||||
@property(nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; | @property(nonatomic, strong) UNMutableNotificationContent *bestAttemptContent; | ||||
@end | @end | ||||
@implementation NotificationService | @implementation NotificationService | ||||
- (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]; | ||||
if ([self isRescind:self.bestAttemptContent.userInfo]) { | |||||
self.contentHandler([[UNNotificationContent alloc] init]); | |||||
return; | |||||
} | |||||
NSString *message = self.bestAttemptContent.userInfo[@"messageInfos"]; | NSString *message = self.bestAttemptContent.userInfo[@"messageInfos"]; | ||||
if (message) { | if (message) { | ||||
TemporaryMessageStorage *temporaryStorage = | TemporaryMessageStorage *temporaryStorage = | ||||
[[TemporaryMessageStorage alloc] init]; | [[TemporaryMessageStorage alloc] init]; | ||||
[temporaryStorage writeMessage:message]; | [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 | ||||
// content, otherwise the original push payload will be used. | // content, otherwise the original push payload will be used. | ||||
if ([self isRescind:self.bestAttemptContent.userInfo]) { | |||||
// If we get to this place it means we were unable to | |||||
// remove relevant notification from notification center in | |||||
// in time given to NSE to process notification. | |||||
// It is an extremely unlikely to happen. | |||||
comm::Logger::log("NSE: Exceeded time limit to rescind a notification."); | |||||
self.contentHandler([[UNNotificationContent alloc] init]); | |||||
} | |||||
self.contentHandler(self.bestAttemptContent); | self.contentHandler(self.bestAttemptContent); | ||||
} | } | ||||
- (BOOL)isRescind:(NSDictionary *)payload { | |||||
return payload[backgroundNotificationTypeKey] && | |||||
[payload[backgroundNotificationTypeKey] isEqualToString:@"CLEAR"]; | |||||
} | |||||
@end | @end |