-(void)didReceiveNotificationRequest:(UNNotificationRequest*)request
withContentHandler:(void(^)(UNNotificationContent*_Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent =[request.content mutableCopy];
//[modified] This is a marker that can modify the content sent from the server. For testing purposes only.
self.bestAttemptContent.title =[NSString stringWithFormat:@"%@ [modified]",self.bestAttemptContent.title];
NSDictionary*apsDic =[request.content.userInfo objectForKey:@"aps"];
NSString*attachUrl =[apsDic objectForKey:@"sobot_chat_big_img"];
NSString*category =[apsDic objectForKey:@"category"];
self.bestAttemptContent.categoryIdentifier = category;
NSURLSession*session =[NSURLSession sharedSession];
NSURL *url =[NSURL URLWithString:attachUrl];
NSURLSessionDownloadTask*downloadTask =[session downloadTaskWithURL:url
completionHandler:^(NSURL *_Nullable location,
NSURLResponse*_Nullable response,
NSError*_Nullable error){
NSString*caches =[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) lastObject];
NSString*file =[caches stringByAppendingPathComponent:response.suggestedFilename];
NSFileManager*mgr =[NSFileManager defaultManager];
[mgr moveItemAtPath:location.path toPath:file error:nil];
if(file &&![file isEqualToString:@""])
{
UNNotificationAttachment*attch=[UNNotificationAttachment attachmentWithIdentifier:@"photo"
URL:[NSURL URLWithString:[@"file://" stringByAppendingString:file]]
options:nil
error:nil];
if(attch)
{
self.bestAttemptContent.attachments =@[attch];
}
}
self.contentHandler(self.bestAttemptContent);
}];
[downloadTask resume];
}
- (void)downloadAndSave:(NSURL *)fileURL handler:(void (^)(NSString *))handler {
// Here, you need to use a system network request to download the image.
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *localPath = nil;
if (!error) {
// Temporary folder path, images will be automatically deleted when the APP is not running, and will not occupy memory.
NSString *localURL = [NSString stringWithFormat:@"%@/%@", NSTemporaryDirectory(), fileURL.lastPathComponent];
if ([[NSFileManager defaultManager] moveItemAtPath:location.path toPath:localURL error:nil]) {
localPath = localURL;
}
}
handler(localPath);
}];
[task resume];
}