iOS 推送通知

一、 本地推送

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
    搭建自己的服务器平台
*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 人有生死。我有,我父母也有。 很快,我就要30了,我将要步入中年,担负起赡养的重担。在那之后,我讲不仅仅是我自己。...
    木槿无声阅读 515评论 0 0
  • 今晨,送走了令人瑟瑟发抖的严寒冻雨,迎来了阳光明媚风正好。欣喜上天的赐予,早早提前出门上班,途经的公园林荫...
    枫林醉晚阅读 347评论 0 3
  • 每个人的战争之抗癌食物 1. 橄榄油、菜籽油和亚麻籽油。 2. 绿茶可阻止组织入侵和血管生成。 3. 姜黄。由姜黄...
    风清扬大侠阅读 870评论 0 0
  • 最近跟老公讨论过一个问题:假如你知道一家商店的服务很差劲,但是你必须买的东西只能从这家店购买,你是去这家店买呢,还...
    静斋看雪阅读 183评论 0 1