Android開發之極光推送的整合

yungfan發表於2016-04-26

客戶器端整合

1、註冊app,生成appKey 和 Master Secret

img_4d032df2f742f2d6de61a55bf4f62b1c.png
註冊app.png

2、匯入SDK開發包到你自己的應用程式專案

img_9e4bad43db8cb09468524517d55d158f.png
配置sdk.png

3、配置 AndroidManifest.xml ,這部分內容太長,官方有明確說明,就不貼上了
主要步驟為:
(1)複製備註為 “Required” 的部分
(2)將備註中所有 您應用的包名 的部分,替換為當前應用程式的包名
(3)將appKey替換為註冊時的appKey

4、新建一個Application,如MyApp,重寫onCreate(),進行引數初始化

public void onCreate()
 {
        super.onCreate();
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
 }

5、AndroidManifest.xml中配置上面的application,即:

<application android:name="com.abc.MyApp">

官方參考:http://docs.jpush.io/guideline/android_guide/


伺服器端整合

極光推送預設有個管理後臺,但是需要每次登入到它的網站進行推送,覺得不是很方便,所以按照官方文件整合一個簡單的伺服器端,步驟如下:

1、下載服務端SDK,準備好如下jar包,一個不能少

img_d311fdee988ab395941007c9d869a43a.png
必須的jar包.PNG

2、封裝訊息和推送方法
MessagePush.java

/** * 封裝訊息推送方法 */
public class MessagePush {

    private static final String appKey = "你申請的appkey";
    private static final String masterSecret = "你申請時生成的masterSecret ";
    private JPushClient jpushClient;
    private String title;//推送的標題
    private String message;//推送的內容

    public MessagePush(String message) {
        this.message = message;
        //建立JPushClient
        jpushClient = new JPushClient(masterSecret, appKey, 3);
    }

    public MessagePush(String message, String title) {
        this(message);
        this.title = title;
    }

    /**
     * 向所有人傳送訊息
     * 
     * @return 訊息id
     */
    public long sendPushAll() {
       //構建一個PushPayload物件(推送物件)
        PushPayload payload = buildPushObject_all_all_alert();
        long msgId = 0;
        try {
            //推送訊息
            PushResult result = jpushClient.sendPush(payload);
            msgId = result.msg_id;
        } catch (APIConnectionException e) {
        } catch (APIRequestException e) {
            msgId = e.getMsgId();
        }
        return msgId;
    }

    /**
     * 向指定別名的客戶端傳送訊息
     * 
     * @param alias
     *            所有別名資訊集合,這裡表示傳送所有學生編號
     * @return 訊息id
     */
    public long sendPushAlias(Set<String> alias) {
        PushPayload payloadAlias = buildPushObject_android_alias_alertWithTitle(alias);
        long msgId = 0;
        try {
            PushResult result = jpushClient.sendPush(payloadAlias);
            msgId = result.msg_id;

        } catch (APIConnectionException e) {
        } catch (APIRequestException e) {
            msgId = e.getMsgId();
        }
        return msgId;
    }

    /**
     * 向指定組傳送訊息
     * 
     * @param tag
     *            組名稱
     * @return 訊息id
     */
    public long sendPushTag(String tag) {
        PushPayload payloadtag = buildPushObject_android_tag_alertWithTitle(tag);
        long msgId = 0;
        try {
            PushResult result = jpushClient.sendPush(payloadtag);
            msgId = result.msg_id;
        } catch (APIConnectionException e) {

        } catch (APIRequestException e) {
            msgId = e.getMsgId();
        }
        return msgId;
    }

    /**
     * 封裝了三種獲得訊息推送物件(PushPayload)的方法
     */
    public PushPayload buildPushObject_android_alias_alertWithTitle(
            Set<String> alias) {
        return PushPayload.newBuilder().setPlatform(Platform.android())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.android(message, title, null))
                .build();
    }

    public PushPayload buildPushObject_android_tag_alertWithTitle(String tag) {
        return PushPayload.newBuilder().setPlatform(Platform.android())
                .setAudience(Audience.tag(tag))
                .setNotification(Notification.android(message, title, null))
                .build();
    }

    public PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll(message);
    }
}

3、模擬伺服器端推送,先全部推送,然後按Tag推送
Main.java

public class Main {

    public static void main(String[] args) {
        // 傳送三次網路推送

        new Thread(new Runnable() {

            public void run() {

                for (int i = 0; i < 3; i++) {

                    // MessagePush push = new MessagePush("這是網路推送" + i, "測試"
                    // + i);

                    MessagePush push = new MessagePush("這是按Tag進行推送" + i, "測試" + i);

                    long msgId = push.sendPushTag("ABC");

                    System.out.println("********" + msgId);
                }
            }
        }).start();

    }

}

4、測試效果

img_abfbccbb8952f41b8f9707ccf21a053c.png
伺服器端執行.png
  • 全部推送
img_50e570c82d4ef1650b2645bd32d8dd48.png
全部推送.png
  • 按Tag推送
img_460271d66788be76b2a8d477ede05348.png
按Tag推送.png

官方參考:http://docs.jpush.io/server/java_sdk/


相關文章