赞
踩
“ 你现在的气质里,藏着你走过的路,读过的书,和爱过的人。 ”
整体还是比较简单地,就是有一些需要注意的地方,很多问题官方文档里面也写了,这里总结一下
对于安卓,谷歌本来有专门的推送通道,但是无奈被墙,所以各个大厂就自己各搞一套,那我们开发者如果每个都去开发一套成本就太高了,所以就需要借助第三方通道,让他们帮我们处理,目前比较火的有个推,极光等等,由于前端使用unipush,它使用的是个推,所以后台就使用的个推。
unipush使用指南:https://ask.dcloud.net.cn/article/35622
首先需要开通unipush,
然后需要获取cid,cid的作用就是标识是哪一个终端,也就是标识app在那个手机上面,后面推送消息就是根据这个cid来推送。
需要将项目打包成app,使用Hbuilder调式是不行的,需要真机安装app才可以获取到cid
获取cid的方法:https://ask.dcloud.net.cn/article/34
document.addEventListener('plusready', function(){
// 页面加载时触发
var pinf = plus.push.getClientInfo();
var cid = pinf.clientid;//客户端标识
}, false );
然后已经获取到cid了,了解一下整体架构,看官网的这张图:
整体流程大概就是:
后台使用个推的开发文档:http://docs.getui.com/getui/server/java/guide/
不难,看看就懂了。
实例代码:仅供参考
/**
* 推送消息
*/
public Map<String, Object> pushMessage(Integer userId, String remark) {
IGtPush push = new IGtPush(URL, config.getPushAppKey(), config.getPushMasterSecret());
// 使用透传模板
TransmissionTemplate template = new TransmissionTemplate();
// 1:收到通知直接激活app,2:客服端自行处理
template.setTransmissionType(2);
// 透传内容
template.setTransmissionContent(remark);
template.setAppId(config.getPushAppId());
template.setAppkey(config.getPushAppKey());
String intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;package=xxx;component=xxx;end";
Notify notify = new Notify();
// 通知栏显示标题
notify.setTitle("您有新的任务下达");
// 通知栏内容
notify.setContent(remark);
notify.setIntent(intent);
// 设置第三方通知
template.set3rdNotifyInfo(notify);
SingleMessage message = new SingleMessage();
message.setData(template);
// 设置消息离线
message.setOffline(true);
// 离线消息有效时间为7天
message.setOfflineExpireTime(1000 * 3600 * 24 * 7);
//添加要推送的终端
Target target = new Target();
MstUserPo userPo = userDao.selectByPrimaryKey(userId);
if (userPo.getPushId() != null) {
target.setAppId(config.getPushAppId());
target.setClientId(userPo.getPushId());
}else {
return null;
}
IPushResult result;
Map<String, Object> response = null;
// 执行推送
try {
result = push.pushMessageToSingle(message, target);
} catch (RequestException e) {
e.printStackTrace();
result = push.pushMessageToSingle(message, target, e.getRequestId());
}
if (result != null) {
response = result.getResponse();
}
return response;
}
intent 里面的包名换成你自己的
需要注意的是:如果使用NotificationTemplate 也就是普通模板,如果推送的app在线,用户点击推送消息是不会跳转到app的,他只对沉默用户有用,所以建议使用透传摸版。
其实我个人理解个推文档里面除了NotificationTemplate 其他的应该都算是透传,所以模板应该都是可以用的,我只用了TransmissionTemplate 。
前端监听到发过来的透传消息后再创建一个本地消息就可以控制点击消息跳转到哪里了。官方文档:https://ask.dcloud.net.cn/article/34
// 监听接收透传消息事件
plus.push.addEventListener('receive', function(msg){
// 有消息推送,创建本地通知栏消息
void plus.push.createMessage( msg.content, msg.payload, {});
}, false);
// 监听系统通知栏消息点击事件
plus.push.addEventListener('click', function(msg){
// 跳转到指定也页面
}, false);
createMessage创建本地可以传入参数,默认是响铃,声音使用系统提示音,需要的话去官方文档里面查看。
整体过程大概就这样,如果有问题可以留言over
**普通推送和透传只在app在线时生效,离线推送需要借助厂商推送!!!
附上个推客服小姐姐的qq:1302641036,除了技术有点菜之外,其他还行 ~~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。