iOS8自定义推送显示按钮及推送优化
http://www.jianshu.com/p/803bfaae989e
iOS8自定义推送显示按钮及推送优化
导语
在iOS8中,推送消息不再只是简单地点击打开客户端,对推送消息下拉时还可以执行预先设定好的操作,接下来我们来介绍如何自定义推送信息显示按钮和对推送的一些优化策略。
注册推送
在iOS8中,我们使用新的函数来注册通知,如下:
- (void)registerForRemoteNotifications NS_AVAILABLE_IOS(8_0);
该函数的作用是向苹果服务器注册该设备,注册成功过后会回调
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0);
注册失败则回调
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0);
执行registerForRemoteNotifications只是完成了与APNS的注册交互,接下来还要设置推送的类型和策略。如果没有设置则接收到的消息都是以静默的方式接收。
设置推送类型
- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0);
在这里我们用到了UIUserNotificationSettings这个新类,苹果对其解释是这样的。
A UIUserNotificationSettings object encapsulates the types of notifications that can be displayed to the user by your app. Apps that use visible or audible alerts in conjunction with a local or push notification must register the types of alerts they employ. UIKit correlates the information you provide with the user’s preferences to determine what types of alerts your app is allowed to employ.
简单来说就是我们可以自行注册推送的提醒类型。再来看看UIUserNotificationSettings为我们提供了那些函数。查看API,我们只找到了
+ (instancetype)settingsForTypes:(UIUserNotificationType)types
categories:(NSSet *)categories;
UIUserNotificationType就是以往我们设定的推送声音、推送数量Badge和是否Alter的参数组合。因为它们是NS_OPTIONS类型,所以是可以多选的。
我们重点放在categories上。可以看到,categories的类型是一个集合(NSSet *),也就是说我们可以设置多个推送策略。继续查找API,我们找到了UIUserNotificationCategory。苹果的说明是这样的
A UIUserNotificationCategory object encapsulates information about custom actions that your app can perform in response to a local or push notification. Each instance of this class represents a group of actions to display in conjunction with a single notification. The title of each action is uses as the title of a button in the alert displayed to the user. When the user taps a button, the system reports the selected action to your app delegate.
就是说我们对每一条推送信息可以设置一组行为,行为以按钮方式显示。当我们点击按钮时会调用app delegate的代理方法。
查看UIUserNotificationCategory相关属性
@property (nonatomic, copy, readonly) NSString *identifier;
- identifier:策略标识,在推送时用来决定客户端显示哪种推送策略,稍后会介绍。
看到该属性是只读的,我们在自定义策略时使用的是UIMutableUserNotificationCategory来设置,设置方法如下:
- (void)setActions:(NSArray *)actions forContext:(UIUserNotificationActionContext)context;
对一个策略我们可以设置多个行为,使用的是UIUserNotificationAction。
查看UIUserNotificationAction相关属性
// The unique identifier for this action.
@property (nonatomic, copy, readonly) NSString *identifier;
// The localized title to display for this action.
@property (nonatomic, copy, readonly) NSString *title;
// How the application should be activated in response to the action.
@property (nonatomic, assign, readonly) UIUserNotificationActivationMode activationMode;
// Whether this action is secure and should require unlocking before being performed. If the activation mode is UIUserNotificationActivationModeForeground, then the action is considered secure and this property is ignored.
@property (nonatomic, assign, readonly, getter=isAuthenticationRequired) BOOL authenticationRequired;
// Whether this action should be indicated as destructive when displayed.
@property (nonatomic, assign, readonly, getter=isDestructive) BOOL destructive;
- identifier:行为标识符,用于调用代理方法时识别是哪种行为。
- title:行为名称。
- UIUserNotificationActivationMode:即行为是否打开APP。
- authenticationRequired:是否需要解锁。
- destructive:这个决定按钮显示颜色,YES的话按钮会是红色。
同样由于这些属性都是只读的,我们使用UIMutableUserNotificationAction来生成自定义行为。
编码
我们设置两种推送策略,每种策略分别设置两种行为。代码如下:
if(8.0 <= [UIDevice currentDevice].systemVersion.doubleValue)
{
[[UIApplication sharedApplication] registerForRemoteNotifications];
UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init];
action1.identifier = @"action1";
action1.title=@"策略1行为1";
action1.activationMode = UIUserNotificationActivationModeForeground;
action1.destructive = YES;
UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init];
action2.identifier = @"action2";
action2.title=@"策略1行为2";
action2.activationMode = UIUserNotificationActivationModeBackground;
action2.authenticationRequired = NO;
action2.destructive = NO;
UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init];
category1.identifier = @"Category1";
[category1 setActions:@[action2,action1] forContext:(UIUserNotificationActionContextDefault)];
UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init];
action3.identifier = @"action3";
action3.title=@"策略2行为1";
action3.activationMode = UIUserNotificationActivationModeForeground;
action3.destructive = YES;
UIMutableUserNotificationAction * action4 = [[UIMutableUserNotificationAction alloc] init];
action4.identifier = @"action4";
action4.title=@"策略2行为2";
action4.activationMode = UIUserNotificationActivationModeBackground;
action4.authenticationRequired = NO;
action4.destructive = NO;
UIMutableUserNotificationCategory * category2 = [[UIMutableUserNotificationCategory alloc] init];
category2.identifier = @"Category2";
[category2 setActions:@[action4,action3] forContext:(UIUserNotificationActionContextDefault)];
UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings: uns];
}
关于推送证书制作请百度,这里推荐一个推送测试工具。
- 推送策略一
{
"aps":{
"alert":"推送内容",
"sound":"default",
"badge":0,
"category":"Category1"
}
}显示结果:
策略一 - 推送策略二
{
"aps":{
"alert":"推送内容",
"sound":"default",
"badge":0,
"category":"Category2"
}
}显示结果:
策略二
点击相应按钮会激活代理方法
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0);
这里我们根据传过来的identifier确定点击了哪个按钮,并执行相应操作。
推送优化
苹果APNS对推送内容大小限制不能超过256个字节(现在这个限制好像放宽了)。如果推送消息内容过多,不仅会造成推送延迟,还会消耗流量。对于推送信息中重复的文本内容,我们可以在本地字符串strings中自定义键值动态设定参数来完成推送。
Localizable.strings中添加:
"pushkey" = "%@ 的iOS8自定义推送显示按钮及推送优化教程 %@是一名iOS开发者,正在前行。";
设置推送信息
{
"aps":{
"alert":{"loc-args":["Arms","Arms"],"loc-key":"pushkey"},
"sound":"default",
"badge":0,
"category":"Category2"
}
}
显示结果:

