| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //
- // NotificationService.m
- // notification
- //
- // Created by Leon on 2022/6/21.
- //
- #import "NotificationService.h"
- #import "JPushNotificationExtensionService.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 {
- [JPushNotificationExtensionService jpushSetAppkey:@"7cf918ada725a9e9aecc8a17"];
- [JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^{
- NSLog(@"apns upload success");
- // contentHandler(request.content);
- }];
- self.contentHandler = contentHandler;
- self.bestAttemptContent = [request.content mutableCopy];
-
- NSString * attachmentPath = self.bestAttemptContent.userInfo[@"message_big_pic"];
- if (!attachmentPath){
- self.contentHandler(self.bestAttemptContent);
- }
- else {
- [self loadAttachmentForUrlString:attachmentPath completionHandle:^(UNNotificationAttachment *attach) {
- if (attach){
- self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach];
- }
-
- self.contentHandler(self.bestAttemptContent);
- }];
- }
- //if exist
- // if (attachmentPath) {
- // //download
- // NSURL *fileURL = [NSURL URLWithString:attachmentPath];
- // [self downloadAndSave:fileURL handler:^(NSString *localPath) {
- // if (localPath) {
- // NSError *e = nil;
- // UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:@"myAttachment" URL:[NSURL fileURLWithPath:localPath] options:nil error:&e];
- // if (!e){
- // self.bestAttemptContent.attachments = @[attachment];
- // // self.bestAttemptContent.title = @"ppp";//self.bestAttemptContent.title;
- // self.contentHandler(self.bestAttemptContent);
- // }else {
- // // self.bestAttemptContent.attachments = @[attachment];
- // self.bestAttemptContent.title = @"111";//localPath;//[NSString stringWithFormat:@"%@",e];
- // self.contentHandler(self.bestAttemptContent);
- // }
- //
- // }
- // else {
- // self.bestAttemptContent.title = @"hello world2";
- // self.contentHandler(self.bestAttemptContent);
- // }
- // // [self apnsDeliverWith:request];
- // }];
- // }else{
- // self.bestAttemptContent.title = @"hello world3";
- // self.contentHandler(self.bestAttemptContent);
- // }
- }
- - (void)loadAttachmentForUrlString:(NSString *)urlStr
- completionHandle:(void(^)(UNNotificationAttachment *attach))completionHandler{
- __block UNNotificationAttachment *attachment = nil;
- NSURL *attachmentURL = [NSURL URLWithString:urlStr];
- NSString *fileExt = @".jpg";
- 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:fileExt]];
- [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];
- }
- - (void)downloadAndSave:(NSURL *)fileURL handler:(void (^)(NSString *))handler {
- NSURLSession * session = [NSURLSession sharedSession];
- NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
- NSString *localPath = nil;
- if (!error) {
- NSString * localURL = [NSString stringWithFormat:@"%@/%@pushimage.jpg", NSTemporaryDirectory(),fileURL.lastPathComponent];
- if ([[NSFileManager defaultManager] fileExistsAtPath:localURL]){
- [[NSFileManager defaultManager] removeItemAtPath:localURL error:nil];
- }
-
- NSError * _Nullable error2;
- if ([[NSFileManager defaultManager] moveItemAtPath:location.path toPath:localURL error:&error2])
- {
- localPath = localURL;
- // self.bestAttemptContent.title = [NSString stringWithFormat:@"%@",localURL];
- // self.contentHandler(self.bestAttemptContent);
- }
- else {
- // if (error2){
- // self.bestAttemptContent.title = @"pppppddd";
- // }
- // else {
- // self.bestAttemptContent.title = @"dddd";//[NSString stringWithFormat:@"%@",@"2222333"];
- // }
- //
- // self.contentHandler(self.bestAttemptContent);
- }
- }
- else {
- // self.bestAttemptContent.title = [NSString stringWithFormat:@"%@",error.description];
- // self.contentHandler(self.bestAttemptContent);
- }
- handler(localPath);
- }];
- [task 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
|