NotificationService.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // NotificationService.m
  3. // notification
  4. //
  5. // Created by Leon on 2022/6/21.
  6. //
  7. #import "NotificationService.h"
  8. #import "JPushNotificationExtensionService.h"
  9. @interface NotificationService ()
  10. @property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
  11. @property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
  12. @end
  13. @implementation NotificationService
  14. - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
  15. [JPushNotificationExtensionService jpushSetAppkey:@"7cf918ada725a9e9aecc8a17"];
  16. [JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^{
  17. NSLog(@"apns upload success");
  18. // contentHandler(request.content);
  19. }];
  20. self.contentHandler = contentHandler;
  21. self.bestAttemptContent = [request.content mutableCopy];
  22. NSString * attachmentPath = self.bestAttemptContent.userInfo[@"message_big_pic"];
  23. if (!attachmentPath){
  24. self.contentHandler(self.bestAttemptContent);
  25. }
  26. else {
  27. [self loadAttachmentForUrlString:attachmentPath completionHandle:^(UNNotificationAttachment *attach) {
  28. if (attach){
  29. self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach];
  30. }
  31. self.contentHandler(self.bestAttemptContent);
  32. }];
  33. }
  34. //if exist
  35. // if (attachmentPath) {
  36. // //download
  37. // NSURL *fileURL = [NSURL URLWithString:attachmentPath];
  38. // [self downloadAndSave:fileURL handler:^(NSString *localPath) {
  39. // if (localPath) {
  40. // NSError *e = nil;
  41. // UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:@"myAttachment" URL:[NSURL fileURLWithPath:localPath] options:nil error:&e];
  42. // if (!e){
  43. // self.bestAttemptContent.attachments = @[attachment];
  44. // // self.bestAttemptContent.title = @"ppp";//self.bestAttemptContent.title;
  45. // self.contentHandler(self.bestAttemptContent);
  46. // }else {
  47. // // self.bestAttemptContent.attachments = @[attachment];
  48. // self.bestAttemptContent.title = @"111";//localPath;//[NSString stringWithFormat:@"%@",e];
  49. // self.contentHandler(self.bestAttemptContent);
  50. // }
  51. //
  52. // }
  53. // else {
  54. // self.bestAttemptContent.title = @"hello world2";
  55. // self.contentHandler(self.bestAttemptContent);
  56. // }
  57. // // [self apnsDeliverWith:request];
  58. // }];
  59. // }else{
  60. // self.bestAttemptContent.title = @"hello world3";
  61. // self.contentHandler(self.bestAttemptContent);
  62. // }
  63. }
  64. - (void)loadAttachmentForUrlString:(NSString *)urlStr
  65. completionHandle:(void(^)(UNNotificationAttachment *attach))completionHandler{
  66. __block UNNotificationAttachment *attachment = nil;
  67. NSURL *attachmentURL = [NSURL URLWithString:urlStr];
  68. NSString *fileExt = @".jpg";
  69. NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  70. [[session downloadTaskWithURL:attachmentURL
  71. completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
  72. if (error != nil) {
  73. NSLog(@"%@", error.localizedDescription);
  74. } else {
  75. NSFileManager *fileManager = [NSFileManager defaultManager];
  76. NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileExt]];
  77. [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
  78. NSError *attachmentError = nil;
  79. attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];
  80. if (attachmentError) {
  81. NSLog(@"%@", attachmentError.localizedDescription);
  82. }
  83. }
  84. completionHandler(attachment);
  85. }] resume];
  86. }
  87. - (void)downloadAndSave:(NSURL *)fileURL handler:(void (^)(NSString *))handler {
  88. NSURLSession * session = [NSURLSession sharedSession];
  89. NSURLSessionDownloadTask *task = [session downloadTaskWithURL:fileURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  90. NSString *localPath = nil;
  91. if (!error) {
  92. NSString * localURL = [NSString stringWithFormat:@"%@/%@pushimage.jpg", NSTemporaryDirectory(),fileURL.lastPathComponent];
  93. if ([[NSFileManager defaultManager] fileExistsAtPath:localURL]){
  94. [[NSFileManager defaultManager] removeItemAtPath:localURL error:nil];
  95. }
  96. NSError * _Nullable error2;
  97. if ([[NSFileManager defaultManager] moveItemAtPath:location.path toPath:localURL error:&error2])
  98. {
  99. localPath = localURL;
  100. // self.bestAttemptContent.title = [NSString stringWithFormat:@"%@",localURL];
  101. // self.contentHandler(self.bestAttemptContent);
  102. }
  103. else {
  104. // if (error2){
  105. // self.bestAttemptContent.title = @"pppppddd";
  106. // }
  107. // else {
  108. // self.bestAttemptContent.title = @"dddd";//[NSString stringWithFormat:@"%@",@"2222333"];
  109. // }
  110. //
  111. // self.contentHandler(self.bestAttemptContent);
  112. }
  113. }
  114. else {
  115. // self.bestAttemptContent.title = [NSString stringWithFormat:@"%@",error.description];
  116. // self.contentHandler(self.bestAttemptContent);
  117. }
  118. handler(localPath);
  119. }];
  120. [task resume];
  121. }
  122. - (void)serviceExtensionTimeWillExpire {
  123. // Called just before the extension will be terminated by the system.
  124. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  125. self.contentHandler(self.bestAttemptContent);
  126. }
  127. @end