關於個推那點事(使用不介紹原理)

 

以浙江產的 個推爲例 官網就是:http://docs.getui.com/前端

有安卓的 ios的 還有java的 都是寫好的demo和定義好的接口,方法 很是方便 拿來即用。java

若前端爲mui集成框架開發的話,ios的會比較麻煩一點 由於ios有本身的消息推送通道即 apns 消息通道。這裏不詳細說明了。ios

如果原生的 基本上沒啥子 大問題。json

其中有java部分 以此爲例:api

有限前端app框架爲mui框架:緩存

代碼實現也筆記簡單,就是接收下後臺來的 透傳消息作下處理便可(其中透傳的消息能夠是帶參數的 也能夠不帶參數直接傳個路徑);網絡

pushParameter 是參數集合 注意此段代碼應放在search 主頁面下。若放在index,下會不穩定 極不穩定。能成功接收到透傳消息參數的機率極低。app

網上也查了下,使用其餘個推 如百度推送 等 前端代碼都大致差很少。框架

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;
  }
相關文章
相關標籤/搜索