走了不少弯路,写出来分享给大家,希望更多的人不要像我这像浪费时间了
直接上代码吧,
//app.vue中执行该方法
export function bindMsgClick(){
//监听系统通知栏消息点击事件
plus.push.addEventListener('click', function(msg){
//处理点击消息的业务逻辑代码
//{title:"标题",content:"内容",payload:"自定义数据"}
console.log(msg)
uni.navigateTo({
url: '///?reflush=true'
})
}, false);
plus.push.addEventListener('receive', function(msg){
plus.push.createMessage(msg.content, message.payload, {
title: message.title
})
});
}
//登录成功之后绑定cid
export function updateCid(){
var pinf = plus.push.getClientInfo();
console.log('client',pinf.clientid)
request.post("/**/",{client:pinf.clientid}).then(res=>{
console.log(res.data)
});
}
后端代码如下,
//发消息给个人
public static String sendMsg(String clientId){
// 获取应用基本信息
IGtPush push = new IGtPush(host, appKey, masterSecret);
// 选择透传通知模板,个推离线只支持透传
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
JSONObject msg = new JSONObject();
msg.put("title","离线消息测试");
msg.put("content","---离线消息测试---");
String payload = msg.toString();
template.setTransmissionContent(payload); // 设置透传内容
template.setTransmissionType(2); // 2代表客户端收到消息后需要自行处理
// 配置第三方厂商推送
Notify notify = new Notify();
notify.setTitle(msg.getString("title"));
notify.setContent(msg.getString("content"));
notify.setType(GtReq.NotifyInfo.Type._intent);
// 设置intent 注意格式要正确(修改你的包名)
notify.setIntent("intent:#Intent;launchFlags=0x04000000;action=android.intent.action.oppopush;" +
"package=uni.包名.包名;" +
"component=uni.包名.包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;" +
"S.title=" + msg.getString("title") + ";" +
"S.content="+ msg.getString("content") + ";" +
"S.payload=" + payload + ";end");
notify.setPayload(payload);
template.set3rdNotifyInfo(notify);
List<String> appIds = new ArrayList<>();
appIds.add(appId);
SingleMessage message = new SingleMessage();
message.setData(template);
message.setOffline(true);
message.setOfflineExpireTime(8* 1000 * 3600); // 离线有效时间 单位毫秒
message.setStrategyJson("{\"default\":4}");
Target target = new Target();
target.setAppId(appId);
target.setClientId(clientId);
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());
return ret.getResponse().toString();
} else {
System.out.println("服务器响应异常");
return "服务器响应异常";
}
}
在后台配置好自己申请的第三方厂商的key,就可以杀掉app进程,向app推送消息了
目前只在小米手机上测试通过了
4 个评论
要回复文章请先登录或注册
chinahappybeer
chinahappybeer
刘先生啊
行者孙爷爷 (作者)