【个推】后端java开发
生活随笔
收集整理的這篇文章主要介紹了
【个推】后端java开发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
申請應用:
申請之后,我們開發人員需要拿到的是
?
開發步驟:
1.在pom文件中引入個推依賴
<!--個推--><dependency><groupId>com.gexin.platform</groupId><artifactId>gexin-rp-sdk-template</artifactId><version>4.0.0.16</version></dependency><dependency><groupId>com.gexin.platform</groupId><artifactId>gexin-rp-sdk-http</artifactId><version>4.0.1.17</version></dependency>2.個推可以使用cid對個別用戶推送,也可以把cid綁定別名(客戶端工作),用別名進行推送,我這里使用別名進行推送
package com.dingyi.common.util;import com.alibaba.fastjson.JSONObject; import com.dingyi.common.Message.PushMessage; import com.dingyi.common.base.CommonConstant; import com.gexin.rp.sdk.base.IPushResult; import com.gexin.rp.sdk.base.impl.AppMessage; import com.gexin.rp.sdk.base.impl.SingleMessage; import com.gexin.rp.sdk.base.impl.Target; import com.gexin.rp.sdk.base.payload.APNPayload; import com.gexin.rp.sdk.base.uitls.AppConditions; import com.gexin.rp.sdk.exceptions.RequestException; import com.gexin.rp.sdk.http.IGtPush; import com.gexin.rp.sdk.template.NotificationTemplate; import com.gexin.rp.sdk.template.TransmissionTemplate; import com.gexin.rp.sdk.template.style.Style0; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;import java.util.ArrayList; import java.util.List;@Component public class Getui2Util {private static Logger logger = LoggerFactory.getLogger(Getui2Util.class);private static String appId = "GI*************VqHPT4";private static String appKey = "7m*************iRvbMaA";private static String masterSecret = "CZ*************HMGWeW2";static String host = "http://sdk.open.api.igexin.com/apiex.htm";/*** 推送給特定ios/安卓用戶 注意:不是直接通知,需要客戶端處理** @param pushMessage* @param alias*/public void pushMessageToOne(PushMessage pushMessage, String alias) {IGtPush push = new IGtPush(host, appKey, masterSecret);TransmissionTemplate template = getTemplate(pushMessage);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);//target.setClientId(CID.toString()); //使用cid來推送target.setAlias(alias);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("服務器響應異常");}}/*** 推送給特定安卓用戶 注意:ios接收不到** @param pushMessage* @param alias*/public void pushMessageToAndriod(PushMessage pushMessage, String alias) {IGtPush push = new IGtPush(host, appKey, masterSecret);NotificationTemplate template = notificationTemplate(pushMessage);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);//target.setClientId(CID.toString());target.setAlias(alias);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("服務器響應異常");}}/*** 把信息推送給安卓app內所有用戶** @param pushMessage*/public void pushMessageToApp(PushMessage pushMessage, String appTaskName) {IGtPush push = new IGtPush(host, appKey, masterSecret);//模版可以替換 這里不是透傳模版 ios收不到NotificationTemplate template = notificationTemplate(pushMessage);AppMessage message = new AppMessage();message.setData(template);message.setOffline(true);//離線有效時間,單位為毫秒,可選message.setOfflineExpireTime(24 * 1000 * 3600);//推送給App的目標用戶需要滿足的條件AppConditions cdt = new AppConditions();List<String> appIdList = new ArrayList<String>();appIdList.add(appId);message.setAppIdList(appIdList);//手機類型List<String> phoneTypeList = new ArrayList<String>();//省份List<String> provinceList = new ArrayList<String>();//自定義tagList<String> tagList = new ArrayList<String>();cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);cdt.addCondition(AppConditions.REGION, provinceList);cdt.addCondition(AppConditions.TAG, tagList);message.setConditions(cdt);//這個任務的名稱,不會被展示IPushResult ret = push.pushMessageToApp(message, appTaskName);System.out.println(ret.getResponse().toString());}/*** 安卓模版*/public NotificationTemplate notificationTemplate(PushMessage pushMessage) {NotificationTemplate template = new NotificationTemplate();// 設置APPID與APPKEYtemplate.setAppId(appId);template.setAppkey(appKey);// 透傳消息設置,1為強制啟動應用,客戶端接收到消息后就會立即啟動應用;2為等待應用啟動template.setTransmissionType(1);template.setTransmissionContent("點擊啟動應用");// 設置定時展示時間// template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");Style0 style = new Style0();// 設置通知欄標題與內容style.setTitle(pushMessage.getTitle());style.setText(pushMessage.getMsg());// 配置通知欄圖標//style.setLogo("icon.png");// 配置通知欄網絡圖標//style.setLogoUrl("");// 設置通知是否響鈴,震動,或者可清除style.setRing(true);style.setVibrate(true);style.setClearable(true);template.setStyle(style);return template;}/*** ios/android透傳模版*/public static TransmissionTemplate getTemplate(PushMessage pushMessage) {TransmissionTemplate template = new TransmissionTemplate();template.setAppId(appId);template.setAppkey(appKey);template.setTransmissionContent(pushMessage.getMsg());template.setTransmissionType(2);APNPayload payload = new APNPayload();//在已有數字基礎上加1顯示,設置為-1時,在已有數字上減1顯示,設置為數字時,顯示指定數字payload.setAutoBadge("+1");payload.setContentAvailable(1);payload.setSound("default");//payload.setCategory("$由客戶端定義");//簡單模式APNPayload.SimpleMsg//payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));//字典模式使用APNPayload.DictionaryAlertMsgpayload.setAlertMsg(getDictionaryAlertMsg(pushMessage));// 添加多媒體資源/*payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video).setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4").setOnlyWifi(true));*///需要使用IOS語音推送,請使用VoIPPayload代替APNPayload// VoIPPayload payload = new VoIPPayload();// JSONObject jo = new JSONObject();// jo.put("key1","value1");// payload.setVoIPPayload(jo.toString());//template.setAPNInfo(payload);return template;}/*** ios離線APNS配置** @return*/private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(PushMessage pushMessage) {APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();alertMsg.setBody(pushMessage.getMsg());//alertMsg.setActionLocKey("ActionLockey");//alertMsg.setLocKey("LocKey");//alertMsg.addLocArg("loc-args");//alertMsg.setLaunchImage("launch-image");// iOS8.2以上版本支持alertMsg.setTitle(pushMessage.getTitle());//alertMsg.setSubtitle("子標題");//alertMsg.setTitleLocKey("TitleLocKey");//alertMsg.addTitleLocArg("TitleLocArg");return alertMsg;}}3.PushMessage類
public class PushMessage extends BaseModel {private String id;private String to;private String title;private String image;private String msg;private Date timestamp;private String readed;private String reached;//離線=offline ;在線=online ;系統 = systemprivate String type;//0=文本; 1=圖片;2=音頻; 3=視頻; 4=簡歷; 5=職位; 6=約面; 7=房源; 8=求租 ;// 9=預約看房;10=名片; 20=招聘;22=租房;25=系統;55=已讀未讀; 77=登錄消息; 99=actionprivate String msgtype;private String destination; }?
客戶端接收不到通知的幾種可能:
1.andriod能接受消息,而ios不行,有可能模版選錯,ios只能接收透傳模板
2.關于透傳模版,在客戶端是沒有任何提示的,透傳消息個推SDK接收到后直接廣播給客戶端,不做任何處理,需要客戶端自己去處理。確認客戶端是否對透傳消息進行處理
?
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的【个推】后端java开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为hcie报考条件
- 下一篇: linux虚拟机中安装geth