一、 本地推送
1-1 本地推送逻辑
UNNotificationContent//推送内容
推送内容
标题/副标题/提醒/声音...
UNNotificationTrigger //推送触发者
UNPushNotificationTrigger
UNTimeIntervalNotificationTrigger //时间间隔触发器
UNCalendarNotificationTrigger //日历触发器
UNLocationNOtificationTrigger //位置触发器
UNNotificationRequest
封装Content 和 Trigger为统一格式
交给NotificationCenter处理
UNUserNotificationCenter
处理推送权限
接收和移除NotificationRequest
UNUserNotificationCenterDelegate
即将展示推送的处理
用户进行交互行为的处理
1-2 实现本地推送
#import "GTNotifation.h"
#import <UserNotifications/UserNotifications.h>
@interface GTNotifation()<UNUserNotificationCenterDelegate>
@end
@implementation GTNotifation
+ (GTNotifation *)notificationManager{ //设置单例
static GTNotifation *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[GTNotifation alloc] init];
});
return manager;
}
/// 检查申请用户通知权限
- (void)checkNotificationAuthorization{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted){
[self _pushLocalNotification];//触发一次推送
}
}];
}
/// 发送推送消息
-(void)_pushLocalNotification{
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.badge = @(1);
content.title = @"标题";
content.body = @"内容";
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval: 30.f repeats:NO];//30秒后;不重复触发
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"_pushLocalNotification" content: content trigger: trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error){
}];
}
#pragma mark - UNUserNotificationCenterDelegate
/// 将要展示通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert); //调用本地需要把Alert展示出来
}
/// 收到了通知--用户点击通知来到这里
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
//处理业务逻辑: 比如跳转页面
completionHandler();//调用系统的完成方法
}
@end
二、远程推送
1-1、APNs
APNs : Apple Push Notification services
APNs的作用:
防止每个App都要维持连接
保证连接带来安全性和性能的挑战
推送的流程
App 发送 UDID和BundleID 给APNs, 生成deviceToken
App 发送deviceToken 给服务器
服务器将信息和设备 deviceToken 发送给APNs
APNs 根据 deviceToken 进行推送
与本地推送不同:
推送数据不是由本地代码生成
代码中只需要接收和处理通知--UNUserNotificationCenterDelegate
需要服务器与APNs配合进行推送
证书的配置/Capabilities配置--后续的证书课程详细讲解
注册DeviceToken
注册获得DeviceToken
registerForRemoteNotifications
UIApplicationDelete 回调注册结果
推送服务基本流程:
本地: 权限申请--> 生成Content、选择Trigger、生成Request-->接收处理-->业务层回调
远程: 权限申请--> 远程推送注册-->APNs + 系统-->接收处理-->业务层回调
第三方的推送平台
极光推送/信鸽推送
无须自己维护后台/全平台(Android)
1-2 实现远程推送
✅在自定义的GTNotifation.m中:
#import "GTNotifation.h"
#import <UserNotifications/UserNotifications.h>
@interface GTNotifation()<UNUserNotificationCenterDelegate>
@end
@implementation GTNotifation
+ (GTNotifation *)notificationManager{ //设置单例
static GTNotifation *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[GTNotifation alloc] init];
});
return manager;
}
/// 检查申请用户通知权限
- (void)checkNotificationAuthorization{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted){
//必须要在主线程执行, 获取deviceToken
dispatch_async(dispatch_get_main_queue(), ^{
//(向远程推送注册deviceToken)向苹果的远程服务器APNs进行请求deviceToken
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
//我把deviceToken传给我们服务器, 我们服务器需要用这个deviceToken来告诉 苹果的APNs具体有哪些设备需要接收这条远程推送.
}
}];
}
/*可以用开源软件"Pusher"来模拟远程push:
在项目"Capabilities"-->开启"Push Notification"
注意:远程推送的内容需要Json的格式
选择推送证书
deviceToken
{"aps":{"alert:"推送标题","badge":1,"sound":"default"}}
*/
#pragma mark - UNUserNotificationCenterDelegate
/// 将要展示通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
completionHandler(UNNotificationPresentationOptionAlert); //调用本地需要把Alert展示出来
}
/// 收到了通知--用户点击通知来到这里
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler{
//处理业务逻辑: 比如跳转页面
completionHandler();//调用系统的完成方法
}
✅在AppDelegate.m中:
#pragma mark - UIApplicationDelegate
/// 远程推送服务器注册成功的回调, 并返回deviceToken
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
//GTNotifation中实现
}
/// 远程推送服务器注册失败的回调
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
}
/*总结:
远程推送证书&代码实现:
注册Token/自定义发送
接收推送消息进行业务处理
远程推送内容:
Json数据格式
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingNotificationPayload.html
远程推送调试工具:
Pusher
搭建自己的服务器平台
*/