uni-app unipush + 个推 实现推送服务全过程(干货)

背景

说明文档这个事情官方应该提供出来,可惜官方觉得是多余的,免费的东西凭啥给你做好。于是我在这里叙述一下实现消息通知推送的步骤。

uni-app官方文档入口
https://uniapp.dcloud.io/api/plugins/push

uniPush官方使用指南
https://ask.dcloud.net.cn/article/35622

个推官方文档入口
https://docs.getui.com/

推送H5+API接口:
https://www.html5plus.org/doc/zh_cn/push.html

一、流程

1.第三方应用集成个推SDK,个推SDK运行后获取CID返回给第三方应用,由第三方应用保存至其应用服务器;
2.第三方应用服务器调用推送API进行消息推送,个推SDK将接收到的推送消息回调给App进行处理。

二、推送方式

个推提供JAVA、C#、PHP、Python等多种语言版本的服务端API SDK,可以和各种第三方应用服务器技术架构进行对接。为了最大程度提高消息推送性能,第三方开发者需要根据业务需求合理选择消息推送形式。如果是针对每 个用户进行定制化的消息推送、或是实现类似IM的点对点消息,请采用单推消息形式(SingleMessage);如果需要根据特定条件筛选出一批CID 后推送相同的内容,请选择批量推送形式(ListMessage);如果希望针对省市或全量用户进行推送,请选择群推形式(AppMessage)。

通过上面描述已经大概了解个推了,可以单推、批量推、按照筛选条件批量推送,详细更多介绍请参考官方文档:http://docs.getui.com/

三、基础环境准备

2.1 DCLOUD平台配置
参考官方教程在dcloud中配置各个厂商的api和secret,这部分不是难点,有问题留言。
https://ask.dcloud.net.cn/article/35716
在这里插入图片描述

为啥要配置厂商呢:
为了在app关闭的时候,可以通过厂商的服务器推送消息给手机通知系统。第三方的消息推送会被阻挡,最好的方式就是按照厂商的要求接入各个推送服务。庆幸的事情,这部分DCLOUD已经帮你做好了。你只需要注册下各个厂商的开发平台,按到appid和secret登录dcloud后台即可。
在这里插入图片描述
2.1 uni-app权限配置
在这里插入图片描述

2.3 准备自定制测试基座或者云打包
Hbuilder的基座配置的信息都是dcloud的,所以用被人的app是没办法测试推送服务的。解决这个问题有两种
1,云打包时打包成为自己的app后测试
2,制作自定义的基座(很简单),这个基座的信息都是自己的,所以可以测试
运行的时候选择自定制基座,或者直接云打包后的的apk安装到手机测试。
在这里插入图片描述
温馨提示: 可以先体验 uni push 后台界面推送(帮助理解推送的过程)

四、APP端代码

代码位置只能在app.vue的onLaunch中,其他地方可能会有问题。

 //#ifdef APP-PLUS  
			 var info = plus.push.getClientInfo();
			console.log( JSON.stringify( info ) );
			 /* 5+  push 消息推送 ps:使用:H5+的方式监听,实现推送*/  
			plus.push.addEventListener("click", function(msg) {  
						console.log("click:"+JSON.stringify(msg));  
						console.log(msg.payload);  
						console.log(JSON.stringify(msg));  
						//这里可以写跳转业务代码
					}, false);  
						// 监听在线消息事件    
					plus.push.addEventListener("receive", function(msg) {  
						// plus.ui.alert(2);  
							//这里可以写跳转业务代码
						console.log("recevice:"+JSON.stringify(msg))  
			   }, false);  

		   //#endif  

这里就是官方文档比较恶心的地方,自己生命onpush之类的api废弃了,但是给的demo还是用的废弃的方式。推荐的H5+api却不给demo。要知道demo对我们就是api文档啊。

这里listener监听的两种事件

  1. “click”-从系统消息中心点击消息启动应用事件;
  2. “receive”-应用从推送服务器接收到推送消息事件。

五、后端代码(java)

5.1个推sdk导入

<dependency>
    <groupId>com.gexin.platform</groupId>
    <artifactId>gexin-rp-sdk-http</artifactId>
    <version>4.1.0.5</version>
</dependency>

<repositories>
    <repository>
        <id>getui-nexus</id>
        <url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url>
    </repository>
 </repositories>

5.2 加入 uin push 应用配置参数,实际中替换为自己的

	public static final String AppID = "";
	public static final String AppSecret = "";
	public static final String AppKey = "";
	public static final String MasterSecret = "";
	public static final String HOST = "http://sdk.open.api.igexin.com/apiex.htm";

