極光推送與IOS實例

參考Api
第一步 maven

將下邊的依賴條件放到你項目的 maven pom.xml 文件裏。

<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.2.17</version>
</dependency>

如果使用 Maven 構建項目,則需要在你的項目 pom.xml 裏增加:

    <dependency>
        <groupId>cn.jpush.api</groupId>
        <artifactId>jiguang-common</artifactId>
        <version>1.0.3</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.6.Final</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>

    <!-- For log4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

第二步 代碼已經全部寫到一個方法裏面(通過別名實現)

 public static void main(String[] args) {
            String alias = "app_doctor_123456";//聲明別名
            String masterSecret = "value";//需要註冊才能得到
            String appKey = "key";//需要註冊才能得到,ios安卓會告訴我們
            String ALERT = "推送信息啊啊啊啊啊啊啊啊啊啊11111111";    
            log.info("對別名" + alias + "的用戶推送信息");
            JSONObject json = new JSONObject();
            json.put("11", true);
            json.put("22", "33");
            ClientConfig clientConfig = ClientConfig.getInstance();
            JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
            PushPayload payload = PushPayload.newBuilder()
                    .setPlatform(Platform.android_ios())
                    .setAudience(Audience.alias(alias))
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(IosNotification.newBuilder()
                                    .addExtra("data", json.toJSONString())
                                    .setAlert(ALERT)
                                    .build())
                            .build())
                    .setOptions(Options.newBuilder()
                            .setApnsProduction(false)//true-推送生產環境 false-推送開發環境(測試使用參數)
                            .setTimeToLive(90)//消息在JPush服務器的失效時間(測試使用參數)
                            .build())
                    .build();
            //PushPayload payload = buildPushObject_ios_messageWithExtras(alias);
            try {
                //payload.messageAll("內容");
                 jpushClient.sendPush(payload);
            } catch (APIConnectionException e) {
                log.error("Connection error. Should retry later. ", e);
            } catch (APIRequestException e) {
                log.error("Error response from JPush server. Should review and fix it. ", e);
                log.info("HTTP Status: " + e.getStatus());
                log.info("Error Code: " + e.getErrorCode());
                log.info("Error Message: " + e.getErrorMessage());
                log.info("Msg ID: " + e.getMsgId());
            }    

        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章