|
|
发表于 2016-9-28 18:03:47
|未经授权,严禁转载,违者必究...
|
|阅读模式
推送 动静 秘笈 iOS UserNotifications usernotification usernotifications ios usernotification ios 推送 userinfo ios 10 推送 userinfo 消息推送 android 消息推送 ios 消息推送 消息推送平台
媒介
之前说会零丁清算动静通知的内容,可是因为工(就)作(是)的(很)事(懒)没有更新文章,违反了本身的进修的初志。因为互联网必然要有危机意识,说不定眼一睁,我们就out丢了饭碗。
“狼,他没有狮子山君强壮,也没有年夜象那重大的身躯,但至少:我从来没在马戏团看到过他们的身影。”
iOS10 新特征一出,各个年夜神就早已研究新特征能给场景智能化所带来的益处(唉,可惜我只是一个小白)。我也被放置适配iOS10的推送工作!
iOS 10 中将之前繁杂的推送通知同一成UserNotifications.framework 来集中治理和利用通知功能,还增添一些适用的功能——撤回单条通知、更新已展示通知、半途点窜通知内容、在通知中显示多媒体资本、自界说UI等功能,功能实在壮大!
iOS推送分为Local Notifications(当地推送) 和 Remote Notifications(长途推送)(道理图来历于收集,若有侵权请奉告,我会添加来历,我怕我赔不起)
图中,Provider是指某个iPhone软件的Push办事器,这篇文章我将利用我花了12块年夜洋(心疼)买的 APNS Pusher 作为我的推送源。
上图可以分为三个阶段:
从上图我们可以看到:
1、 假如你的App有远端推送的话,那你需要开辟者账号的,需要新建一个对应你bundle的push 证书。证书这一块我就不说了,假如针对质书有什么问题可以给我留言,我会零丁把证书相关的常识点清算起来!当然本人时很是喜好的分享的(又装逼),假如你没有账号,我可以我测试用的证书发给你,用于你的测试和进修,私聊我。
2、 Capabilities中打开Push Notifications 开关
在XCode7中这里的开关不打开,推送也是可以正常利用的,可是在XCode8中,这里的开关必需要打开,否则会报错:
打开后会主动在项目里生成entitlements文件。
3、 推送的注册
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import [U]
#endif
第二步:我们需要在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中注册通知,代码如下
上面需要留意:
4、 远端推送需要获取设备的Device Token的方式是没有变的,代码如下
5、这一步吊了,这是iOS 10系统更新时,苹果给了我们2个代办署理方式来处置通知的领受和点击事务,这两个方式在[U]的和谈中,大师可以查看下。
此外,苹果把当地通知跟长途通知合二为一。区分当地通知跟长途通知的类是UNPushNotificationTrigger.h类中,UNPushNotificationTrigger的类型是新增添的,经由过程它,我们可以获得一些通知的触发前提 ,注释如下:
#pragma mark - iOS10 收到通知(当地和远端) UNUserNotificationCenterDelegate
//App处于前台领受通知时
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
//收到推送的请求
UNNotificationRequest *request = notification.request;
//收到推送的内容
UNNotificationContent *content = request.content;
//收到用户的根基信息
NSDictionary *userInfo = content.userInfo;
//收到推送动静的角标
NSNumber *badge = content.badge;
//收到推送动静body
NSString *body = content.body;
//推送动静的声音
UNNotificationSound *sound = content.sound;
// 推送动静的副题目
NSString *subtitle = content.subtitle;
// 推送动静的题目
NSString *title = content.title;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//此处省略一万行需求代码。。。。。。
NSLog(@"iOS10 收到长途通知:%@",userInfo);
}else {
// 判定为当地通知
//此处省略一万行需求代码。。。。。。
NSLog(@"iOS10 收到当地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
// 需要执行这个方式,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
completionHandler(UNNotificationPresentationOptionBadge|
UNNotificationPresentationOptionSound|
UNNotificationPresentationOptionAlert);
}
//App通知的点击事务
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
//收到推送的请求
UNNotificationRequest *request = response.notification.request;
//收到推送的内容
UNNotificationContent *content = request.content;
//收到用户的根基信息
NSDictionary *userInfo = content.userInfo;
//收到推送动静的角标
NSNumber *badge = content.badge;
//收到推送动静body
NSString *body = content.body;
//推送动静的声音
UNNotificationSound *sound = content.sound;
// 推送动静的副题目
NSString *subtitle = content.subtitle;
// 推送动静的题目
NSString *title = content.title;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
NSLog(@"iOS10 收到长途通知:%@",userInfo);
//此处省略一万行需求代码。。。。。。
}else {
// 判定为当地通知
//此处省略一万行需求代码。。。。。。
NSLog(@"iOS10 收到当地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo);
}
//2016-09-27 14:42:16.353978 UserNotificationsDemo[1765:800117] Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.
completionHandler(); // 系统要求执行这个方式
}
需要留意的:
6、 iOS 10之前领受通知的兼容方式
段结:是不是觉得就竣事了?NO NO NO(你觉得分开了幻景,其实才方才踏入幻景!)上面的介绍了根基道理、根基设置装备摆设以及根基方式申明,此刻做完这些工作,我们的进修才方才最先!此刻天时、地利、人和、可以最先下面推送coding的进修和测试了。
我们先学会根基的技术简单的推送(爬),后面在进修进阶定制推送(走),最后看看能不克不及高级推送(飞不飞起来看小我了,我是飞不起来):
一、 根基的当地推送
1、新功能trigger可以在特定前提触发,有三类:UNTimeIntervalNotificationTrigger、UNCalendarNotificationTrigger、UNLocationNotificationTrigger
1.2 UNCalendarNotificationTrigger :挪用
+ (instancetype)triggerWithDateMatchingComponents:(NSDateComponents *)dateComponents repeats:(BOOL)repeats;进行注册;时候点信息用 NSDateComponents.(按期推送)
1.3、UNLocationNotificationTrigger:挪用
+ (instancetype)triggerWithRegion:(CLRegion *)region repeats:(BOOL)repeats;
进行注册,地域信息利用CLRegion的子类CLCircularRegion,可以设置装备摆设region属性 notifyOnEntry和notifyOnExit,是在进入地域、从地域出来或者两者都要的时辰进行通知,这个测试过程专门从公司跑抵家时刻存眷手机有推送嘛,公然是有的(定点推送)
2、建立推送的内容(UNMutableNotificationContent)
970779-1547493e70e42a53.jpg
装X决赛通知.jpg
二、 根基的远端推送
假如你想模拟远端推送,按照我前面介绍的设置装备摆设根基情况、证书、push开关和根基方式就可以模拟远端的根基远端推送。
1、运行工程则会拿到设备的Device Token,后面会用到。
2、此刻我们需要一个推送办事器给APNS发送信息。我前面说了我花了12块年夜洋(心疼死我了)买了一个APNS pusher 来模拟远端推送办事,当然你可以不花钱也可以用到,例如:
970779-5b3ffa741642c78e.png
APNS pusher
3、你需要把你方才获取的device token填到响应位置,同时你要设置装备摆设好push证书哦。
5、电光石火你就收到了远端动静了
6、Notification Management
对推送进行查、改、删。都需要一个必需的参数requestIdentifier
Remote Notification 更新需要经由过程新的字段apps-collapse-id来作为独一标示,我前面用的APNS pusher暂不撑持这个字段,不外github上有良多如许的东西:
970779-b635e34e6d355d76.jpg
推送图片.jpg
970779-c64d525923dd8079.jpg
推送图片2.jpg
参考资料:
https://developer.apple.com/reference/usernotifications
http://www.jianshu.com/p/b74e52e866fc
http://www.jianshu.com/p/b74e52e866fc
http://blog.csdn.net/he317165264/article/details/52574934
http://qoofan.com/read/PnEaMEZonD.html
http://www.qingpingshan.com/rjbc/ios/140921.html
|
推送, 动静, 秘笈, iOS, UserNotifications, usernotification, usernotifications, ios, userinfo, 消息推送, android, 消息推送平台
|