Advertisement
Guest User

notification sample code

a guest
Dec 2nd, 2016
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  NotificationService.m
  3. //  RichNotification
  4. //
  5. //  Copyright © 2016 Benhauer. All rights reserved.
  6. //
  7.  
  8. #import "NotificationService.h"
  9.  
  10. @interface NotificationService ()
  11.  
  12. @property(nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
  13. @property(nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
  14.  
  15. @end
  16.  
  17. @implementation NotificationService
  18.  
  19.  
  20. - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler {
  21.  
  22.     self.contentHandler = contentHandler;
  23.     self.bestAttemptContent = [request.content mutableCopy];
  24.  
  25.     // Modify the notification content here...
  26.  
  27.     NSDictionary *userInfo = request.content.userInfo;
  28.     if (userInfo == nil) {
  29.         [self contentComplete];
  30.         return;
  31.     }
  32.  
  33.     NSString *payloadUrl = userInfo[@"att-url"];
  34.     NSString *payloadType = userInfo[@"att-type"];
  35.  
  36.     [self loadAttachmentForUrlString:payloadUrl attachmentType:payloadType completionHandler:^(UNNotificationAttachment *attachment) {
  37.         if (attachment) {
  38.             self.bestAttemptContent.attachments = [NSArray arrayWithObject:attachment];
  39.         }
  40.         [self contentComplete];
  41.     }];
  42.  
  43.  
  44. }
  45.  
  46. - (void)contentComplete {
  47.     self.contentHandler(self.bestAttemptContent);
  48. }
  49.  
  50. - (void)serviceExtensionTimeWillExpire {
  51.     // Called just before the extension will be terminated by the system.
  52.     // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
  53.     self.contentHandler(self.bestAttemptContent);
  54. }
  55.  
  56. - (void)loadAttachmentForUrlString:(NSString *)urlString attachmentType:(NSString*) attachmentType completionHandler:(void (^)(UNNotificationAttachment *))completionHandler {
  57.  
  58.     __block UNNotificationAttachment *attachment = nil;
  59.  
  60.    
  61.    
  62.     NSURL *attachmentURL = [NSURL URLWithString:urlString];
  63.  
  64.     NSString *attType = [@"." stringByAppendingString:attachmentType];
  65.    
  66.     NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  67.     [[session downloadTaskWithURL:attachmentURL
  68.                 completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
  69.                     if (error != nil) {
  70.                         NSLog(@"%@", error.localizedDescription);
  71.                     } else {
  72.                         NSFileManager *fileManager = [NSFileManager defaultManager];
  73.                         NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path stringByAppendingString:attType]];
  74.                         [fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
  75.  
  76.                         NSError *attachmentError = nil;
  77.                         attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];
  78.                         if (attachmentError) {
  79.                             NSLog(@"%@", attachmentError.localizedDescription);
  80.                         }
  81.                     }
  82.                     completionHandler(attachment);
  83.                 }] resume];
  84. }
  85.  
  86.  
  87. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement