后台app个推
来源:互联网 发布:网络通信加密软件 编辑:程序博客网 时间:2024/04/29 16:45
做项目会用到把消息推送给app
public class AppMessagePush { private static String appId = MyProperties.getByKey("appId");; private static String appKey = MyProperties.getByKey("appKey");; private static String masterSecret = MyProperties.getByKey("masterSecret");; public static void sendMessage(PushMsg msg) { if (msg.getType().equals("android")) { ITemplate notificationTemplate = notificationTemplateDemo( msg.getTitle(), msg.getMessageInfo(), msg.getBadge()); pushSingleMessage(msg.getCid(), notificationTemplate, false); ITemplate iosTransmissionTemplate = iosTransmissionTemplate( msg.getTitle(), msg.getMessageInfo(), msg.getBadge());// 带APNPayload pushSingleMessage(msg.getCid(), iosTransmissionTemplate, false); } else if (msg.getType().equals("ios")) { // ios透传通知 安卓透传 ITemplate iosTransmissionTemplate = iosTransmissionTemplate( msg.getTitle(), msg.getMessageInfo(), msg.getBadge());// 带APNPayload pushSingleMessage(msg.getCid(), iosTransmissionTemplate, true); } } // 单个推送 private static void pushSingleMessage(String Cid, ITemplate template, boolean offline) { IGtPush push = new IGtPush(appKey, masterSecret, true); SingleMessage message = new SingleMessage(); message.setOffline(offline); // 离线有效时间,单位为毫秒,可选 message.setOfflineExpireTime(24 * 3600 * 1000); message.setData(template); // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发 message.setPushNetWorkType(0); 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()); } } private static NotificationTemplate notificationTemplateDemo(String title, String messageInfo, String badge) { NotificationTemplate template = new NotificationTemplate(); // 设置APPID与APPKEY template.setAppId(appId); template.setAppkey(appKey); // 设置通知栏标题与内容 template.setTitle(title); template.setText(messageInfo); // 配置通知栏图标 template.setLogo("icon.png"); // 配置通知栏网络图标 template.setLogoUrl(""); // 设置通知是否响铃,震动,或者可清除 template.setIsRing(true); template.setIsVibrate(true); template.setIsClearable(true); template.setAPNInfo(getApnPayload(title, messageInfo, badge)); template.setTransmissionType(2); template.setTransmissionContent("notification." + messageInfo); return template; } // ios透传,设置APNPayload参数 private static TransmissionTemplate iosTransmissionTemplate(String title, String messageInfo, String badge) { TransmissionTemplate template = transmissionTemplate(messageInfo); APNPayload payload = getApnPayload(title, messageInfo, badge); // 字典模式使用下者 // payload.setAlertMsg(getDictionaryAlertMsg()); template.setAPNInfo(payload); return template; } private static APNPayload getApnPayload(String title, String messageInfo, String badge) { APNPayload payload = new APNPayload(); // +1在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字 payload.setAutoBadge(badge); payload.setContentAvailable(1); payload.setSound("123.wav"); payload.setCategory("$由客户端定义"); // //简单模式APNPayload.SimpleMsg // payload.setAlertMsg(new APNPayload.SimpleAlertMsg(messageInfo)); DictionaryAlertMsg dictionaryAlertMsg = new DictionaryAlertMsg(); dictionaryAlertMsg.setTitle(title); dictionaryAlertMsg.setBody(messageInfo); payload.setAlertMsg(dictionaryAlertMsg); return payload; } // 透传消息 private static TransmissionTemplate transmissionTemplate(String messageInfo) { TransmissionTemplate template = new TransmissionTemplate(); template.setAppId(appId); template.setAppkey(appKey); template.setTransmissionContent(messageInfo); template.setTransmissionType(2); return template; }}
package com.park.util.push;public class PushMsg { private String cid; private String type; private String title; private String messageInfo; private String badge; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMessageInfo() { return messageInfo; } public void setMessageInfo(String messageInfo) { this.messageInfo = messageInfo; } public String getBadge() { return badge; } public void setBadge(String badge) { this.badge = badge; } public static PushMsg initial(String cid, String type, String title, String messageInfo,String badge) { PushMsg msg = new PushMsg(); msg.setCid(cid); msg.setType(type); msg.setTitle(title); msg.setMessageInfo(messageInfo); msg.setBadge(badge); return msg; }}
在项目上调用就好了
PushMsg msg = PushMsg.initial(clientId, "ios", msgTitle, msgContent, badge + ""); AppMessagePush.sendMessage(msg);
阅读全文
0 0
- 后台app个推
- 个推(App端)
- 个推App消息推送
- HBuilder开发的APP使用个推推送消息
- 个推PC端推送消息至App
- 利用MUI+个推实现APP消息推送
- mui 开发知识 积累总结 --------------Hbuilder APP 个推服务
- 使用hbuilder开发的app集成的个推
- 个推
- android实现home键效果、将当前app推到后台
- IOS使用个推前台可以收到推送消息后台收不到
- 关于HBuilder生成app时,plus.push推送SDK权限问题__选用个推方案
- android个推平台
- 个推使用总结
- 个推的使用方法
- 个推合作案例
- 个推运行环境
- 个推使用总结
- 第十周项目1(2)-由先序序列和中序序列构造二叉树
- 几个面试算法题,附源码
- 图解HTTP笔记:返回结果的HTTP状态码及其表意
- Linux(RedHat)学习之路6.0之逻辑卷管理
- 金蝶首席用户体验官对“用户体验”的思考
- 后台app个推
- webpack入门
- hadoop 之MR的join操作
- echarts.js的基础
- jquery ajax、 原生js ajax、string和json之间的转换
- STM32总结
- 激活函数-ReLU
- Redis常用命令
- 两种开源聊天机器人的性能测试(一)——ChatterBot