java个推实战,Android和Ios
16lz
2021-01-23
前言:
最近公司在接了一个外包项目,要开发微信公众号,Android和Ios三端的项目,公众号不能推送,只能在Android和Ios推送。之前也没研究过个推,所以在个推官网看了下,不懂的又问了个推技术客服,最终Android和Ios两端都能推送成功,自己记录下,方便以后再遇到类似的有个印象,同时也提供给大家参考下。
个推个人中心的配置我就不多说了,Android和Ios都是需要配置的,大家别忘记,首先去个人中心配置成功,在官网测试一下推送,如果没问题,在去代码测试,步骤就是这样的。
个推的模板有很多,Android和Ios我使用的都是透传模板。
1、导入依赖
com.gexin.platform gexin-rp-sdk-http 4.1.0.0
2、Android推送(透传模板)
@Slf4jpublic class WorkerAppPushUtils { //配置从个人中心获取 private static String appId = ""; private static String appKey = ""; private static String masterSecret = ""; private static String host = "http://sdk.open.api.igexin.com/apiex.htm"; /** * Android 透传模板 */ public static String pushTransmissionTemplate(Map map) { IGtPush push = new IGtPush(host, appKey, masterSecret); SingleMessage message = new SingleMessage(); TransmissionTemplate template = new TransmissionTemplate(); template.setAppId(appId); template.setAppkey(appKey); template.setTransmissionContent(map.get("payload").toString()); template.setTransmissionType(2); message.setData(template); message.setOffline(true); //离线有效时间,单位为毫秒,可选 message.setOfflineExpireTime(24 * 1000 * 3600); // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发 message.setPushNetWorkType(0); Target target = new Target(); target.setAppId(appId); target.setClientId(map.get("cid").toString()); IPushResult ret = null; try { ret = push.pushMessageToSingle(message, target); } catch (RequestException e) { e.printStackTrace(); ret = push.pushMessageToSingle(message, target, e.getRequestId()); } if (ret != null) { return ret.getResponse().toString(); } else { log.error("【Android】个推,服务器异常"); return ""; } } public static void main(String[] args) { HashMap map = new HashMap<>(); map.put("title", "通知"); map.put("content", "您有新的订单"); map.put("cid", "d7f1b96fe4799b64226ccb4c38c97030"); map.put("payload", "{title:\"通知标题\",content:\"通知内容\",\"payload\":\"\"}"); String sa = pushTransmissionTemplate(map); System.out.println(sa); }}
3、ios推送(透传模板+apninfo)
@Slf4jpublic class WorkerAppPushUtils { //配置从个人中心获取 private static String appId = ""; private static String appKey = ""; private static String masterSecret = ""; private static String host = "http://sdk.open.api.igexin.com/apiex.htm"; /** * 单个用户ios推送 * * @param map * @return */ public static String sendSingleIos(Map map) { IGtPush push = new IGtPush(host, appKey, masterSecret); SingleMessage message = new SingleMessage(); TransmissionTemplate template = new TransmissionTemplate(); template.setAppId(appId); template.setAppkey(appKey); template.setTransmissionContent(map.get("payload").toString()); // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动 template.setTransmissionType(2); APNPayload payload = new APNPayload(); //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字 payload.setAutoBadge("+1"); payload.setContentAvailable(1); //ios 12.0 以上可以使用 Dictionary 类型的 sound payload.setSound("default"); payload.setCategory("$由客户端定义"); //简单模式APNPayload.SimpleMsg //payload.setAlertMsg(new APNPayload.SimpleAlertMsg(""+map.get("content"))); //字典模式使用下者 payload.setAlertMsg(getDictionaryAlertMsg(map)); template.setAPNInfo(payload); message.setData(template); message.setOffline(true); //离线有效时间,单位为毫秒,可选 message.setOfflineExpireTime(24 * 1000 * 3600); // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发 message.setPushNetWorkType(0); Target target = new Target(); target.setAppId(appId); target.setClientId(map.get("cid").toString()); IPushResult ret = null; try { ret = push.pushMessageToSingle(message, target); } catch (RequestException e) { e.printStackTrace(); ret = push.pushMessageToSingle(message, target, e.getRequestId()); } if (ret != null) { return ret.getResponse().toString(); } else { log.error("【IOS】个推,服务器异常"); return ""; } } private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(Map map) { APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg(); alertMsg.setBody(map.get("body").toString()); alertMsg.setLocKey("" + map.get("content")); alertMsg.setActionLocKey("ActionLockey"); alertMsg.addLocArg("loc-args"); alertMsg.setLaunchImage("launch-image"); // IOS8.2以上版本支持 alertMsg.setTitle("" + map.get("title")); alertMsg.setTitleLocKey("" + map.get("title")); alertMsg.addTitleLocArg("TitleLocArg"); return alertMsg; } public static void main(String[] args) { HashMap map = new HashMap<>(); map.put("title", "通知"); map.put("content", "您有新的订单"); map.put("cid", "9b3a1a0f564f40df4456c7f06fd55d7b"); map.put("payload", "{title:\"通知标题\",content:\"通知内容\"}"); map.put("body", "您有新的订单"); String s = sendSingleIos(map); System.out.println(s); }}
总结:
透传模板是推送消息到前端,由前端处理之后再展示。上面两种方式都可以成功推送,我已经测试过,我目前的逻辑是在登录的时候获取到ClientID保存到数据库,在订单提交5分钟之后,没有人接单就推送给附近的可以接收订单的人。
更多相关文章
- Android应用开发笔记(13): Android移动应用界面的模板化设计
- Android移动应用界面的模板化设计
- Android移动应用界面的模板化设计【自定义BaseActivity】
- android中的通知
- android中如何隐藏应用程序标题栏和通知栏
- Android消息通知(notification)和PendingIntent传值
- 获取Android屏幕尺寸、控件尺寸、状态栏/通知栏高度、导航栏高度
- Android(Java):Android的状态栏通知(Notification)
- cordova的android notify消息通知插件