我们首先要在AppDelegate里面进行iOS的适配,可以参考这篇文章
,如果已经适配过了请忽略。
Xcode打开项目,File-->New-->Target;
然后分别选UNNotificationServiceExtension、UNNotificationContent创建Target;
然后在UNNotificationServiceExtension的- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {}方法中添加下面的代码;
NSMutableArray *actionMutableArr = [[NSMutableArray alloc] initWithCapacity:1];
UNNotificationAction * actionA =[UNNotificationAction actionWithIdentifier:@"ActionA" title:@"不感兴趣" options:UNNotificationActionOptionAuthenticationRequired];
UNNotificationAction * actionB = [UNNotificationAction actionWithIdentifier:@"ActionB" title:@"不感兴趣" options:UNNotificationActionOptionDestructive];
UNNotificationAction * actionC = [UNNotificationAction actionWithIdentifier:@"ActionC" title:@"进去瞅瞅" options:UNNotificationActionOptionForeground];
UNTextInputNotificationAction * actionD = [UNTextInputNotificationAction actionWithIdentifier:@"ActionD" title:@"作出评论" options:UNNotificationActionOptionDestructive textInputButtonTitle:@"send" textInputPlaceholder:@"say some thing"];
[actionMutableArr addObjectsFromArray:@[actionA,actionB,actionC,actionD]];
if (actionMutableArr.count) {
UNNotificationCategory * notficationCategory = [UNNotificationCategory categoryWithIdentifier:@"categoryNoOperationAction" actions:actionMutableArr intentIdentifiers:@[@"ActionA",@"ActionB",@"ActionC",@"ActionD"] options:UNNotificationCategoryOptionCustomDismissAction];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:notficationCategory]];
}
上面的方法是添加推送消息下面的事件(进入应用查看,取消查看,快捷回复)的,如果你的应用不需要可以忽略;
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
self.bestAttemptContent.categoryIdentifier = @"categoryNoOperationAction";
// Modify the notification content here...
// self.bestAttemptContent.title = [NSString stringWithFormat:@"点击查看更多内容"];
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);
}];
//处理视频,图片的等多媒体
- (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];
}
上面的这两段代码是当推送消息来了后,我们将mdeia下的url内的文件下载到本地,然后将路径交给系统,进而实现推送多媒体文件的目的;
这里说一下必须注意的两个坑、个坑、坑:
1.将UNNotificationServiceExtension中的pilst文件中添加
(1)在Info.plist中添加NSAppTransportSecurity类型Dictionary。
(2)在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES
这是因为从iOS9开始苹果不允许直接http访问,加上这两个字段就可以访问http,如果你不添加,只有你推送https的media文件才能被下载,http则不能被下载;
2.选中工程---> UNNotificationServiceExtension所对应的Target-->Deploy Target设置为iOS10,因为是从iOS10才支持推送多媒体文件,它默认是从当前Xocde支持的最高版本,比如小编的手机版本iOS10.0.2,它默认是iOS 10.2.0.刚开始小编没有修改,推送死活出不来图片,只有文字;后台检查才发现这里是从iOS 10.2.0支持的,心中一万个草泥马崩腾而过;
忙活了这么多还是看一下效果吧,我只做了图片与视频,声音应该差别不大。
自定义推送的UI
1.在UNNotificationServiceExtension的- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler方法内为self.bestAttemptContent添加categoryIdentifier
self.bestAttemptContent.categoryIdentifier = @"myNotificationCategory";
然后将这个categoryIdentifier粘贴在UNNotificationContent的infoplist内NSExtension-->NSExtensionAttributes-->UNNotificationExtensionCategory的字段内;然后在下面在添加一个字段UNNotificationExtensionDefaultContentHidden设置bool值为YES,这里是隐藏系统的UI;
2. 在上面的下载多媒体文件的- (void)loadAttachmentForUrlString:(NSString *)urlStr withType:(NSString *)type completionHandle:(void(^(UNNotificationAttachment *attach))completionHandler;方法内添加
NSMutableDictionary * dict = [self.bestAttemptContent.userInfo mutableCopy];
[dict setObject:[NSData dataWithContentsOfURL:localURL] forKey:@"image"];
self.bestAttemptContent.userInfo = dict;
我这里是将图片的Data数据放到推送的userInfo里面,然后在自定义UI的UNNotificationContent内获取这个Date,然后加载UI;
3. 在UNNotificationContent的maininterface.storyboard内部将UI做好,然后在UNNotificationContent的- (void)didReceiveNotification:(UNNotification *)notification ;方法内进行UI加载。
来看一下自定义UI的效果。
这里是Demo下载地址 ,如果有错误欢迎大家指出,谢谢。