• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

IOS推送

武飞扬头像
刘姥姥爱学习
帮助1

IOS推送

推送

●iOS不允许App常驻后台
●提供服务器主动与用户相互的功能
●新闻类(热点新闻的推送)

●本地推送

●由本地应用触发(闹铃/待办事项)
●无需网络数据,提供和远程推送统一的交互

●远程推送

●普通远程推送
●静默推送

UserNotifications

UserNotifications

●iOS10之后统一UserNotificatiohsframework
●需要兼容低版本机型
●UNUserNotificationCenter单例管理全部推送

●统一的权限的申请

●在每次App启动时调用requestAuthorizationWithOptions
●系统在首次会出现弹窗,之后保存用户选择
●远程/本地Options相同

大体流程

  1. 开发者生成
  2. UNNotificationContent && UNNotificationTrigger
  3. 封装成UNNotificationRequest
  4. 然后给UNUserNotificationCenter
  5. 最后调用UNUserNotificationCenterDelegate

每个的作用

本地推送
UNNotificationContent

●推送内容
●标题/副标题/提醒/声音…

UNNotificationTrigger

●UNPushNotificationTrigger
●UNTimeIntervalNotificationTrigger
●UNCalendarNotificationTrigger
●UNL ocationNotificationTrigger

UNNotificationRequest

●封装Content 和Trigger为统一格式
●交给NotificationCenter处理

UNUserNotificationCenter

处理推送权限
●接收和移除止NotificaitonRequest

UNUserNotificationCenterDelegate

●即将展示推送的处理
●用户进行交互行为的处理

本地推送代码

- (void)checkNotificationAuthorization{
    
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    center.delegate = self;
    [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
        //UNAuthorizationOptionBadge 右上角图标
        //UNAuthorizationOptionSound 声音
        //如果允许推送 granted == yes
        if (granted) {
            //本地推送
            [self _pushLocalNotification];

            });
        }
    }];
}
学新通

- (void)_pushLocalNotification{
    
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    content.badge = @(1);
    content.title = @"我是推送标题";
    content.body = @"我是需要推送的内容";
    content.sound = [UNNotificationSound defaultSound];
    //30秒推送一次 且不重复
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:30.f repeats:NO];
    
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"_pushLocalNotification" content:content trigger:trigger];
    
    
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        //
    }];
}

学新通
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    completionHandler(UNNotificationPresentationOptionAlert);
}


- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
    
    //处理badge展示逻辑
    //点击之后根据业务逻辑处理
    //[UIApplication sharedApplication].applicationIconBadgeNumber = 100;

    //处理业务逻辑
    completionHandler();
}

远程推送

推送数据不是由本地代码生成

●只需要接收和处理通知
●UNUserNotification CenterDelegate

需要服务器与APNs配合进行推送

●证书的配置/ Capabilities配置

●注册Device Token
●注册获得Device Token
●registerForRemoteNotifications
●UlApplicationDelegate回调注册结果

步骤
远程推送证书&代码实现

●注册Token/自定义发送
●接收推送消息进行业务处理

远程推送基本流程

权限申请,远程推送注册,本地推送的生成content选择Trigger生成request都变成了远程推送中用APNS 系统进行处理,最后接收处理,业务层回调
使用第三方推送平台:极光推送,信鸽推送,无需自己维护后台,全平台
远程推送内容

●Json 数据格式
●https://developer.apple.com/ibrary/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/
CreatingtheNotificationPayload.html

远程推送调试工具

●Pusher
●搭建自己的服务器平台

APNs是啥

Apple Push Notification services
●APNs的作用

●防止每个App都要维持连接
●保证连接带来安全性和性能的挑战

●推送的流程

●App发送UDID和BundlelD给APNs,生成device Token
●App发送deviceToken给服务器
●服务器将信息和设备device Token发送给APNs
●APNs根据device Token进行推送

远程推送代码

因为在application获取register需要在主线程执行

    dispatch_async(dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            });

在appdelegate中代理这两个方法

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    //尽量收敛到GTNotification中实现
    //注册成功
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //注册失败
}

剩下的将要弹出跟已经弹出推送的方法,跟本地推送一样

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaakkc
系列文章
更多 icon
同类精品
更多 icon
继续加载