友盟的官方 sdk 中,ios 的推送是不支持你设定标题和内容的,有点奇怪这应该是基础功能,为什么会不支持。
官方给的 API 里显示,是可以设标题和内容的,如图
也就是说, alert 可以是文本,也可以是 jsonobj,但是官方给的 sdk 中,alert 只能设置为文本
public void setAlert(String token) throws Exception {
this.setPredefinedKeyValue("alert", token);
}
一开始我以为将标题内容序列化为 json string 即可,试了一下发现不行,所以只能看它的源代码。
它的推送内容最终会生成一个 big json,也都是调用 setPredefinedKeyValue 方法,我们会发现 setPredefinedKeyValue 是接收 Object value 的,阅读该方法,也能看出,其它的内容设入也有直接传递 json 的,此时如果我们将 alert 也以 json obj 设入,即可解决该问题。所以我重写了 ios 的传参对象
public class NewIOSUnicast extends IOSUnicast {
public NewIOSUnicast(String appkey, String appMasterSecret) throws Exception {
super(appkey, appMasterSecret);
}
public void setAlertObj(JSONObject token) throws Exception {
this.setPredefinedKeyValue("alert", token);
}
}