APNs推送的系统做法
1、
#pragma mark - 远程推送注册获得device Token
if (IOS_VERSION >= 10.0) { UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
[center setDelegate:self];
UNAuthorizationOptions type = UNAuthorizationOptionBadge | UNAuthorizationOptionSound |UNAuthorizationOptionAlert; [center requestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) {
NSLog(@"注册成功");
}else{
NSLog(@"注册失败");
} }]; }else if (IOS_VERSION >= 8.0){ UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound |
UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes
categories:nil];
[application registerUserNotificationSettings:settings]; }else{
//ios8以下不适配了
}
// 注册获得device Token
[application registerForRemoteNotifications];
2、
#pragma mark - 远程推送注册获得device Token
// 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *deviceTokenStr = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
} // 注册deviceToken失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"注册deviceToken失败error -- %@",error);
}
3、
#pragma mark - 远程推送消息的处理 - iOS10以后
//在前台
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
completionHandler(UNNotificationPresentationOptionBadge |
UNNotificationPresentationOptionSound |
UNNotificationPresentationOptionAlert); } //在后台、关闭状态
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
//处理推送过来的数据
NSLog(@"%@", response.notification.request.content.userInfo);
completionHandler();
} #pragma mark - 远程推送消息的处理 - iOS10之前
//在前台、在后台、关闭状态统一进入该方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary * _Nonnull)userInfo fetchCompletionHandler:(void (^ _Nonnull)(UIBackgroundFetchResult))completionHandler{ /*
UIApplicationStateActive 应用程序处于前台
UIApplicationStateBackground 应用程序在后台,用户从通知中心点击消息将程序从后台调至前台
UIApplicationStateInactive 用用程序处于关闭状态(不在前台也不在后台),用户通过点击通知中心的消息将客户端从关闭状态调至前台
*/ //应用程序在前台给一个提示特别消息
if (application.applicationState == UIApplicationStateActive) { //应用程序在前台
NSLog(@"应用程序在前台%@", userInfo); }else{ //其他两种情况,一种在后台程序没有被杀死,另一种是在程序已经杀死。用户点击推送的消息进入app的情况处理。
NSLog(@"应用程序在后台、关闭状态%@", userInfo); } completionHandler(UIBackgroundFetchResultNewData); }
APNs推送的系统做法的更多相关文章
- APNs 推送原理及问题
http://bbs.csdn.net/topics/390461996 在 iOS 平台上,大部分应用是不允许在后台运行并连接网络的.在应用没有被运行的时候,只能通过 Apple Push Noti ...
- 手把手教你配置苹果APNS推送服务|钿畑的博客 | 钿畑的博客
http://www.360doc.com/content/15/0118/17/1073512_441822850.shtml# 钿畑的文章索引 1. 什么是推送通知 2. 什么是APNS? 3. ...
- APNS推送原理详解
推送是解决轮询所造成的流量消耗和电量消耗的一个比较好的解决方案,在Android上,虽然Google提供了GCM(之前为C2DM),但在国内基本等于没用,各大Android应用基本都自己架设推送Ser ...
- iOS开发之功能模块--Apns推送中的的json格式介绍
在开发向苹果Apns推送消息服务功能,我们需要根据Apns接受的数据格式进行推送.下面接受我在进行apns推送时候总结的一点apns服务接受的Json数据格式 示例 1: 以下负载包含哦一个简单的 a ...
- Apns推送中的的json格式介绍
在开发向苹果Apns推送消息服务功能,我们需要根据Apns接受的数据格式进行推送.下面接受我在进行apns推送时候总结的一点apns服务接受的Json数据格式 示例 1: 以下负载包含哦一个简单的 a ...
- iOS 后台调用apns推送
1.java调用apns推送 2.php 调用apns 推送,可借助终端
- iOS apns推送
前言:推送分为本地推送以及远程推送. 两者的区别为本地推送一般为定时推送.定期推送或者位置推送.而远程推送更为多样化,能满足较高的要求.当然远程推送需要服务器端开发,开发流程较复杂. 1.本地推送只需 ...
- python3 三行代码基于HTTP2完美实现APNS推送【详解】
第一次做苹果APNS(Apple Push Notification service)推送,关于APNS推送原理以及证书的获取方式网上已经有许多资料,在此不做过多赘述,需要注意的是证书分为测试证书和正 ...
- iOS 下APNS推送处理函数具体解释
相比起Android,iOS在推送方面无疑惯例得更好.APNS(Apple Push Notification Service)是苹果公司提供的消息推送服务.其原理就是.第三方应用将要推送给用户的信息 ...
随机推荐
- 【译】高级T-SQL进阶系列 (三)【上篇】:理解公共表表达式(CTEs)
[译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文链接:传送门. 伴随着SQL SERVER 2005的首次展示,微软介绍了一种新的被称为“公共表 表达式”(CTE)的查询结构. ...
- 容器远程访问vnc--CentOS 6.8安装和配置VNC
对于用惯了WIN系统的朋友来说,没有图形化操作界面的Linux用起来实在太难受了.实际上,Linux也是有图形化操作界面的,这就是VNC.接下来本文将告诉大家如何在CentOS 6.8下安装和配置 V ...
- mac下Red Hat 7.4服务器初始化
物料:VMware Fusion for Mac版 rhel-server-7.4-x86_64-dvd.iso 通过VMware安装好虚拟机,打开终端: 1.通过ifconfig查看ip和网 ...
- PyQt5控件支持拖拽方法
让控件支持拖拽动作A.setDragEnable(True) 设置A可以拖动B.setAcceptDrops(True) 设置B可以接受拖动B需要满足两个事件1.dragEnterEvent 将A拖到 ...
- pair node stack vector string priority_queue
multiset 元素重复 自动排序 map #include <bits/stdc++.h> using namespace std; map<int,int> s;//自当 ...
- hash路由
class HashRouter{ constructor(){ //用于存储不同hash值对应的回调函数 this.routers = {}; window.addEventListener('ha ...
- gitlab回退到某次commit——本地+远程
## 查看所有commits记录$ git log ## gitlab回退到某次commit$ git reset --hard 3018a546427e1f865524b82b488d6a2721d ...
- vue+element ui table组件封装,使用render渲染
后台管理经常会用到表格,一开始封装了一个常用的功能性表格,点击这里: 后来由于需求增加,在表格中还会用到switch,select,input等多种组件,每次都要在html中增加<el-tabl ...
- 国外最受欢迎的15个BT下载网站
1.EYH.BIZ 海盗湾(The Pirate Bay)现在在中国成立的一个分部 www.eyh.biz 一个提供BT种子文件和链接,以方便使用BT协议的对等文件共享网站.该网站于2003年在瑞典创 ...
- PSP第二次总结
项目计划总结: 姓名:李志强 日期:2017/12/06 听课 编程 阅读课本 准备考试 日总计 周日11.26 周一 100 100 周二 ...