iOS NotifacationServiceExtension 通知显示多媒体内容

#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];
    
    // Modify the notification content here...
    self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
    
    NSDictionary *dict =  self.bestAttemptContent.userInfo;

    //    NSDictionary *notiDict = dict[@"aps"];

    NSString *mediaUrl = [NSString stringWithFormat:@"%@",dict[@"media"][@"url"]];

    NSLog(@"%@",mediaUrl);

    if (!mediaUrl.length) {

    self.contentHandler(self.bestAttemptContent);

    }

    [self loadAttachmentForUrlString:mediaUrl withType:dict[@"media"][@"type"] completionHandle:^(UNNotificationAttachment *attach) {

    if (attach) {

        self.bestAttemptContent.attachments = [NSArray arrayWithObject:attach];

    }

        self.contentHandler(self.bestAttemptContent);
    
    }];
//    self.bestAttemptContent.categoryIdentifier = @"myNotificationCategory";
//
//    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.
    self.contentHandler(self.bestAttemptContent);
}
//处理视频,图片的等多媒体

- (void)loadAttachmentForUrlString:(NSString *)urlStr
                          withType:(NSString *)type
                  completionHandle:(void(^)(UNNotificationAttachment *attach))completionHandler{

        __block UNNotificationAttachment *attachment = nil;

        NSURL *attachmentURL = [NSURL URLWithString:urlStr];

        NSString *fileExt = [self fileExtensionForMediaType:type];

        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];

}

- (NSString *)fileExtensionForMediaType:(NSString *)type {

    NSString *ext = type;

    if ([type isEqualToString:@"image"]) {

        ext = @"jpg";

    }

    if ([type isEqualToString:@"video"]) {

        ext = @"mp4";

    }

    if ([type isEqualToString:@"audio"]) {

        ext = @"mp3";

    }

    return [@"." stringByAppendingString:ext];

}
@end

NSAppTransportSecurity

    NSAllowsArbitraryLoads
    

{
    "aps": {
        "alert": {
            "title": "通知title",
            "body": "通知详情"
        },
        "mutable-content": 1,
        "category": "myNotificationCategory"
    },
    "media": {
        "url": "http://img.cdynyqkj.cn/idImage/16564830005.png",
        "type": "image"
    }
}

你可能感兴趣的:(iOS NotifacationServiceExtension 通知显示多媒体内容)