Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // NotificationService.m
- // RichNotification
- //
- // Copyright © 2016 Benhauer. All rights reserved.
- //
- #import "NotificationService.h"
- @interface NotificationService ()
- @property(nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
- @property(nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
- @end
- @implementation NotificationService
- - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler {
- self.contentHandler = contentHandler;
- self.bestAttemptContent = [request.content mutableCopy];
- // Modify the notification content here...
- NSDictionary *userInfo = request.content.userInfo;
- if (userInfo == nil) {
- [self contentComplete];
- return;
- }
- NSString *payloadUrl = userInfo[@"att-url"];
- NSString *payloadType = userInfo[@"att-type"];
- [self loadAttachmentForUrlString:payloadUrl attachmentType:payloadType completionHandler:^(UNNotificationAttachment *attachment) {
- if (attachment) {
- self.bestAttemptContent.attachments = [NSArray arrayWithObject:attachment];
- }
- [self contentComplete];
- }];
- }
- - (void)contentComplete {
- self.contentHandler(self.bestAttemptContent);
- }
- - (void)serviceExtensionTimeWillExpire {
- // Called just before the extension will be terminated by the system.
- // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
- self.contentHandler(self.bestAttemptContent);
- }
- - (void)loadAttachmentForUrlString:(NSString *)urlString attachmentType:(NSString*) attachmentType completionHandler:(void (^)(UNNotificationAttachment *))completionHandler {
- __block UNNotificationAttachment *attachment = nil;
- NSURL *attachmentURL = [NSURL URLWithString:urlString];
- NSString *attType = [@"." stringByAppendingString:attachmentType];
- NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
- [[session downloadTaskWithURL:attachmentURL
- completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
- if (error != nil) {
- NSLog(@"%@", error.localizedDescription);
- } else {
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:attType]];
- [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
- NSError *attachmentError = nil;
- attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];
- if (attachmentError) {
- NSLog(@"%@", attachmentError.localizedDescription);
- }
- }
- completionHandler(attachment);
- }] resume];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement