简介
UserNotifications.framework框架是iOS 10中一个重要的更新,它统一了旧版本杂乱的和通知相关的API.
iOS 10后最大的特色是可以支持多媒体推送,包括图像jpg、gif以及声音及视频等,另外还可以自定义推送视图、撤回通知以及在不增加推送信息数量的情况下更新推送内容等等。
快速集成步骤
1、新的请求推送权限
//弹出允许权限通知提示不应该在app启动的时候,应该在程序需要的地方调用,仅提示一次
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate=self;
UNAuthorizationOptions types10=UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//点击允许
NSLog(@"允许");
} else {
//点击不允许
NSLog(@"不允许");
}
}];
2、新的接收及处理推送信息
//iOS10新增:处理前台收到通知的代理方法
(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于前台时的远程推送接受
//关闭友盟自带的弹出框
[UMessage setAutoAlert:NO];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//应用处于前台时的本地推送接受
}
//当应用处于前台时提示设置,需要哪个可以设置哪一个
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
//iOS10新增:处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
[[YKPushNotificationManager shareInstance]dealWithNotificationBackgroundIniOS10:response Completion:completionHandler];
}
多媒体推送
1、工程中导入
UserNotifications.framework以及UserNotificationsUI.framework
2、在开发者账户中新建对应项目bundle ID对应的WildCardAppId:
如工程的bundle ID为:com.test.xxx
则wildCardAppID为 :com.test.xxx.*
3、如果需要在推送信息中显示图像、声音或者视频,需要建立NotificationService与NotificationContent两个target,其中NotificationServiceExtendsion用来处理推送中的多媒体资源,
NotificationContentExtentsion用来处理自定义视图。
新建的两个target都包含了info.plist文件,如果需要在推送中显示多媒体资源,多媒体中的链接要求为https,否则需要在需要在info.plist中把ATS Setting设置为yes。
ContentTarget中的info.plist内的UNNotificationExtensionCategory表示推送的类别标识,推送信息中带上这个标识可以显示自定义视图以及自定义的按钮交互。
NotificationServiceExtendsion中生成了两个自带的方法,
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)requestwithContentHandler:用于处理接受推送后用来请求媒体资源,
另一个用于请求超时的处理操作
- (void)serviceExtensionTimeWillExpire
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];
self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title];
NSDictionary *apsDic = [request.content.userInfo objectForKey:@"aps"];
//设定标识交互
NSString *category = [apsDic objectForKey:@"category"];
self.bestAttemptContent.categoryIdentifier = category;
NSString *imageUrl = [apsDic objectForKey:@"image"];
NSString *gifUrl = [apsDic objectForKey:@"gif"];
NSString *videoUrl = [apsDic objectForKey:@"video"];
NSString *audioUrl = [apsDic objectForKey:@"audio"];
NSString *typeString;
NSURL *url;
if (imageUrl.length > 0)
{
url = [NSURL URLWithString:imageUrl];
typeString = @"jpg";
}
if (gifUrl.length > 0)
{
url = [NSURL URLWithString:gifUrl];
typeString = @"gif";
}
if (videoUrl.length > 0)
{
url = [NSURL URLWithString:videoUrl];
typeString = @"mp4";
}
if (audioUrl.length > 0)
{
url = [NSURL URLWithString:audioUrl];
typeString = @"mp3";
}
NSURLSession *session = [NSURLSession sharedSession];
if (url)
{
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url completionHandler:
^(NSURL * _Nullable location,
NSURLResponse * _Nullable response,
NSError * _Nullable error)
{
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *toPath = [caches stringByAppendingString:[NSString stringWithFormat:@".%@",typeString]];
NSLog(@"fileToPath = %@",toPath);
// 将临时文件剪切或者复制Caches文件夹
NSFileManager *mgr = [NSFileManager defaultManager];
// AtPath : 剪切前的文件路径
// ToPath : 剪切后的文件路径
[mgr moveItemAtPath:location.path toPath:toPath error:nil];
if (toPath && ![toPath isEqualToString: @""])
{
UNNotificationAttachment *attch = [UNNotificationAttachment attachmentWithIdentifier:@""
URL:[NSURL URLWithString:[@"file://" stringByAppendingString:toPath]]
options:nil
error:nil];
if(attch)
{
self.bestAttemptContent.attachments = @[attch];
}
}
self.contentHandler(self.bestAttemptContent);
}];
[downloadTask resume];
}
else
{
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);
}
本地推送
//能够通过identitifier来区别其他通知,并且能够进行本地消息的更新、寻找、删除等,其中更新通知能够在不增加推送条数的情况下更新内容。
NSString *requestIdentifier = @"sampleRequest";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier
content:content
trigger:trigger1];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
}];
category id 必须对应 Notification info.plist 里面才能显示自定义交互
每一个推送内容最好带上一个catagory,这样方便我们根据不同的categoryId来自定义不同类型的视图,以及自定义交互。
有可能遇到的坑:
一、不能收到多媒体信息,仍然显示普通文本
解决方法:
1、推送信息中检查是否包含: "mutable-content" = 1 ,通知权限要设置UNNotificationPresentationOptionAlert
2、更换不同的系统版本,在测试中发现10.3系统终端能收到,10.2.1系统不能收到
二、调试的时候要选择对应的target才能断点