一 通知的种类:
通知设计模式(NSNotification)
本地通知(UILocalNotification)
远程通知(APNs)
静默推送(Silent Notification)
通知设计模式:是一种设计模式,是一种设计思想,是抽象的,推送通知(本地和远程)是肉眼可以看到的,是有界面的。
本地推送通知:本地通知不需要连接网络,一般是开发人员在合适的情况下在App内发送通知,应用场景:当能够确定在某个时间时需要提醒用户。
远程通知:远程通知必须需要连接网络,远程推送服务又称为APNs(Apple Push Notification Services),一般是服务器端发送通知。
二 :推送通知的使用场景:
一些任务管理App,会在任务时间即将到达时,通知你该做任务了。如:提醒事项App、 电影App:电影即将开始提示用户按时到达影院。
聊天App:程序退出到后台或者完全退出时收到消息
电商App:推荐新品时
新闻App:推送新闻
三 :推送通知的作用?
在App退到后台或者完全退出时,可以使用通知来告诉用户某件事情,比如推送新的聊天消息、新闻等
通知对应的效果:
1.在主屏幕的顶端会出现通知消息
2.当手机锁屏时出现在锁屏界面,可以通过滑动打开该App,
3.在通知中心中出现推送的消息
4.App图标会有微标值① badgeValue
5.推送通知提示音
注意:发送通知时,如果程序正在前台允许,那么推送通知UI就不会显示出来;点击通知系统默认会打开该App。
四 :本地通知(UILocalNotification)
//第一步:注册通知
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// 必须写代理,不然无法监听通知的接收与点击
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// 点击允许
NSLog(@"request authorization successed!");
}
}];
//之前注册推送服务,用户点击了同意还是不同意,以及用户之后又做了怎样的更改我们都无从得知,现在 apple 开放了这个 API,我们可以直接获取到用户的设定信息了。
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@",settings);
}];
//第二步:新建通知内容对象
self.content = [[UNMutableNotificationContent alloc] init];
//引入代理
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
self.content.title = @"iOS10通知";
self.content.subtitle = @"新通知学习笔记";
self.content.body = @"新通知变化很大,之前本地通知和远程推送是两个类,现在合成一个了。";
self.content.badge = @1;
UNNotificationSound *sound = [UNNotificationSound soundNamed:@"caodi.m4a"];
self.content.sound = sound;
//第三步:通知触发机制。 5秒钟后提醒
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
// 每小时重复 1次(循环)
// UNTimeIntervalNotificationTrigger *trigger2 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3600 repeats:YES];
//每周一早上 8:00提醒我
// NSDateComponents *components = [[NSDateComponents alloc] init];
// components.weekday = 2;
// components.hour = 8;
// UNCalendarNotificationTrigger *trigger3 = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
NSString *requertIdentifier = @"RequestIdentifier";
//第四步:创建UNNotificationRequest通知请求对象
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:self.content trigger:trigger1];
//第五步:将通知加到通知中心
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"Error:%@",error);
}];
五 : 远程通知(APNs)
远程通知实现:Ios10推出了全新的UserNotification框架(Ios10之前属于UIKit框架)。
1,在AppDelegate中,遵循代理UNUserNotificationCenterDelegate,导入头文件:
#import <UserNotifications/UserNotifications.h>
2,在application:didFinishLaunchingWithOptions方法中, 注册远程通知
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {//请求通知权限, 本地和远程共用
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOLgranted,NSError* _Nullable error) {
if(granted) {NSLog(@"请求成功");
}else
{ NSLog(@"请求失败");
}
}];//注册远程通知
[[UIApplicationsharedApplication] registerForRemoteNotifications];
//设置通知的代理
center.delegate =self;
returnYES;
}
3,在接收远程推送的DeviceToken方法中, 获取Token
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
//将来需要将此Token上传给后台服务器
NSLog(@"token:%@", deviceToken);
}
ios10远程通知的处理方法
以及实现下面实现3个方法, 用于处理点击通知时的不同情况的处理(这3个方法都是UNUserNotificationCenterDelegate协议的方法)
willPresentNotification:withCompletionHandler 用于前台运行
didReceiveNotificationResponse:withCompletionHandler 用于后台及程序退出
didReceiveRemoteNotification:fetchCompletionHandler: 用于静默推送
/**
前台运行 会调用的方法
iOS10之前, 在前台运行时, 不会出现通知的横幅
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary*userInfo = notification.request.content.userInfo;
//可以设置当收到通知后, 有哪些效果呈现(声音/提醒/数字角标)
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
/**
后台运行及程序退出 会调用的方法
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSDictionary*userInfo = response.notification.request.content.userInfo;
completionHandler();
}
六 : 静默推送(Silent Notification)
静默通知:app死掉的时候不进行,属于特殊的远程推送通知,其目的不是为了弹出通知框提醒用户,而是用于后台运行的App和服务端同步数据。例:App在后台放置一段时间,网络已不再活跃,App内数据可能已经过时;服务端可推送一条携带参数的静默通知,处于后台的App可以触发静默通知回调,在后台运行状态下获取对应参数并发起网络请求,获取最新数据更新,整个过程用户无感知。iOS7以后出现, 不会出现提醒及声音.
静默通知限制和注意事项:
静默通知主要用于更新和同步数据,用户对其无感知,因此静默通知一般不设置通知内容、声音和角标;静默通知唤醒后台App并执行下载任务时,最多有30秒时间执行;App处于前台/后台时均可触发对应通知回调,App关闭后不能触发;静默通知请求在APNs属于低优先级任务,苹果不保证静默通知的到达率;不要利用静默通知对App进行保活,APNs若检测到较高频率的静默通知发送请求,可能会终止其发送(具体策略苹果未公开)。
实现
静默推送不仅在定义上和其他的推送方式不同,在推送内容上也和其他推送不同。在后台给应用的推送内容中只要满足下面的条件,该推送就是静默推送。要求:
推送的payload中不能包含alert及sound字段,需要添加content-available字段, 并设置值为1。
例如: {"aps":{"content-available":"1"},"PageKey”":"2"} 。因为静默推送的关键意义在于后台的推松,所以一定要记得打开app后台推送的功能。
普通的推送-----{
"aps" : {
"alert" : {
"title" :"title",
"subtitle" :"subtitle",
"body" :"Copyright © 2016年 Hong. All rights reserved."
},
"badge" : 1,
"sound":"default"
},
}
静默推送--------
APNS去掉alert、badge、sound字段实现静默推送,增加增加字段:"content-available":1,也可以在后台做一些事情。
{
"aps ":{
"content-available":1,
"message":"we are here",
}
}
/**
iOS10中, 此方法主要处理静默推送 --> iOS7以后出现, 不会出现提醒及声音
1. 推送的payload中不能包含alert及sound字段
2. 需要添加content-available字段, 并设置值为1
例如: {"aps":{"content-available":"1"},"PageKey":"2"}
*/
//如果是以前的旧框架, 此方法 前台/后台/退出/静默推送都可以处理- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler{//静默推送
completionHandler(UIBackgroundFetchResultNewData);
}