以浙江產的 個推為例 官網就是:http://docs.getui.com/
有安卓的 ios的 還有java的 都是寫好的demo和定義好的接口,方法 非常方便 拿來即用。
若前端為mui集成框架開發的話,ios的會比較麻煩一點 因為ios有自己的消息推送通道即 apns 消息通道。這里不詳細說明了。
若是原生的 基本上沒啥子 大問題。
其中有java部分 以此為例:
有限前端app框架為mui框架:
代碼實現也筆記簡單,就是接收下后台來的 透傳消息做下處理即可(其中透傳的消息可以是帶參數的 也可以不帶參數直接傳個路徑);
pushParameter 是參數集合 注意此段代碼應放在search 主頁面下。若放在index,下會不穩定 極不穩定。能成功接收到透傳消息參數的概率極低。
網上也查了下,使用其他個推 如百度推送 等 前端代碼都大體差不多。
document.addEventListener( "plusready", function(){
message = document.getElementById("message");
// 監聽在線消息事件
plus.push.addEventListener( "receive", function( msg ) {
var info = plus.push.getClientInfo();
var jsonStr=msg.content;
// if ( msg.aps ) { // Apple APNS message
// alert("接收到在線APNS消息:111" );
// outSet( "接收到在線APNS消息:" );
// } else {
// //alert("接收到在線透傳消息:" );
// var jsonStr=msg.content;
// var content=JSON.parse(jsonStr)
// //alert(content.type); alert(content.clientid);
// outSet( "接收到在線透傳消息:" );
// }
var pushinfo=JSON.parse(jsonStr)
var viewId = pushinfo.ViewId;
var viewUrl = pushinfo.viewUrl;
var pushParameter = pushinfo.pushParameter;
//alert(pushParameter);
if(pushinfo.type == "1"){
if(viewUrl != null && viewUrl != ""){
mui.openWindow({
id: viewId,
url: viewUrl,
show: {
aniShow: "pop-in"
},
waiting: {
autoShow: true
},
extras: pushParameter
});
}
}
}, false );
// 監聽點擊消息事件
plus.push.addEventListener( "click", function( msg ) {
// 判斷是從本地創建還是離線推送的消息
switch( msg.payload ) {
case "LocalMSG":
outSet( "點擊本地創建消息啟動:" );
break;
default:
outSet( "點擊離線推送消息啟動:");
break;
}
// 提示點擊的內容
plus.ui.alert( msg.content );
// 處理其它數據
logoutPushMsg( msg );
}, false );
}, false );
至於后台就更為簡單了:
創建消息對象 調用接口 發送 ok了 例:
PushInfo pushInfo = new PushInfo(); PushParameter pushParameter = new PushParameter(); pushParameter.setCarId(searchCarNormalId); pushParameter.setCarBrandName(searchCarNormal.getCarBrandName()); pushParameter.setCarModelName(searchCarNormal.getModelName()); pushParameter.setCarStyleName(searchCarNormal.getStyleName()); pushInfo.setType(PushInfo.TYPE.DETAILPAGE);//選擇是否 跳轉頁面 pushInfo.setViewUrl(PushInfo.VIEWURL.findNormaDetail);//頁面跳轉路徑 pushInfo.setTitle(PushInfo.TITLE.normaSystem);//消息標題 pushInfo.setBody(content);//消息內容 pushInfo.setPushParameter(pushParameter);//透傳消息參數 實體對象 pushInfo.setImge(PushInfo.IMGE.DEFAULT);//個推常規設置-顯示的標題圖片名 pushInfo.setUrl(PushInfo.URL.NULL);//個推常規設置-顯示的標題圖片路徑 pushInfo.setClientid(buyUser.getClientid());//從 后台獲取到的CID // pushInfo.setGoUrl("http://www.baidu.com"); MessageSending.messageSending(mobile, content, pushInfo);//調用發送個推和短信 工具類中的方法
單啟線程進行消息 發送 因為個推會用到 別人的接口 和服務 有時候會比較慢所以:
public static void messageSending(String mobile, String content, PushInfo pushInfo) { new Thread(IntermessageSending(mobile,content,pushInfo)).start(); } private static Runnable IntermessageSending(String mobile, String content, PushInfo pushInfo) { SendSms.SMS(mobile,MobileShort.CHECKTYPE.NOTICE, content); PushMessageUtil.linkTemplate(pushInfo); return null; }
具體實現,這里用的都是別人的接口及方法 官網上都有api 文檔說的很詳細:
public static NotificationTemplate linkTemplate(PushInfo pushInfo) { IGtPush push = new IGtPush(host, appKey, masterSecret); SingleMessage message = new SingleMessage(); NotificationTemplate template = new NotificationTemplate(); // 設置APPID與APPKEY template.setAppId(appId); template.setAppkey(appKey); //ios apns通道消息欄通知設置 APNPayload aPNPayload=new APNPayload(); //aPNPayload.setAutoBadge("+1");//設置角標數值 aPNPayload.setContentAvailable(1); aPNPayload.setSound("default"); aPNPayload.setAlertMsg(getDictionaryAlertMsg(pushInfo)); aPNPayload.setContentAvailable(0); template.setAPNInfo(aPNPayload); //透傳消息設置,1,為強制啟動應用, 2:等待應用啟動 template.setTransmissionType(1); template.setTransmissionContent(JSONObject.toJSONString(pushInfo)); Style0 style=new Style0(); // 安卓設置通知欄標題與內容 style.setTitle(pushInfo.getTitle()); style.setText(pushInfo.getBody()); // 配置通知欄圖標 style.setLogo(pushInfo.getImge()); // 配置通知欄網絡圖標,填寫圖標URL地址 style.setLogoUrl(pushInfo.getUrl()); // 設置通知是否響鈴,震動,或者可清除 style.setRing(true); style.setVibrate(true); style.setClearable(true); template.setStyle(style); //設置app離線消息發送緩存 message.setOffline(true); // 離線有效時間,單位為毫秒,可選 message.setOfflineExpireTime(24 * 3600 * 1000); message.setData(template); // 可選,1為wifi,0為不限制網絡環境。根據手機處於的網絡情況,決定是否下發 message.setPushNetWorkType(0); extracted(push, message,pushInfo.getClientid()); return template; }