Hale Cai
2018-08-10 42c80d0b43ff8736edb5be4bbfcbc7a99fc8a119
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
//  NotificationService.m
//  NotificationServiceTest
//
//  Created by jpush on 16/7/26.
//
//
 
#import "NotificationService.h"
#import "JPushNotificationExtensionService.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
 
@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];
  self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [NotificationService]", self.bestAttemptContent.title];
  
  NSURLSession * session = [NSURLSession sharedSession];
  NSString * attachmentPath = self.bestAttemptContent.userInfo[@"my-attachment"];
  //if exist
  if (attachmentPath && [attachmentPath hasSuffix:@"png"]) {
    //download
    NSURLSessionTask * task = [session dataTaskWithURL:[NSURL URLWithString:attachmentPath] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
      if (data) {
        NSString * localPath = [NSString stringWithFormat:@"%@/myAttachment.png", NSTemporaryDirectory()];
        if ([data writeToFile:localPath atomically:YES]) {
          UNNotificationAttachment * attachment = [UNNotificationAttachment attachmentWithIdentifier:@"myAttachment" URL:[NSURL fileURLWithPath:localPath] options:nil error:nil];
          self.bestAttemptContent.attachments = @[attachment];
        }
      }
      [self apnsDeliverWith:request];
    }];
    [task resume];
  }else{
    [self apnsDeliverWith:request];
  }
}
 
- (void)apnsDeliverWith:(UNNotificationRequest *)request {
  
  //please invoke this func on release version
  //[JPushNotificationExtensionService setLogOff];
  
  //service extension sdk
  //upload to calculate delivery rate
  //please set the same AppKey as your JPush
  [JPushNotificationExtensionService jpushSetAppkey:@"AppKey copied from JiGuang Portal application"];
  [JPushNotificationExtensionService jpushReceiveNotificationRequest:request with:^ {
    NSLog(@"apns upload success");
    self.contentHandler(self.bestAttemptContent);
  }];
}
 
- (void)serviceExtensionTimeWillExpire {
  self.contentHandler(self.bestAttemptContent);
}
 
@end
#endif