第一个推送方式,也是最常用性能最高的推送,单推:

	/**
	 * 
	 * @param cidOrAlias
	 *            别名或者cid
	 * @param msg
	 *            透传消息内容
	 * @param type
	 *            1-cid推,2-别名推
	 */
	public static void pushToSingle(String cidOrAlias, String msg, int type) {
		IGtPush push = new IGtPush(HOST, AppKey, MasterSecret);
		ITemplate template = buildTransmissionTemplate(msg);
		SingleMessage message = new SingleMessage();
		// 是否离线推送
		message.setOffline(true);
		// 离线有效时间,单位为毫秒,可选
		message.setOfflineExpireTime(24 * 3600 * 1000);
		// 消息内容
		message.setData(template);
		// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
		message.setPushNetWorkType(0);
 
		Target target = new Target();
		target.setAppId(AppID);
		if (type == 1) {
			target.setClientId(cidOrAlias);
		} else if (type == 2) {
			// 按别名推送
			target.setAlias(cidOrAlias);
		}
		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("服务器响应异常");
		}
	}

上面有详细的注释,有几个需要注意几个地方。

推送的消息类型,支持各种模板,有通知栏透传模板,通知栏点击跳转网页模板,透传模板等,下面列举示例

1、通知栏点击跳转网页模板:

public static LinkTemplate buildLinkTemplate() {
		LinkTemplate template = new LinkTemplate();
		// 设置APPID与APPKEY
		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);
		template.setStyle(style);
 
		// 设置打开的网址地址
		template.setUrl("http://www.baidu.com");
		return template;
	}

通过这种方式推送,手机上会收到一条通知栏消息,点击后会打开指定网页。

2、通知栏透传模板

	public static NotificationTemplate buildNotificationTemplate() {
		NotificationTemplate template = new NotificationTemplate();
		// 设置APPID与APPKEY
		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);
		template.setStyle(style);
 
		// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
		template.setTransmissionType(2);
		template.setTransmissionContent("请输入您要透传的内容");
		return template;
	}

通过这种方式,手机上会收到一条通知栏消息,并且带有透传消息。

3、纯透传模板:

	public static NotificationTemplate buildNotificationTemplate() {
		NotificationTemplate template = new NotificationTemplate();
		// 设置APPID与APPKEY
		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);
		template.setStyle(style);
 
		// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
		template.setTransmissionType(2);
		template.setTransmissionContent("请输入您要透传的内容");
		return template;
	}

客户端集成SDK设置监听后,会收到透传消息,客户端可以自己灵活的选择处理方式。

上面还提到按别名推送,那么别名是怎么来的呢

	public static void bindAlias(String cid, String alias) {
		IGtPush push = new IGtPush(HOST, AppKey, MasterSecret);
		IAliasResult bindSCid = push.bindAlias(AppID, alias, cid);
		System.out.println("绑定结果:" + bindSCid.getResult() + "错误码:" + bindSCid.getErrorMsg());
	}

这是绑定别名的方法,这样通过绑定别名可以和自己的业务数据绑定进行推送。单推就介绍到这里。

第二种推送方式:批量推送:

	public static void pushToList(List<String> cids, String msg) {
		// 配置返回每个用户返回用户状态,可选
		System.setProperty("gexin_pushList_needDetails", "true");
		// 配置返回每个别名及其对应cid的用户状态,可选
		// System.setProperty("gexin_pushList_needAliasDetails", "true");
		IGtPush push = new IGtPush(HOST, AppKey, MasterSecret);
		// 透传模板
		ITemplate template = buildTransmissionTemplate(msg);
		ListMessage message = new ListMessage();
		message.setData(template);
		// 设置消息离线,并设置离线时间
		message.setOffline(true);
		// 离线有效时间,单位为毫秒,可选
		message.setOfflineExpireTime(24 * 1000 * 3600);
		// 配置推送目标
		List<Target> targets = new ArrayList<Target>();
		Target target = null;
		for (String cid : cids) {
			target = new Target();
			target.setAppId(AppID);
			target.setClientId(cid);
			targets.add(target);
			// target.setAlias(Alias1);
		}
		// taskId用于在推送时去查找对应的message
		String taskId = push.getContentId(message, "任务别名_toApp");
		// String taskId = push.getContentId(message);
		IPushResult ret = push.pushMessageToList(taskId, targets);
		System.out.println(ret.getResponse().toString());
	}

这里实际上就是,对一批指定cliendid的用户进行推送,也支持上面的几种模板,参数也可以传别名集合进来。

第三种推送方式,按各种筛选条件进行群推:

