iOS推送javaPNS源码解析二,消息体类

来源:互联网 发布:2017网络直播平台排名 编辑:程序博客网 时间:2024/05/22 23:47

消息体类是推送的基本组成部分,巧妇难为无米之炊,没消息体还推送神马。

消息体的顶级类是Payload,这是一个抽象类,作为基本的javabean,不需要什么其他的接口方法。

该类默认了消息体的实现是utf-8字符集,定义了消息体是一个JSONObject对象,说明了默认消息体失效时间是1天,类中payloadSizeEstimatedWhenAdding用于判断添加消息内容时候消息体长度是否过长,默认false;preSendConfiguration用于说明是否检测消息体为空。

set/get方法就不说了,构造函数也不说了就是实例化一个JSONObject。

public JSONObject getPayload(),返回自身

public void addCustomDictionary(String name, String value) throws JSONException ,增加String类型的字典属性和值
public void addCustomDictionary(String name, int value) throws JSONException ,增加int类型的字典属性和值。

public void addCustomDictionary(String name, List values) throws JSONException ,增加list类型的自定义字典属性和值。
public String toString() ,重写toString方法,也就是使用JSONObject的toString方法,打印json字符串。

void verifyPayloadIsNotEmpty() 如果设置检测消息体为检测,即preSendConfiguration!=0那么久判断是否为空,为空抛出异常

public byte[] getPayloadAsBytes() throws Exception,如果消息体长度大于消息允许的最大长度,抛出异常否则根据默认配置获取指定字符集的消息体byte[]格式,调用getPayloadAsBytesUnchecked() 。
private byte[] getPayloadAsBytesUnchecked() throws Exception ,获取消息体的byte[]形式,使用设置的字符集失败则默认执行。
 private boolean isPayloadTooLong() ,是否消息体的byte[]长度大于消息允许的最大值。
 public int estimatePayloadSizeAfterAdding(String propertyName, Object propertyValue)获取添加该属性后消息体长度。
 public boolean isEstimatedPayloadSizeAllowedAfterAdding(String propertyName, Object propertyValue) 是否允许添加该属性和属性值,是否添加后长度大于最大长度。
 private void validateMaximumPayloadSize(int currentPayloadSize) throws PayloadMaxSizeExceededException校验消息体长度。

 protected void put(String propertyName, Object propertyValue, JSONObject object, boolean opt) throws JSONException 属性放入消息体,opt确定调用存储函数,如果为true则为key/valu有为空的则不存储,为false则报异常。

 public int getMaximumPayloadSize()获取消息体最大长度,需要子类实现。

 public Payload asSimulationOnly() 获取仿真模式的消息体,不明白啥意思,因为里面就修改了一个失效时间,由一天设置为10天+。哪位知道要告诉我哈,不胜感激!

别问我字典是什么,在下面!

常用的推送消息体实现:

public class PushNotificationPayload extends Payload

看好了,这个是类,是消息体的子类,该子类用于最普通的手机推送,手机上新闻推送,qq推送,基本上就是用的这个!

这个类重写了消息体长度,非空消息判断方法,构造方法就不说了。

下面是子类自己的方法

public static PushNotificationPayload alert(String message)
public static PushNotificationPayload badge(int badge) 
public static PushNotificationPayload sound(String sound)

分别创建有弹出提示语,未读消息数量,提示声音的消息体。

着这里解释下:alert就是手机收到推送的时候顶部显示的信息,比如说你有一条新消息,sound表示提示声音是什么,badge表示app图标上显示的未读消息数量,字典表示隐藏函数,比如新消息的id。

public static PushNotificationPayload combined(String message, int badge, String sound)该方法表示创建三个元素都有的消息体。

public static PushNotificationPayload test() 创建一个空的消息体,设置检测值为1,

public static PushNotificationPayload complex(),返回一个空消息体

public static PushNotificationPayload fromJSON(String rawJSON) throws JSONException 根据json字符串返回消息体
 public void addBadge(int badge) throws JSONException ;
 public void addSound(String sound) throws JSONException ;
 public void addAlert(String alertMessage) throws JSONException;

分别设置消息体的未读消息数量,提示声音,提示信息。

 private JSONObject getOrAddCustomAlert() throws JSONException;返回一个自定义消息体提示属性的对象
 private <T> T getCompatibleProperty(String propertyName, Class<T> expectedClass, String exceptionMessage) throws JSONException 
 private <T> T getCompatibleProperty(String propertyName, Class<T> expectedClass, String exceptionMessage, JSONObject dictionary) throws JSONException 
 public void addCustomAlertBody(String body) throws JSONException 将提示语放入消息体。
 public void addCustomAlertActionLocKey(String actionLocKey) throws JSONException ,添加action-loc-key参数指定动作。
 public void addCustomAlertLocKey(String locKey) throws JSONException 添加loc-key参数指定动作
 public void addCustomAlertLocArgs(List args) throws JSONException添加loc-args参数指定动作

有两个方法就是解析属性值,没心情写了,他们两个被getOrAddCustomAlert调用。

其他还有一些实现,不常用不说了回家吃饭!

原创粉丝点击