From ebd483ee5880131d6602e63ca46cfa49caef2983 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 18 Oct 2018 23:09:39 +0300 Subject: Avatars in notifications --- JuickPush/NotificationService.m | 46 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 JuickPush/NotificationService.m (limited to 'JuickPush/NotificationService.m') diff --git a/JuickPush/NotificationService.m b/JuickPush/NotificationService.m new file mode 100644 index 0000000..82dead8 --- /dev/null +++ b/JuickPush/NotificationService.m @@ -0,0 +1,46 @@ +// +// NotificationService.m +// JuickPush +// +// Created by Vitaly Takmazov on 18/10/2018. +// Copyright © 2018 com.juick. 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]; + + NSURL *avatarURL = [NSURL URLWithString:request.content.userInfo[@"avatarUrl"]]; + + NSURLSession * downloadSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; + + [[downloadSession downloadTaskWithURL:avatarURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { + if (!error) { + NSError *err = nil; + NSString *temporaryDirectory = [NSTemporaryDirectory() stringByAppendingPathComponent:[response suggestedFilename]]; + [[NSFileManager defaultManager] moveItemAtPath:location.path toPath:temporaryDirectory error:&err]; + UNNotificationAttachment *avatarAttachment = [UNNotificationAttachment attachmentWithIdentifier:avatarURL.lastPathComponent URL:[NSURL fileURLWithPath:temporaryDirectory] options:nil error:&err]; + self.bestAttemptContent.attachments = @[avatarAttachment]; + self.contentHandler(self.bestAttemptContent); + } + }] resume]; +} + +- (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); +} + +@end -- cgit v1.2.3