public static void pushToApp(String msg, List<String> tagList) throws Exception {
		IGtPush push = new IGtPush(HOST, AppKey, AppSecret);
 
		// 使用通知栏链接模板
		ITemplate template = buildLinkTemplate();
		AppMessage message = new AppMessage();
		message.setData(template);
 
		message.setOffline(true);
		// 离线有效时间,单位为毫秒,可选
		message.setOfflineExpireTime(24 * 1000 * 3600);
		// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
		message.setPushNetWorkType(0);
		// 全量推送个推控制下发速度在100条/秒,只有toApp支持定速推送。
		// message.setSpeed(100);
		// 设置指定时间推送
		// message.setPushTime("201903271756");
 
		List<String> appIdList = new ArrayList<String>();
		appIdList.add(AppID);
		message.setAppIdList(appIdList);
 
		// 推送给App的目标用户需要满足的条件
		AppConditions cdt = new AppConditions();
 
		// 手机类型
		List<String> phoneTypeList = new ArrayList<String>();
		phoneTypeList.add("ANDROID");
		phoneTypeList.add("IOS");
 
		// 省份
		List<String> provinceList = new ArrayList<String>();
		// 50000000代表重庆市
		provinceList.add("50000000");
 
		// 设置手机类型筛选
		cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
		// 设置省份筛选
		cdt.addCondition(AppConditions.REGION, provinceList);
		// 设置tag筛选
		cdt.addCondition(AppConditions.TAG, tagList);
 
		// 交并补
		// cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList,
		// OptType.or);
		// cdt.addCondition(AppConditions.REGION, provinceList, OptType.or);
		// cdt.addCondition(AppConditions.TAG, tagList, OptType.or);
		message.setConditions(cdt);
 
		IPushResult ret = push.pushMessageToApp(message, "任务别名_toApp");
		System.out.println(ret.getResponse().toString());
	}

几个注意事项:

省份和城市编码,请参考官方文档

定速推送:旨在解决个推群推系统在全量推送时速度过快,导致部分客户服务器连接压力过大的问题。提供接口设置让用户按自身情况控制推送速度,如果未设置则按默认推送速度发送。

定时推送:对单个指定应用的所有用户群发推送消息。该消息可以在用户设定的时间点进行推送。此接口需要开通。

交并补设置:应用群推对于复杂的查询条件新增加的交并补功能,以对应查询语义中的与或非的关系

  • 场景:需要发送给城市在A,B,C里面,没有设置tagtest标签,手机型号为android的用户,用条件交并补功能可以实现,city(A|B|C)
    && !tag(tagtest) && phonetype(andriod)
  • 条件类型(OptType.or 或, OptType.and 与, OptType.not 非)

tag列表:上面有提到tag,tag就是给用户打的标签,可以实现按标签推送,tag的设置方式如下:

public static void setTag(String cid, List<String> tagList) {
		IGtPush push = new IGtPush(HOST, AppKey, MasterSecret);
		IQueryResult ret = push.setClientTag(AppID, cid, tagList);
		System.out.println(ret.getResponse().toString());
	}

通过设置标签,所有终端都可以设置一个或者多个标签,进行标签推送时就会实现筛选作用。

群推就介绍到这里。

单推还有一种扩展形式,批量单推,用于一次创建提交多个单推任务。当单推任务较多时,推荐使用该接口,可以减少与服务端的交互次数。

public static void pushBatch(String cid, String content) throws Exception {
		IIGtPush push = new IGtPush(HOST, AppKey, MasterSecret);
		IBatch batch = push.getBatch();
		try {
			// 构建透传消息
			constructClientTransMsg(cid, content, batch);
			// 构建点击通知打开网页消息
			constructClientLinkMsg(cid, content, batch);
		} catch (Exception e) {
			e.printStackTrace();
		}
		batch.submit();
	}
private static void constructClientTransMsg(String cid, String msg, IBatch batch) throws Exception {
		SingleMessage message = new SingleMessage();
		TransmissionTemplate template = new TransmissionTemplate();
		template.setAppId(AppID);
		template.setAppkey(AppKey);
		template.setTransmissionContent(msg);
		// 这个Type为int型,填写1则自动启动app
		template.setTransmissionType(1);
 
		message.setData(template);
		message.setOffline(true);
		message.setOfflineExpireTime(1 * 1000);
 
		// 设置推送目标,填入appid和clientId
		Target target = new Target();
		target.setAppId(AppID);
		target.setClientId(cid);
		batch.add(message, target);
	}
private static void constructClientLinkMsg(String cid, String msg, IBatch batch) throws Exception {
		SingleMessage message = new SingleMessage();
		LinkTemplate template = new LinkTemplate();
		template.setAppId(AppID);
		template.setAppkey(AppKey);
		template.setTitle("title");
		template.setText("msg");
		template.setLogo("push.png");
		template.setLogoUrl("logoUrl");
		template.setUrl("url");
 
		message.setData(template);
		message.setOffline(true);
		message.setOfflineExpireTime(1 * 1000);
 
		// 设置推送目标,填入appid和clientId
		Target target = new Target();
		target.setAppId(AppID);
		target.setClientId(cid);
		batch.add(message, target);
	}

这样就可以实现一次服务端交互,完成多个单推任务,cid也可以是不同的,给不同的终端进行推送,推送内容也可以不同,这里为了简便就取的一样的。

个推java服务端核心API就介绍到这里,官网还有很多辅助API,查询统计,用户,推送结果等等,这里就不一一列举了,有这个需求的请移至官网文档详细阅读。

  • 23
    点赞
  • 108
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是胡小结

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值