summaryrefslogtreecommitdiff
path: root/etc/NotificationService.m
blob: c30eaed9088be2a83aea59d34318ec6487c54dc2 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
//  NotificationService.m
//  NotificationService
//
//  Created by User on 29/09/16.
//
//
// Credit https://github.com/Leanplum/Leanplum-iOS-Samples/blob/master/iOS_basicSetup/basicSetup/richPushExtension/NotificationService.m

#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];
    NSDictionary *userInfo = request.content.userInfo;
    
    
    // If there is no image in the payload than
    // the code will still show the push notification.
    if (userInfo == nil || userInfo[@"image_url_jpg"] == nil) {
        NSLog(@"zmNinja Notification: Did not get a payload or image");
        [self contentComplete];
        return;
    }
    
    NSString *mediaUrl = userInfo[@"image_url_jpg"];
   // if (mediaType == nil) {
   //   NSLog(@"zmNinja Notification: No media type specified, assuming .jpg");
  //    mediaType = @".jpg";
  //  }
    
    // load the attachment
    [self loadAttachmentForUrlString:mediaUrl
                            
                   completionHandler:^(UNNotificationAttachment *attachment) {
                       if (attachment) {
                           self.bestAttemptContent.attachments = [NSArray arrayWithObject:attachment];
                       }
                       [self contentComplete];
                   }];
    
}

- (NSString*)determineType:(NSString *) fileType {
    // Determines the file type of the attachment to append to NSURL.
    //return @".gif";
      // Determines the file type of the attachment to append to NSURL.
    NSLog (@"zmNinja Notification: determineType got filetype=%@",fileType);
    if ([fileType isEqualToString:@"image/jpeg"]){
        NSLog (@"zmNinja Notification: returning JPG");
        return @".jpg";
    }
    if ([fileType isEqualToString:@"video/mp4"]){
        NSLog (@"zmNinja Notification: returning MP4");
        return @".mp4";
    }

    if ([fileType isEqualToString:@"image/gif"]) {
         NSLog (@"zmNinja Notification: returning GIF");
        return @".gif";
    }
    if ([fileType isEqualToString:@"image/png"]) {
         NSLog (@"zmNinja Notification: returning PNG");
        return @".png";
   
    }
     NSLog (@"zmNinja Notification: unrecognized filetype, returning JPG");
    return @".jpg";
   
    
}

- (void)loadAttachmentForUrlString:(NSString *)urlString 
                 completionHandler:(void(^)(UNNotificationAttachment *))completionHandler  {
    
    __block UNNotificationAttachment *attachment = nil;
    NSURL *attachmentURL = [NSURL URLWithString:urlString];
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session downloadTaskWithURL:attachmentURL
                completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
                    if (error != nil) {
                     
                        NSLog(@"unable to add attachment: %@", error.localizedDescription);
          
                    } else {
                        NSString *fileType = [self determineType: [response MIMEType]];
                        NSFileManager *fileManager = [NSFileManager defaultManager];
                        NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:fileType]];
                        [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
                        
                        NSError *attachmentError = nil;
                        attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];
                        if (attachmentError) {
                      
                            NSLog(@"unable to add attchment: %@", attachmentError.localizedDescription);
                        
                        }
                    }
                    completionHandler(attachment);
                }] resume];
}

- (void)contentComplete {
    self.contentHandler(self.bestAttemptContent);
}

- (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.
    NSLog (@"zmNinja Notification: Time about to expire, handing off to best attempt");
    self.contentHandler(self.bestAttemptContent);
}

@end