MUI開發的app集成個推服務


送您一個最高1888元的阿里雲大禮包,快來領取吧~

 

1.首先就是去個推官網注冊用戶  (官網地址     https://www.getui.com/cn/index.html)此處不做詳解

2.在官網上下載對應的sdk,我下載的是Java版本   (此處應注意客戶端sdk和服務端sdk,踩過坑

  該api支持Android和iso的推送。當然需要對Android和iso分別處理

   

3.后台代碼實現

  如果是普通的web項目,直接將jar包拷貝到lib下

  如果是maven項目,在pom文件中配置

<!--移動端消息推送(個推)start-->
        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-base</artifactId>
            <version>4.0.0.20</version>
        </dependency>
        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-http</artifactId>
            <version>4.0.1.15</version>
        </dependency>
        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-template</artifactId>
            <version>4.0.0.14</version>
        </dependency>
        <dependency>
            <groupId>com.gexin</groupId>
            <artifactId>gexin-rp-fastjson</artifactId>
            <version>1.0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>2.5.0</version>
        </dependency>
        <!--移動端消息推送(個推)end-->

我這邊實現了兩個推送模板(1. 點擊通知打開應用模板,2.點擊通知打開網頁消息)

package com.zsplat.contract.message.pushmessage;


import com.gexin.rp.sdk.base.IBatch;
import com.gexin.rp.sdk.base.IIGtPush;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;

import java.io.IOException;

public class MyBatchPushDemo {
    private static String appId = "Up6z3dQcbw9W8QkTcX3mK9";
    private static String appKey = "ryTDQMsFZy7pb2BDJjp1s4";
    private static String masterSecret = "rAKmpj2Xjb68b0A293DuU7";

    static String CID_A = "81a3d3c3bee1adb51d306771af2aeb6a";//在打包后的APP的js中獲取 var cId = plus.push.getClientInfo().clientid;
    static String CID_B = "bae837b470994d614f0773097b92dbf3";
    //    static String CID_B = "bae837b470994d614f0773097b92dbf3";
    //別名推送方式
    // static String Alias = "";
    static String host = "https://sdk.open.api.igexin.com/apiex.htm";
//    static String host = "http://sdk.open.api.igexin.com/apiex.htm";

    public static void main(String[] args) throws IOException {

        IIGtPush push = new IGtPush(host, appKey, masterSecret);
        IBatch batch = push.getBatch();

        try {
            //構建客戶a的透傳消息a
            constructClientTransMsg(CID_A, "msgA", batch);
            //構建客戶B的點擊通知打開網頁消息b
            constructClientLinkMsg(CID_B, "msgB", batch);
        } catch (Exception e) {
            e.printStackTrace();
        }
        batch.submit();
    }
    //點擊通知打開應用模板
    private static void constructClientTransMsg(String cid, String msg, IBatch batch) throws Exception {

        SingleMessage message = new SingleMessage();
        NotificationTemplate template = new NotificationTemplate();
//        TransmissionTemplate template = new TransmissionTemplate();//自定義模板
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(msg);//消息內容
        template.setTransmissionType(1); // 這個Type為int型,填寫1則自動啟動app
        Style0 style = new Style0();        // 設置通知欄標題與內容
        style.setTitle("請輸入通知欄標題");
        style.setText("請輸入通知欄內容");        // 配置通知欄圖標
        style.setLogo("icon.png");        // 配置通知欄網絡圖標
        style.setLogoUrl("");//網絡圖標地址
        // 設置通知是否響鈴,震動,或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        message.setData(template);
        message.setOffline(true);
        message.setOfflineExpireTime(60 * 1000);

        // 設置推送目標,填入appid和clientId
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        batch.add(message, target);
    }
    //點擊通知打開網頁消息
    private static void constructClientLinkMsg(String cid, String msg, IBatch batch) throws Exception {

        SingleMessage message = new SingleMessage();
        LinkTemplate template = new LinkTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTitle("title");
        template.setText(msg);
        template.setLogo("push.png");
        template.setLogoUrl("logoUrl");
        template.setUrl("http://www.baidu.com");

        message.setData(template);
        message.setOffline(true);
        message.setOfflineExpireTime(60 * 1000);

        // 設置推送目標,填入appid和clientId
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        batch.add(message, target);
    }
}

這里都是對單用戶發送

其他功能參考官方文檔(http://docs.getui.com/getui/server/java/template/)

4.在HBuilder中配置推送插件的步驟如下

1.雙擊應用的manifest.json文件,點擊“sdk配置”

2.點擊manifest.json文件的“代碼視圖”,在permissions節點下添加push節點:

"Push": {
"description": "管理推送消息插件"
},

3.在plus -> distribute -> plugins 節點下添加push節點

"push": {
    "mkeypush":{
        "appid":"必選,mkey推送應用標示",
        "appkey":"必選,mkey推送應用的appkey",
        "server" :"必選,mkey推送服務器地址"
    },
    "igexin": {
        "appid": "必選,個推應用標識",
        "appkey": "必選,個推應用appkey",
        "appsecret": "必選,個推應用安全標識"
    }
},

4.添加不同推送平台的方法
a)個推推送平台
1.在plus->distribute->plugins->push節點下添加igexin節點。
appid屬性,填寫在個推平台創建的應用的appid
appkey屬性,填寫在個推平台創建的應用的appkey
appsecret屬性,填寫在個推平台創建的應用的appseret
b)MKey 推送服務
MKey推送服務為企業用戶提供的一套私有的推送解決方案。目前暫不對公眾用戶開放。
1.在plus->distribute->plugins->push節點下添加mkeypush節點。
appid屬性,填寫MKey推送應用標識
appkey屬性,填寫MKey推送應用appkey
server屬性,填寫MKey推送服務器地址

5.前台代碼處理(網上說要處理,我的處理和不處理結果一樣)

<script type="text/javascript">
            mui.init({
                swipeBack: false //右滑關閉
            });
            mui.plusReady(function() {
                LoginIndex.init();
            });
            document.addEventListener("plusready", function() {
                var message = document.getElementById("message");
                // 監聽點擊消息事件
                plus.push.addEventListener("click", function(msg) {
                    // 判斷是從本地創建還是離線推送的消息
                    switch(msg.payload) {
                        case "LocalMSG":
                            outSet("點擊本地創建消息啟動:");
                            break;
                        default:
                            outSet("點擊離線推送消息啟動:");
                            break;
                    }
                    // 提示點擊的內容
                    plus.ui.alert(msg.content);
                    // 處理其它數據
                    logoutPushMsg(msg);
                }, false);
                // 監聽在線消息事件
                plus.push.addEventListener("receive", function(msg) {
                    if(msg.aps) { // Apple APNS message
                        outSet("接收到在線APNS消息:");
                    } else {
                        outSet("接收到在線透傳消息:");
                    }
                    logoutPushMsg(msg);
                }, false);
            }, false);
        </script>

6.注意事項

  1.需要打包測試,真機運行不支持推送。

  2.如何獲取到實際的clientid(安卓)或者token(蘋果),在H5+功能接口中有說明;

plus.push.getClientInfo().clientid; 
plus.push.getClientInfo().token;

clientid和token也需要實際打包后獲取,真機測試可能會不一致。

 3.打包需要安全證書


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM