参考
UniPush 使用指南:
https://ask.dcloud.net.cn/article/35622
UniPush 开通指南:
https://ask.dcloud.net.cn/article/35716
在 uni-app 中使用 UniPush:
https://ask.dcloud.net.cn/article/35726
说明
5+ 接口 plus.push.getClientInfoAsync(successCB, errorCB)
,需要客户端在第一次运行时获取并提交到业务服务器绑定,不需要重复获取
不集成 rom 厂商的推送,就无法在 App 离线时发送 push
个推的付费提升送达率的产品就是 vip push,而 uniPush 就是个推的 vip Push
推送通道选择逻辑
在线走个推,离线走厂商
- Android平台
APP在线(个推推送通道可用)
推送通知和透传消息都使用个推的推送通道下发推送消息。
APP离线(个推推送通道不可用。推送通知-个推离线推送通道,透传消息-先厂商后个推离线推送通道)
推送通知,使用个推离线推送通道,离线消息会存储在消息离线库,离线时间内APP在线后下发推送消息。
透传消息,如果符合厂商推送的厂商手机(配置了手机厂商推送参数并且在对应厂商的手机上),则使用厂商推送通道下发推送消息;否则使用个推的离线推送通道,离线消息会存储在消息离线库,离线时间内APP在线后下发推送消息。
- iOS平台
推送通知,uniPush后台管理界面中不支持下发此类型,个推提供的服务端API支持下发推送通知(设置APN参数则通过苹果的APNS通道,否则使用个推通道)。
透传消息,设置APN参数则通过苹果的APNS通道下发推送消息,没有设置APN参数则使用个推的推送通道下发。
整体架构
推送消息类型
通知栏消息(推送通知)
UniPush推送服务定义好的推送样式、后续动作的推送方式,客户端接收到后显示在系统通知栏,用户点击通知栏消息启动APP(激活到前台)
透传消息
即自定义消息,UniPush推送服务只负责消息传递,不做任何处理,客户端在接收到透传消息后需要自己去处理消息的展示方式或后续动作「消息不会出现在通知栏」
UniPush推送服务对透传消息的数据符合以下格式时做了特殊处理,将透传消息显示到系统通知栏,前端 receive 中接收不到
1
| {"title": "xxx","content": "xxx","payload": "xxx"}
|
服务端
RestAPI V2「推荐」
开发前必读-个推文档中心 (getui.com)
公共参数:公共参数-个推文档中心 (getui.com)
GetuiLaboratory/getui-pushapi-java-client-v2: 个推官方提供的推送服务端SDK(Java语言),基于全新的RestAPI V2接口(https://docs.getui.com/getui/server/rest_v2/introduction/) (github.com)
环境要求
- 需要配合
JDK 1.6
或其以上版本
- 使用
个推PUSH SDK
前,您需要先前往个推开发者中心 完成开发者接入的一些准备工作,创建应用。详细见操作步骤
- 准备工作完成后,前往个推开发者中心获取应用配置,后续将作为使用SDK的输入。详细见操作步骤
安装依赖
推荐通过Maven来管理项目依赖,您只需在项目的pom.xml
文件中声明如下依赖
1 2 3 4 5
| <dependency> <groupId>com.getui.push</groupId> <artifactId>restful-sdk</artifactId> <version>1.0.0.3</version> </dependency>
|
快速开始
下列代码示例向您展示了使用个推Push SDK For Java
调用一个API的3个主要步骤:
- 设置参数,创建API
- 发起API调用
- 处理响应
创建API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class TestCreatApi { public void test() { System.setProperty("http.maxConnections", "200"); GtApiConfiguration apiConfiguration = new GtApiConfiguration(); apiConfiguration.setAppId("xxx"); apiConfiguration.setAppKey("xxx"); apiConfiguration.setMasterSecret("xxx"); apiConfiguration.setDomain("https://restapi.getui.com/v2/"); ApiHelper apiHelper = ApiHelper.build(apiConfiguration); PushApi pushApi = apiHelper.creatApi(PushApi.class); } }
|
根据cid进行单推
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| public class TestPushApi { public PushApi pushApi; public void test() { PushDTO<Audience> pushDTO = new PushDTO<Audience>(); pushDTO.setRequestId(System.currentTimeMillis() + ""); PushMessage pushMessage = new PushMessage(); pushDTO.setPushMessage(pushMessage); GTNotification notification = new GTNotification(); pushMessage.setNotification(notification); notification.setTitle("标题" + new Date()); notification.setBody("body"); notification.setClickType("url"); notification.setUrl("https://www.getui.com"); Audience audience = new Audience(); pushDTO.setAudience(audience); audience.addCid("xxx");
ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO); if (apiResult.isSuccess()) { System.out.println(apiResult.getData()); } else { System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg()); } } }
|
获取单日推送数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class TestStatisticApi { public StatisticApi statisticApi; public void test() { Set<String> taskIds = new HashSet<String>(); taskIds.add("xxx"); ApiResult<Map<String, Map<String, StatisticDTO>>> apiResult = statisticApi.queryPushResultByTaskIds(taskIds); if (apiResult.isSuccess()) { System.out.println(apiResult.getData()); } else { System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg()); } } }
|
查询用户状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class TestUserApi { public UserApi userApi; public void test() { Set<String> cids = new HashSet<String>(); cids.add("xxx"); ApiResult<Map<String, CidStatusDTO>> apiResult = userApi.queryUserStatus(cids); if (apiResult.isSuccess()) { System.out.println(apiResult.getData()); } else { System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg()); } } }
|
其余推送功能可参考该链接
设置代理
当需要使用代理进行http访问时,可以参考如下设置
1 2 3 4 5 6
| GtApiConfiguration apiConfiguration = new GtApiConfiguration(); //设置代理对象,参数依次为host、端口、鉴权账户、鉴权密码。其中鉴权账户密码可选 GtHttpProxyConfig proxyConfig = new GtHttpProxyConfig("xxx",xxx,"xxx","xxx"); apiConfiguration.setProxyConfig(proxyConfig); ApiHelper apiHelper = ApiHelper.build(apiConfiguration); PushApi pushApi = apiHelper.creatApi(PushApi.class);
|
已支持的API列表
以下是消息推送功能与推送API的对应关系
API类别 |
功能 |
调用的API名称 |
鉴权API |
鉴权 |
com.getui.push.v2.sdk.api.AuthApi.auth |
鉴权API |
删除鉴权 |
com.getui.push.v2.sdk.api.AuthApi.close |
推送API |
cid单推 |
com.getui.push.v2.sdk.api.PushApi.pushToSingleByCid |
推送API |
别名单推 |
com.getui.push.v2.sdk.api.PushApi.pushToSingleByAlias |
推送API |
cid批量单推 |
com.getui.push.v2.sdk.api.PushApi.pushBatchByCid |
推送API |
别名批量单推 |
com.getui.push.v2.sdk.api.PushApi.pushBatchByAlias |
推送API |
tolist创建消息 |
com.getui.push.v2.sdk.api.PushApi.createMsg |
推送API |
cid批量推 |
com.getui.push.v2.sdk.api.PushApi.pushListByCid |
推送API |
别名批量推 |
com.getui.push.v2.sdk.api.PushApi.pushListByAlias |
推送API |
群推 |
com.getui.push.v2.sdk.api.PushApi.pushAll |
推送API |
条件筛选用户推送 |
com.getui.push.v2.sdk.api.PushApi.pushByTag |
推送API |
标签快速推送 |
com.getui.push.v2.sdk.api.PushApi.pushByFastCustomTag |
推送API |
停止任务 |
com.getui.push.v2.sdk.api.PushApi.stopPush |
推送API |
查询定时任务 |
com.getui.push.v2.sdk.api.PushApi.queryScheduleTask |
推送API |
删除定时任务 |
com.getui.push.v2.sdk.api.PushApi.deleteScheduleTask |
统计API |
获取推送结果 |
com.getui.push.v2.sdk.api.StatisticApi.queryPushResultByTaskIds |
统计API |
任务组名查报表 |
com.getui.push.v2.sdk.api.StatisticApi.queryPushResultByGroupName |
统计API |
单日推送数据 |
com.getui.push.v2.sdk.api.StatisticApi.queryPushResultByDate |
统计API |
单日用户数据接口 |
com.getui.push.v2.sdk.api.StatisticApi.queryUserDataByDate |
统计API |
24小时在线用户数 |
com.getui.push.v2.sdk.api.StatisticApi.queryOnlineUserData |
用户API |
绑定别名 |
com.getui.push.v2.sdk.api.UserApi.bindAlias |
用户API |
根据cid查询别名 |
com.getui.push.v2.sdk.api.UserApi.queryAliasByCid |
用户API |
根据别名查询cid |
com.getui.push.v2.sdk.api.UserApi.queryCidByAlias |
用户API |
批量解绑别名 |
com.getui.push.v2.sdk.api.UserApi.batchUnbindAlias |
用户API |
解绑所有别名 |
com.getui.push.v2.sdk.api.UserApi.unbindAllAlias |
用户API |
一个用户绑定一批标签 |
com.getui.push.v2.sdk.api.UserApi.userBindTags |
用户API |
一批用户绑定一个标签 |
com.getui.push.v2.sdk.api.UserApi.usersBindTag |
用户API |
一批用户解绑一个标签 |
com.getui.push.v2.sdk.api.UserApi.deleteUsersTag |
用户API |
查询标签 |
com.getui.push.v2.sdk.api.UserApi.queryUserTags |
用户API |
添加黑名单用户 |
com.getui.push.v2.sdk.api.UserApi.addBlackUser |
用户API |
移除黑名单用户 |
com.getui.push.v2.sdk.api.UserApi.removeBlackUser |
用户API |
查询用户状态 |
com.getui.push.v2.sdk.api.UserApi.queryUserStatus |
用户API |
设置角标(仅支持IOS) |
com.getui.push.v2.sdk.api.UserApi.setBadge |
用户API |
查询用户总量 |
com.getui.push.v2.sdk.api.UserApi.queryUser |
老版本
https://docs.getui.com/getui/server/java/push/
简述
个推为开发者提供了如下3种消息推送方式
- toSingle :简称“单推”,指向单个用户推送消息
- toList:简称“批量推”,指向制定的一批用户推送消息
- toApp:简称“群推”,指向APP符合筛选条件的所有用户推送消息,
支持定速推送、定时推送,支持条件的交并补功能
maven 引入依赖
1 2 3 4 5
| <dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-http</artifactId> <version>4.1.2.0</version> </dependency>
|
再增加一个repository
1 2 3 4 5 6
| <repositories> <repository> <id>getui-nexus</id> <url>http://mvn.gt.getui.com/nexus/content/repositories/releases/</url> </repository> </repositories>
|
示例
toSingle
简称“单推”,指向单个用户推送消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| import com.gexin.rp.sdk.base.IPushResult; import com.gexin.rp.sdk.base.impl.SingleMessage; import com.gexin.rp.sdk.base.impl.Target; import com.gexin.rp.sdk.exceptions.RequestException; import com.gexin.rp.sdk.http.IGtPush; import com.gexin.rp.sdk.template.LinkTemplate; import com.gexin.rp.sdk.http.Constants;
public class UniPushUtil { private static String appId = ""; private static String appKey = ""; private static String masterSecret = "";
static String CID = ""; static String host = "http://api.getui.com/apiex.htm";
public static void main(String[] args) throws Exception { System.setProperty(Constants.GEXIN_PUSH_SINGLE_ALIAS_DETAIL, "true"); IGtPush push = new IGtPush(host, appKey, masterSecret); NotificationTemplate template = getNotificationTemplate(); SingleMessage message = new SingleMessage(); message.setOffline(true); message.setOfflineExpireTime(24 * 3600 * 1000); message.setData(template); message.setPushNetWorkType(0); message.setStrategyJson("{\"default\":4,\"ios\":4,\"st\":4}"); Target target = new Target(); target.setAppId(appId); target.setClientId(CID); IPushResult ret = null; try { ret = push.pushMessageToSingle(message, target); } catch (RequestException e) { e.printStackTrace(); ret = push.pushMessageToSingle(message, target, e.getRequestId()); } if (ret != null) { System.out.println(ret.getResponse().toString()); } else { System.out.println("服务器响应异常"); } } public static NotificationTemplate getNotificationTemplate() { NotificationTemplate template = new NotificationTemplate(); template.setAppId(appId); template.setAppkey(appKey);
Style0 style = new Style0(); style.setTitle("请输入通知栏标题"); style.setText("请输入通知栏内容"); style.setLogo("icon.png"); style.setLogoUrl(""); style.setRing(true); style.setVibrate(true); style.setClearable(true); style.setChannel("通知渠道id"); style.setChannelName("通知渠道名称"); style.setChannelLevel(3); template.setStyle(style);
template.setTransmissionType(1); template.setTransmissionContent("请输入您要透传的内容");
return template; } }
|
toList
简称“批量推”,指向制定的一批用户推送消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| import java.util.ArrayList; import java.util.List; import com.gexin.rp.sdk.base.IPushResult; import com.gexin.rp.sdk.base.impl.ListMessage; import com.gexin.rp.sdk.base.impl.Target; import com.gexin.rp.sdk.http.IGtPush; import com.gexin.rp.sdk.template.NotificationTemplate; public class PushList { private static String appId = ""; private static String appKey = ""; private static String masterSecret = "";
static String CID1 = ""; static String CID2 = ""; static String host = "http://api.getui.com/apiex.htm"; public static void main(String[] args) throws Exception { System.setProperty("gexin_pushList_needDetails", "true"); IGtPush push = new IGtPush(host, appKey, masterSecret); NotificationTemplate template = notificationTemplateDemo(); ListMessage message = new ListMessage(); message.setData(template); message.setOffline(true); message.setOfflineExpireTime(24 * 1000 * 3600); message.setStrategyJson("{\"default\":4,\"ios\":4,\"st\":4}");
List targets = new ArrayList(); Target target1 = new Target(); Target target2 = new Target(); target1.setAppId(appId); target1.setClientId(CID1); target2.setAppId(appId); target2.setClientId(CID2); targets.add(target1); targets.add(target2); String taskId = push.getContentId(message); IPushResult ret = push.pushMessageToList(taskId, targets); System.out.println(ret.getResponse().toString()); } public static NotificationTemplate notificationTemplateDemo() { NotificationTemplate template = new NotificationTemplate(); template.setAppId(appId); template.setAppkey(appKey);
Style0 style = new Style0(); style.setTitle("请输入通知栏标题"); style.setText("请输入通知栏内容"); style.setLogo("icon.png"); style.setLogoUrl(""); style.setRing(true); style.setVibrate(true); style.setClearable(true); style.setChannel("通知渠道id"); style.setChannelName("通知渠道名称"); style.setChannelLevel(3); template.setStyle(style);
template.setTransmissionType(2); template.setTransmissionContent("请输入您要透传的内容");
return template; } }
|
toApp
简称“群推”,指向APP符合筛选条件的所有用户推送消息,支持定速推送、定时推送,支持条件的交并补功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import com.gexin.rp.sdk.base.IPushResult; import com.gexin.rp.sdk.base.impl.AppMessage; import com.gexin.rp.sdk.http.IGtPush; import com.gexin.rp.sdk.template.LinkTemplate;
import java.io.IOException; import java.util.ArrayList; import java.util.List;
public class AppPush {
private static String appId = ""; private static String appKey = ""; private static String masterSecret = ""; private static String url = "http://api.getui.com/apiex.htm";
public static void main(String[] args) throws IOException {
IGtPush push = new IGtPush(url, appKey, masterSecret);
Style0 style = new Style0(); style.setTitle("请输入通知栏标题"); style.setText("请输入通知栏内容"); style.setLogo("push.png"); style.setRing(true); style.setVibrate(true);
NotificationTemplate template = new NotificationTemplate(); template.setAppId(appId); template.setAppkey(appKey); template.setStyle(style);
List<String> appIds = new ArrayList<String>(); appIds.add(appId); AppMessage message = new AppMessage(); message.setData(template); message.setAppIdList(appIds); message.setOffline(true); message.setOfflineExpireTime(1000 * 600);
IPushResult ret = push.pushMessageToApp(message); System.out.println(ret.getResponse().toString()); } }
|
web 自助发送界面
在DCloud的开发者后台:https://dev.dcloud.net.cn/
客户端调用的 js API
https://www.html5plus.org/doc/zh_cn/push.html
- 服务端发送通知消息,出现在通知栏,通过 click 监听点击事件,并解析通知内容
- 服务端发送透传消息时,不会出现在通知栏,通过 receive 接收。透传消息 json 结构自定义,。注意:如果透传消息结构为
{"title": "xxx","content": "xxx","payload": "xxx"}
,则会被解析为通知消息,在通知栏出现,receive 则接收不到
1 2 3 4 5 6 7 8 9 10 11 12 13
| plus.push.addEventListener('click', function(msg){ mui.toast('You clicked: ' + msg.content); console.log( 'You clicked: ' + msg.content ); }, false );
plus.push.addEventListener('receive', function(msg){ mui.toast('You received: ' + msg.content); console.log( 'You received: ' + msg.content ); }, false );
|