1、能够完成推送的条件
①、ios应用必须有开发证书,或者发布证书,能够打包成ipa包(ios打包及证书申请查看:https://blog.csdn.net/u011118071/article/details/104937328),运行在真实苹果手机上,模拟器不行。
②、需要在苹果开发者网址,注册应用APPID时勾选推送服务,并创建推送证书(非常重要),需要上传到个推应用配置中心
③、ios 端需要使用H5+,监听推送,并做后续处理。
④、ios只能推送透传消息(重要)
2、操作配置步骤
①、在个推平台注册账户,并创建应用
个推官网网址:https://www.getui.com/?f=3&p=1
进入后,点击应用管理

点击创建应用


如上图,①、自己根据实际情况填写即可,②部分,android包名,对应安卓应用打包时包名。android签名,使用Hbuilder提供通用签名59:20:1C:F6:58:92:02:CB:2C:DA:B2:67:52:47:21:12即可。③、IOS bundleID即在苹果开发者中心创建AppID时创建的bundleID。
在ios推送时,还需要在苹果开发者中心创建推送证书,步骤如下

②、选择之前创建的App ID

③、选择Push Notificatios ,点击Configure

④、选择创建证书,Create Certificate


选择证书申请文件。
证书申请文件步骤如下:
生成证书请求文件
不管是申请开发 (Development) 证书还是发布 (Distribution) 证书,都需要使用证书请求 (.certSigningRequest) 文件,证书请求文件需在Mac OS上使用 “钥匙串访问” 工具生成。
在“Spltlight Search”中搜索“钥匙串”并打开 “钥匙串访问” 工具:
打开菜单 “钥匙串访问”->“证书助理”,选择“从证书颁发机构请求证书...”:
打开创建请求证书页面,在页面中输入用户邮件地址、常用名称,选择存储到磁盘,点击 “继续” :
文件名称为“CertificateSigningRequest.certSigningRequest”,选择保存位置,点击 “存储” 将证书请求文件保存到指定路径下,后面申请开发(Development)证书和发布(Production)证书时需要用到
到此,推送证书已配置完成。点击下载即可。

我申请的证书文件如下

进入个推平台,选择个推-消息推送>选择应用【ios供热助手】>创建推送

选择应用配置,在红框位置上传刚创建的证书,下图为我上传成功后的效果,可以修改证书。也可以测试。
这儿要说一下测试一下的作用。将打包好的ios开发包安装到苹果手机号,启动应用,
可以通过plus.push.getClientInfo()获取clientId及token,点击测试,将获取的token输入到文本框,点击测试。若设备能收到推送消息,说明设备与个推平台已建立联系。【应用打包时需要在manifest.json配置个推sdk,设置个推appid,appkey,appsecret】

测试成功后,就可以在个推平台推送消息中发消息测试了。注意IOS只能接收透传消息。
下面贴一下我应用的前端js代码
//监听推送点击事件
warmApp.push.addEventListenerClick=function(handle)
{
plus.push.addEventListener("click", function (msg) {
handle(msg);
//消息类型
// msg.payload LocalMSG:本地创建消息 否则为离线消息
}, false);
}
// 监听在线消息事件
warmApp.push.addEventListenerReceive=function(handle)
{
plus.push.addEventListener("receive", function (msg) {
handle(msg);
//msg.aps Apple APNS message:在线APNS消息 否则 在线透传消息
});
}
为了使用方便我做了简单封装,其中handle为回调函数。
java端集成推送代码
PushMessageUtil类
package com.sheshu.common.pushmessage;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.ITemplate;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.NotyPopLoadTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2017/6/22.
*/
@Component
public class PushMessageUtil {
@Autowired
public PushMessagePojo pushMessagePojo;
public PushMessageUtil(PushMessagePojo pushMessagePojo)
{
this.pushMessagePojo=pushMessagePojo;
}
/**
* 指定单个用户推送消息
* @return
*/
public String pushToSingle(TempData tempData)
{
IGtPush push = new IGtPush(pushMessagePojo.getUrl(), pushMessagePojo.getAppKey(), pushMessagePojo.getMasterSecret());
SingleMessage message = new SingleMessage();
message.setData(getTemplate(tempData));
message.setOffline(true);
message.setOfflineExpireTime(1800 * 1000);
Target target = new Target();
target.setAppId(pushMessagePojo.getAppId());
target.setClientId(tempData.getCid());
IPushResult result = push.pushMessageToSingle(message, target);
return result.getResponse().get("result").toString();
}
/**
* 群推消息
* @return
*/
public String pushGroup(TempData tempData)
{
IGtPush push = new IGtPush(pushMessagePojo.getUrl(), pushMessagePojo.getAppKey(), pushMessagePojo.getMasterSecret());
List<String> appIds = new ArrayList<String>();
appIds.add(pushMessagePojo.getAppId());
AppMessage message = new AppMessage();
message.setData(getTemplate(tempData));
message.setOffline(true);
message.setOfflineExpireTime(1800 * 1000);
message.setAppIdList(appIds);
IPushResult result = push.pushMessageToApp(message);
return result.getResponse().get("result").toString();
}
/**
* 指定用户列表cid推送消息
* @return
*/
public String pushToSingleList(TempData tempData)
{
IGtPush push = new IGtPush(pushMessagePojo.getUrl(), pushMessagePojo.getAppKey(), pushMessagePojo.getMasterSecret());
ListMessage message = new ListMessage();
message.setData(getTemplate(tempData));
message.setOffline(true);
message.setOfflineExpireTime(1800 * 1000);
List<Target> targetList=new ArrayList<Target>();
for(String cid:tempData.getCidList())
{
Target target = new Target();
target.setAppId(pushMessagePojo.getAppId());
target.setClientId(cid);
targetList.add(target);
}
String taskId = push.getContentId(message);
IPushResult result = push.pushMessageToList(taskId, targetList);
return result.getResponse().get("result").toString();
}
//消息模板设置函数
public ITemplate getTemplate(TempData tempData)
{
ITemplate iTemplate=null;
switch (tempData.getTemplateType())
{
case NotificationTemplate:
NotificationTemplate notificationTemplate=new NotificationTemplate();
notificationTemplate.setAppId(pushMessagePojo.getAppId());
notificationTemplate.setAppkey(pushMessagePojo.getAppKey());
notificationTemplate.setTitle(tempData.getTempTitle());
notificationTemplate.setText(tempData.getTempText());
iTemplate=notificationTemplate;
break;
case LinkTemplate:
LinkTemplate linkTemplate = new LinkTemplate();
linkTemplate.setAppId(pushMessagePojo.getAppId());
linkTemplate.setAppkey(pushMessagePojo.getAppKey());
linkTemplate.setTitle(tempData.getTempTitle());
linkTemplate.setText(tempData.getTempText());
linkTemplate.setUrl(tempData.getTempUrl());
iTemplate=linkTemplate;
break;
case NotyPopLoadTemplate:
NotyPopLoadTemplate notyPopLoadTemplate=new NotyPopLoadTemplate();
//获取appId
notyPopLoadTemplate.setAppId(pushMessagePojo.getAppId());
notyPopLoadTemplate.setAppkey(pushMessagePojo.getAppKey());
//设置通知栏信息
notyPopLoadTemplate.setNotyTitle(tempData.getTempTitle());
notyPopLoadTemplate.setNotyContent(tempData.getTempText());
//配置通知栏图标
notyPopLoadTemplate.setNotyIcon("");
//设置是否响铃,震动,或者清除
notyPopLoadTemplate.setBelled(true);
notyPopLoadTemplate.setVibrationed(true);
notyPopLoadTemplate.setCleared(true);
//设置弹出标题与内容
notyPopLoadTemplate.setPopTitle(tempData.getPopTitle());
notyPopLoadTemplate.setPopContent(tempData.getPopContent());
//设置弹出框显示图片
notyPopLoadTemplate.setPopImage("");
notyPopLoadTemplate.setPopButton1(tempData.getPopButton1());
notyPopLoadTemplate.setPopButton2(tempData.getPopButton2());
//设置下载标题
notyPopLoadTemplate.setLoadTitle(tempData.getLoadTitle());
notyPopLoadTemplate.setLoadIcon("");
//设置下载地址
notyPopLoadTemplate.setLoadUrl(tempData.getLoadUrl());
notyPopLoadTemplate.setActived(true);
iTemplate =notyPopLoadTemplate;
break;
case TransmissionTemplate:
TransmissionTemplate transmissionTemplate=new TransmissionTemplate();
transmissionTemplate.setAppId(pushMessagePojo.getAppId());
transmissionTemplate.setAppkey(pushMessagePojo.getAppKey());
//1强制启动应用程序,2等待程序启动
transmissionTemplate.setTransmissionType(1);
transmissionTemplate.setTransmissionContent(tempData.getTransmissionContent());
// 设置定时展示时间
// template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
// iOS推送使用该字段
APNPayload payload = new APNPayload();
payload.setAutoBadge("0");// 设置角标,还可以实现显示数字的自动增减,如"+1"、"-1"、"1"等
// 推送直接带有透传数据
// 当content-available为1时,App存活(前台或后台时),消息内容会直接透传给App。在未设置其他提醒(Alert、Sound)时,可以无提示和无声音的刷新数据。
// (支持 iOS 7.0 及以上版本。)
payload.setContentAvailable(0);
payload.setSound("default"); // 通知铃声文件名,无声设置为"com.gexin.ios.silence"
payload.setAlertMsg(getDictionaryAlertMsg(tempData.getTempTitle(), tempData.getTempText())); // 字典模式使用APNPayload.DictionaryAlertMsg
payload.setCategory(tempData.getTransmissionContent());
transmissionTemplate.setAPNInfo(payload);
iTemplate=transmissionTemplate;
break;
}
return iTemplate;
}
/**
* ios数据封装 APNPayload节点
*
* @param title 通知标题
* @param text 通知内容 :不能有转义字符,大小限制2kb
* @return
*/
private APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String title, String text ) {
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setBody(text);// 通知文本消息字符串
alertMsg.setActionLocKey("ActionLockey");// (用于多语言支持)指定执行按钮所使用的Localizable.strings
alertMsg.setLocKey("loc-key");// (用于多语言支持)指定Localizable.strings文件中相应的key
alertMsg.addLocArg("loc-args");// 如果loc-key中使用的占位符,则在loc-args中指定各参数
alertMsg.setLaunchImage("launch-image");// 指定启动界面图片名
// iOS8.2以上版本支持
alertMsg.setTitle(title);// 通知标题
alertMsg.setTitleLocKey("TitleLocKey");// (用于多语言支持)对于标题指定执行按钮所使用的Localizable.strings,仅支持iOS8.2以上版本
alertMsg.addTitleLocArg("TitleLocArg");// 标题,如果loc-key中使用的占位符,则在loc-args中指定各参数,仅支持iOS8.2以上版本
return alertMsg;
}
}
PushMessagePojo类
package com.sheshu.common.pushmessage;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Created by Administrator on 2017/6/22.
*/
@Data
@Component
@ConfigurationProperties(prefix = "push")
public class PushMessagePojo {
private String appId; // = "3DFlQTretF73wQC1hviQT3";
private String appKey; // = "Gob1gQ1woN8XJo6LRNbgC5";
private String masterSecret; // = "j6BGytOgWtAMjC6SBzcKp7";
private String url; // = "http://sdk.open.api.igexin.com/apiex.htm";
}
TempData类
package com.sheshu.common.pushmessage;
import lombok.Data;
import java.util.List;
/**
* @Auther: chenxiaodong
* @Date: 2019-05-27 8:58
* @Description:
*/
@Data
public class TempData {
//模板抬头
private String tempTitle;
//模板内容
private String tempText;
//模板url
private String tempUrl;
//模板类型
private TemplateType templateType;
//客户端appId
private String cid;
//客户端appId列表
private List<String> cidList;
//是否响铃
private Boolean isBelled;
//是否震动
private Boolean isVibrationed;
//是否清除
private Boolean isCleared;
//通知栏图标
private String notyIcon;
/*********推送下载部分***********/
//弹出层标题
private String popTitle;
//弹出层内容
private String popContent;
//弹出层图片
private String popImage;
//弹出层按钮1
private String popButton1;
//弹出层按钮2
private String popButton2;
//下载抬头
private String loadTitle;
//下载图标
private String loadIcon;
//下载地址
private String loadUrl;
/*********透传消息部分***********/
//透传消息内容
private String transmissionContent;
}
TemplateType 类
package com.sheshu.common.pushmessage;
/**
* Created by Administrator on 2017/6/22.
*/
public enum TemplateType {
NotificationTemplate, LinkTemplate,TransmissionTemplate,NotyPopLoadTemplate;
}
TransmissionContent 类
package com.sheshu.common.pushmessage;
import lombok.Data;
/**
* @Auther: chenxiaodong
* @Date: 2019-05-27 10:57
* @Description:
*/
@Data
public class TransmissionContent {
/**
* 通知栏标题
*/
private String title;
/**
* 通知栏内容
*/
private String content;
/**
*其他数据
*/
private PayloadData payloadData;
public TransmissionContent()
{
payloadData=new PayloadData();
}
@Data
public class PayloadData
{
//审批,工单,消息id,可根据id直接打开详情页面
private String id;
//消息类型(审批,工单,消息,下载),可根据type直接打开列表
private String type;
//url地址
private String url;
//是否语音播报
private String isSpeech;
}
}
单元测试示例
package com.sheshu.common;
import com.google.gson.Gson;
import com.sheshu.common.pushmessage.*;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: chenxiaodong
* @Date: 2020-03-18 17:49
* @Description:
*/
public class PushTest {
@Test
public void pushTest()
{
PushMessagePojo pushMessagePojo=new PushMessagePojo();
pushMessagePojo.setAppId("G6OneF4tIZ9zb5A9MdGJ99");
pushMessagePojo.setAppKey("JZhbDdK6Sh9B9WrmyLbcA8");
pushMessagePojo.setMasterSecret("y1mfBXfo7U9BPSgyRXjDJ");
pushMessagePojo.setUrl("http://sdk.open.api.igexin.com/apiex.htm");
PushMessageUtil pushMessageUtil=new PushMessageUtil(pushMessagePojo);
List<String> cidList =new ArrayList<>();
cidList.add("fb08f5d94c9c938f02452cbcc61c099f");
cidList.add("0048224eac1bcf5697a8a8ca54f0df8d");
//手机端推送
TempData tempData = new TempData();
//设置模板为透传模板
tempData.setTemplateType(TemplateType.TransmissionTemplate);
//设置cid集合
tempData.setCidList(cidList);
tempData.setTempTitle("您有一条审批记录,请注意查收!");
tempData.setTempText("审批");
TransmissionContent transmissionContent = new TransmissionContent();
//设置推送标题
transmissionContent.setTitle("审批");
//设置推送内容
transmissionContent.setContent("您有一条审批记录,请注意查收!");
//设置语音播报
transmissionContent.getPayloadData().setIsSpeech("true");
//设置推送类型
transmissionContent.getPayloadData().setType("审批");
//设置id为空
transmissionContent.getPayloadData().setId("1");
Gson gson = new Gson();
//将对象转换为json
String json = gson.toJson(transmissionContent);
//设置透传消息内容
tempData.setTransmissionContent(json);
//群发
String result = pushMessageUtil.pushToSingleList(tempData);
System.out.println(result);
}
}
我代码全部贴出。以便于测试。
目前我ios及android均使用透传推送,所以主要关注如下代码


完成





539

被折叠的 条评论
为什么被折叠?



