極光推送的代碼實現

一:去官網下載SDK或者直接在maven項目中進行相應配置

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>cn.jpush.api</groupId>
        <artifactId>jpush-client</artifactId>
        <version>3.2.17</version>
    </dependency>
  </dependencies>

二:按照API文檔示例進行相應的修改添加

進行推送的關鍵在於構建一個 PushPayload 裝載對象。html

推送的話大體分爲:

  1. 對全部平臺全部設備上的進行推送
  2. 對全部平臺指定推送目標進行推送
  3. 對Android平臺上的指定設備進行推送
  4. 對IOS平臺上的指定設備進行推送
  5. 對Android+IOS平臺上的指定設備進行推送
  6. 給指定設備發送SMS信息

接下來咱們須要在SDK的基礎之上設置合適的調用參數進行取用java

package com.test.push.messagepush01;
     
import java.util.Map;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.SMS;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class PushTest{
    
    private static final String MASTER_SECRET="9a3869c9d2fec8333db*****";//JPush服務器端下發的master_key
    private static final String APP_KEY="0e621306bf07eb4eefc*****";//JPush服務器端下發的app_key
    
    /**
     * 構建推送對象:對全部平臺,全部設備,內容爲 alert的通知
     * @param alter
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert(String alter) {
        return PushPayload.alertAll(alter);
    } 
    /**
     * 全部平臺,推送目標是別名爲 "alias",通知內容爲 alert
     * @param alias
     * @param alert
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(String alias,Object alert) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.alert(alert))
                .build();
    }
    /**
     * 構建推送對象:平臺是 Android,目標是 tag的設備,通知內容是alert,而且標題爲title。
     * @param tag
     * @param alert
     * @param title
     * @param extras
     * @return
     */
    public static PushPayload buildPushObject_android_tag_alertWithTitle(String tag,String alert,String title,Map<String, String> extras) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag(tag))
                .setNotification(Notification.android(alert, title, extras))
                .build();
    }
    /**
     * 構建推送對象:平臺是 iOS,推送目標是 tags(能夠是一個設備也能夠是多個設備),推送內容同時包括通知與消息 - 通知信息是alert,消息內容是 msgContent,角標數字爲badge(應用程序左上角或者右上角的數字),通知聲音爲sound,而且附加字段 from = "JPush"。
     * 通知是 APNs 推送通道的,消息是 JPush 應用內消息通道的。
     * APNs 的推送環境是「生產」(若是不顯式設置的話,Library 會默認指定爲開發)
     * @param alert
     * @param msgContent
     * @param badge
     * @param sound
     * @param tags
     * @return
     */
    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage(Object alert,String msgContent,int badge,String sound,String...tags) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and(tags))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(alert)
                                .setBadge(badge)
                                .setSound(sound)
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                 .setMessage(Message.content(msgContent))
                 .setOptions(Options.newBuilder()
                         .setApnsProduction(true)
                         .build())
                 .build();
    }
    /**
     * 構建推送對象:平臺是 Andorid 與 iOS,推送的設備有(推送目標爲tags和推送目標別名爲aliases),推送內容是 - 內容爲 msg_content的消息,而且附加字段 from = JPush。
     * @param msg_content
     * @param tags
     * @param aliases
     * @return
     */
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String msg_content,String[] tags,String[] aliases) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag(tags))
                        .addAudienceTarget(AudienceTarget.alias(aliases))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(msg_content)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }
    /**
     * 構建推送對象:推送內容包含SMS信息
     * @param title
     * @param sendSMSContent
     * @param delayTime
     * @param aliases
     */
    public static void testSendWithSMS(String title,String sendSMSContent,int delayTime,String... aliases) {
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        try {
            SMS sms = SMS.content(sendSMSContent, delayTime);
            PushResult result = jpushClient.sendAndroidMessageWithAlias(title, sendSMSContent, sms, aliases);
            System.out.println("Got result - " + result);
        } catch (APIConnectionException e) {
            System.out.println("Connection error. Should retry later. "+e);
        } catch (APIRequestException e) {
            System.out.println("Error response from JPush server. Should review and fix it. "+e);
            System.out.println("HTTP Status: " + e.getStatus());
            System.out.println("Error Code: " + e.getErrorCode());
            System.out.println("Error Message: " + e.getErrorMessage());
        }
    }
    
    public static void main(String[] args) {
        String master_secret=PushTest.MASTER_SECRET;
        String app_key=PushTest.APP_KEY;
        JPushClient jpushClient = new JPushClient(master_secret,app_key, null, ClientConfig.getInstance());
        PushPayload payload = PushTest.buildPushObject_all_all_alert("消息推送");
        //PushPayload payload=PhicommPush.buildPushObject_android_tag_alertWithTitle("tag1", "123", "123", null);
        
        try {
            PushResult result = jpushClient.sendPush(payload);
            System.out.println("Got result - " + result);

        } catch (APIConnectionException e) {
            // Connection error, should retry later
            System.out.print("Connection error, should retry later "+e);

        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            System.out.println("根據返回的錯誤信息覈查請求是否正確"+e);
            System.out.println("HTTP 狀態信息碼: " + e.getStatus());
            System.out.println("JPush返回的錯誤碼: " + e.getErrorCode());
            System.out.println("JPush返回的錯誤信息: " + e.getErrorMessage());
        }
    }
}

3、查看推送日誌:

4、在極光開發者服務中查看剛纔推送的消息android

登陸到極光開發者頁面中https://www.jiguang.cn/ios

 

推送多環境配置見:http://www.cnblogs.com/lingyejun/p/7943779.htmlweb

相關文章
相關標籤/搜索