前言
之前在《玩转推送通知》中介绍过iOS9推送中的新特性,由于iOS10系统的更新,苹果对推送这方面进行了一个大的更新,那么这篇文章便将介绍iOS10中如何去实现iOS9中对应的推送功能。
iOS10 注册推送通知方法
- 1.需要导入系统框架:UserNotifications.framework
- 2.实现代码如下
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// 必须写代理,不然无法监听通知的接收与点击
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//用户点击允许
NSLog(@"注册成功");
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@", settings);
}];
} else {
//用户点击不允许
NSLog(@"注册失败");
}
}];
iOS10推送通知之快捷点赞
先来效果图一张
iOS10中取消了通知消息左滑出按钮的方式,改为通过3D Touch的方式展现更多操作
添加赞事件代码如下
//创建事件
UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:kNotificationActionIdentifileStar title:@"赞" options:UNNotificationActionOptionDestructive];
//枚举options解释如下
/**
UNNotificationActionOptionAuthenticationRequired: 锁屏时需要解锁才能触发事件,触发后不会直接进入应用
UNNotificationActionOptionDestructive:字体会显示为红色,且锁屏时触发该事件不需要解锁,触发后不会直接进入应用
UNNotificationActionOptionForeground:锁屏时需要解锁才能触发事件,触发后会直接进入应用界面
*/
同iOS9中操作类似,创建完事件对象之后,需要创建动作集合,并进行注册
UNNotificationCategory *catetory = [UNNotificationCategory categoryWithIdentifier:kNotificationCategoryIdentifile actions:@[action1, action2] intentIdentifiers:@[kNotificationActionIdentifileStar, kNotificationActionIdentifileComment] options:UNNotificationCategoryOptionNone];
[center setNotificationCategories:[NSSet setWithObject:catetory]];
iOS10推送通知之快捷回复
效果图
添加评论事件代码如下
//创建事件
UNTextInputNotificationAction *action2 = [UNTextInputNotificationAction actionWithIdentifier:kNotificationActionIdentifileComment title:@"评论一下吧" options:UNNotificationActionOptionForeground textInputButtonTitle:@"评论" textInputPlaceholder:@"请输入评论"];
注册该事件同上,加入到UNNotificationCategory的对象中,进行注册
iOS10推送事件回调
iOS10中把通知回调整合成了两个方法
//应用程序在前台状态下收到推送消息的回调
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
//应用程序在后台状态下收到推送消息的回调
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
NSString *actionIdentifile = response.actionIdentifier;
if ([actionIdentifile isEqualToString:kNotificationActionIdentifileStar]) {
[self showAlertView:@"点了赞"];
} else if ([actionIdentifile isEqualToString:kNotificationActionIdentifileComment]) {
[self showAlertView:[(UNTextInputNotificationResponse *)response userText]];
}
// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置,iOS10中如果该block中加入了UNNotificationPresentationOptionAlert 这个参数,则应用程序在前台状态时也会有横幅通知
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
}
关于推送里面的基础参数介绍请移步iOS开发 iOS10推送必看(基础篇),此处不再做详细介绍。
iOS10发送本地推送通知
废话不说,直接上代码
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.body = @"Body:夏目友人帐";
content.badge = @(1);
content.title = @"Title:夏目·贵志";
content.subtitle = @"SubTitle:三三";
content.categoryIdentifier = kNotificationCategoryIdentifile;
content.userInfo = @{kLocalNotificationKey: @"iOS10推送"};
//推送类型
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Test" content:content trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"iOS 10 发送推送, error:%@", error);
}];
到了这里忽然发现“啊咧,我的推送怎么没有纳兹咩萨玛的图片?”,不要急,继续看,要有纳兹咩的图片是需要代价的
- 必须是iOS10系统
- 项目中必须有对应的图片或者视频(本文只说本地通知,所以本地必须有)
- 最后就是你必须写对应的代码啊
iOS10推送通知之附件功能(图片或小视频)
//给创建的UNMutableNotificationContent对象,多增加一个属性即可
NSString *path = [[NSBundle mainBundle] pathForResource:@"0" ofType:@"mp4"];
NSError *error = nil;
//注意这个error必须要填
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"AttachmentIdentifile" URL:[NSURL fileURLWithPath:path] options:nil error:&error];
content.attachments = @[attachment];
关于远程推送如何添加action事件及附件
- 添加action事件
以极光推送为例
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;
//这个getNotificationCategorys的方法和上面本地通知的一样
entity.categories = [NSSet setWithObject:[self getNofiticationCategorys]];
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];