iOS8自定义推送显示按钮及推送优化的更多相关文章
- C# ASP.NET MVC 之 SignalR 学习 实时数据推送显示 配合 Echarts 推送实时图表
本文主要是我在刚开始学习 SignalR 的技术总结,网上找的学习方法和例子大多只是翻译了官方给的一个例子,并没有给出其他一些经典情况的示例,所以才有了本文总结,我在实现推送简单的数据后,就想到了如何 ...
- 【Android应用开发】 推送原理解析 极光推送使用详解 (零基础精通推送)
作者 : octopus_truth 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/45046283 推送技术产生场景 : -- ...
- 与众不同 windows phone (10) - Push Notification(推送通知)之推送 Tile 通知, 推送自定义信息
原文:与众不同 windows phone (10) - Push Notification(推送通知)之推送 Tile 通知, 推送自定义信息 [索引页][源码下载] 与众不同 windows ph ...
- 将网站固定到开始菜单,自定义图标、颜色和Windows推送通知
Windows 8.1——将网站固定到开始菜单,自定义图标.颜色和Windows推送通知 记得在IE 9和Windows 7刚出来那会儿我写过一篇文章来介绍如何自定义网站将其固定到Windows的任务 ...
- Android推送服务——百度云推送
一.推送服务简介 消息推送,顾名思义,是由一方主动发起,而另一方与发起方以某一种方式建立连接并接收消息.在Android开发中,这里的发起方我们把它叫做推送服务器(Push Server),接收方叫做 ...
- Android之使用个推实现三方应用的推送功能
PS:用了一下个推.感觉实现第三方应用的推送功能还是比较简单的.官方文档写的也非常的明确. 学习内容: 1.使用个推实现第三方应用的推送. 所有的配置我最后会给一个源代码,内部有相关的配置和 ...
- 左右推拽显示对比图 - jQyery封装 - 附源文件
闲来无事,做了一个模块效果 左右拖拽显示对比图,是用jq封装的 利用鼠标距离左侧(0,0)坐标的横坐标位移来控制绝对定位的left值 再配合背景图fixed属性,来制作视觉差效果 代码如下 <! ...
- PhoneGap 的消息推送插件JPush极光推送
一. 什么是极光推送 极光推送,使得开发者可以即时地向其应用程序的用户推送通知或者消息,与用户保持互动, 从而有效地提高留存率,提升用户体验.平台提供整合了 Android 推送.iOS 推送的统一推 ...
- php 微信客服信息推送失败 微信重复推送客服消息 40001 45047
/*** * 微信客服发送信息 * 微信客服信息推送失败 微信重复推送客服消息 40001 45047 * 递归提交到微信 直到提交成功 * @param $openid * @param int $ ...
随机推荐
- python线程死锁与递归锁
死锁现象 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去. 此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待 ...
- SpringMVC初探-HelloWorld
MVC的概念 MVC是一种设计模式,即Model--View-Controller,模型--视图--控制器 Model(模型)表示应用程序核心(比如数据库记录列表). View(视图)显示数据(数据库 ...
- 6_文件IO
1. 基本文件读取 readline(),readlines(),write(),writelines() f.read(size),指定读取文件的字节数,需要注意的是 ...
- odoo开发笔记 -- odoo10 视图界面根据字段状态,动态隐藏创建&编辑按钮
场景描述: 解决方式: 网络搜索,vnsoft_form_hide_edit 找到了这个odoo8的模块, odoo10语法和视图界面有新的变化,所以需要修改一些地方,感兴趣的小伙伴可以对比下两个代码 ...
- odoo第三方市场 -- 模块推荐
odoo 除了开源,另一个非常给力的地方就是,强大的第三方应用市场: 你入坑后,会发现非常的好玩,全球还有这么多小伙伴并肩前行,共同成长. 第三方市场有很多不错的模块,当然,好东西,不是完全免费的! ...
- 剑指offer十二之数值的整数次方
一.题目 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.思路 1.传统方法计算,时间复杂度O(n) 2.递归方式计算,时间复杂度O ...
- 阿里云安装配置Ghost
阿里云手动重装系统N次了,折腾不止. 系统环境 CentOS 6.3 X64 , 两块硬盘 系统 +数据盘 #重新挂载硬盘 [root@AY14040623435015772eZ ~]# fdisk ...
- Charles抓包实战详解
访问我的博客 前言 通过上一篇文章,想必你已经掌握了如何正确安装抓包神器 Charles,如果还是抓不了包,可以再看看. 今天要做是抓包实战,因为我在做网络文学的公司就职,所以就拿网络文学的 APP ...
- Ceph/共享存储 汇总
Ceph 存储集群 - 搭建存储集群 Ceph 存储集群 - 存储池 Ceph 块设备 - 命令,快照,镜像 Ceph 块设备 - 块设备快速入门 OpenStack 对接 Ceph CentOS7 ...
- js设计模式总结1
js设计模式有很多种,知道不代表会用,更不代表理解,为了更好的理解每个设计模式,对每个设计模式进行总结,以后只要看到总结,就能知道该设计模式的作用,以及模式存在的优缺点,使用范围. 本文主要参考张容铭 ...