極光推送(JPush)開篇


Date:2019-11-11

讀前思考:

  極光推送是什么?

  極光推送是能做什么?有什么優勢?

  怎么根據業務需求來實現極光推送服務呢?

簡介

極光推送(JPush)是獨立的第三方雲推送平台,致力於為全球移動應用開發者提供移動消息推送服務。

極光先后推出了極光即時通訊雲(JMessage)、極光短信服務(JSMS)、極光統計(JAnalytics)、極光分享(JShare)等開發者服務。  
極光IM(JMessage) 致力於幫助app解決應用內聊天及跨應用聊天問題,快速集成SDK,提供穩定可靠的APP用戶間聊天功能。
極光短信(JSMS) 為開發者提供短信下發功能,包括驗證碼類短信、通知類短信和營銷類短信三大類。
極光統計(JAnalytics) 提供整合Android、iOS的統計服務。
極光分享幫助應用具備國內主流社交平台分享功能,提供新浪微博、QQ、微信等第三方社會化分享服務,提高產品推廣效率,幫助產品提高用戶體驗,獲得更多用戶。

極光官方文檔:https://docs.jiguang.cn/

極光社區https://community.jiguang.cn/

    遇到問題,可以在這里找到相應的解決辦法。

服務端 REST API :https://docs.jiguang.cn/jpush/server/push/server_overview/

server_sdk:https://docs.jiguang.cn/jpush/server/server_sdk/

JPush REST API 的 Java 版本封裝開發包,是由極光推送官方提供的,一般支持最新的 API 功能。

對應的 REST API 文檔:[REST API - Push](https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/),

[REST API - Report](https://docs.jiguang.cn/jpush/server/push/rest_api_v3_report/).

本開發包 Javadoc:[API Docs](http://jpush.github.io/jpush-api-java-client/apidocs/)

版本更新:[Release頁面](https://github.com/jpush/jpush-api-java-client/releases)

 

極光推送測試

先注冊賬號:https://www.jiguang.cn/

登錄賬號創建應用獲取AppKey以及Master Secret

開發者服務: https://www.jiguang.cn/dev2/#/app/a76cf62afbe91d4b57f4c36e/info

 

示例代碼:

JPushUtil

package com.joey.jpush.test1;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import java.util.List;

/**
 * 極光推送工具類 https://github.com/
 * @Probject JPushUtil
 * @Create By Joey
 * @Create IN 2019-11-12
 **/
public class JPushUtil {
    /**
     * 所有平台,所有設備,內容為 content 的通知
     * @param content
     * @return
     */
    public static PushPayload buildPushObjectAllAlert(String content) {
        return PushPayload.alertAll(content);
    }
    /**
     * 根據 設備終端ID 推送消息
     * @param regesterIds 設備終端ID集合
     * @param content     內容
     * @return
     */
    public static PushPayload buildPushObjectByregesterIds(List<String> regesterIds, String content) {
        return PushPayload.newBuilder().setPlatform(Platform.all())
                .setAudience(Audience.registrationId(regesterIds))
                .setNotification(Notification.alert(content))
                .build();
    }
    /**
     * 所有平台,推送目標是別名為 "alias",通知內容為 TEST
     * @param alias
     * @param content
     * @return
     */
    public static PushPayload buildPushObjectAliasAlert(String alias, String content) {
        return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
                .setNotification(Notification.alert(content)).build();
    }
}
JpushService:
package com.joey.jpush.test1;
import java.util.List;

public interface JpushService {
    /**
     * 所有平台推送消息內容 
     * @return
     */
    public boolean pushAll();

    /**
     * 指定的設備推送消息內容
     * @param regeSterIds
     * @param msg
     * @return
     */
    public boolean pushByRegesterId(List<String> regeSterIds, String msg);
}
JpushServiceImpl:
package com.joey.jpush.test1;

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.PushPayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @Probject JpushServiceImpl
 * @Create By Joey
 * @Create IN 2019-11-12
 **/
@Service("jpushService")
public class JpushServiceImpl implements JpushService {
    //AppKey
    private final static String appKey = "a76cf62afbe91d4b57f4c36e";
    //Master Secret
    private final static String masterSecret = "34960b30f9cec35615156a4d ";
    /**
     * 保存離線的時長。秒為單位。最多支持10天(864000秒)。
     * 0 表示該消息不保存離線。
     * 即:用戶在線馬上發出,當前不在線用戶將不會收到此消息。
     * 此參數不設置則表示默認,默認為保存1天的離線消息(86400秒)。
     */
    private static long timeToLive = 60 * 60 * 24;
    private static JPushClient jPushClient = null;
    private static final Logger logger = LoggerFactory.getLogger(JpushServiceImpl.class);

    @Override
    public boolean pushAll() {
        jPushClient = new JPushClient(masterSecret, appKey, (int) timeToLive);
        boolean flag = false;
        try {
            String title = "小姐姐,再抱抱我.真的好舒服呢";

            //推送對象
            PushPayload payload = JPushUtil.buildPushObjectAllAlert(title);

            //推送結果
            PushResult result = jPushClient.sendPush(payload);
            if (null != result) {
                flag = true;
            }
        } catch (APIConnectionException e) {
            logger.error("Connection error. Should retry later. ", e);
            flag = false;
        } catch (APIRequestException e) {
            logger.error("Error response from JPush server. Should review and fix it. ", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
            logger.info("Msg ID: " + e.getMsgId());
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean pushByRegesterId(List<String> regeSterIds, String msg) {
        jPushClient = new JPushClient(masterSecret, appKey);
        boolean flag = false;
        try {
            //推送對象
            PushPayload payload = JPushUtil.buildPushObjectByregesterIds(regeSterIds, msg);

            //PushResult 表示服務端推送結果
            PushResult result = jPushClient.sendPush(payload);
            if (null != result) {
                flag = true;
            }
        } catch (APIConnectionException e) {
            logger.error("Connection error. Should retry later. ", e);
            flag = false;
        } catch (APIRequestException e) {
            logger.error("Error response from JPush server. Should review and fix it. ", e);
            logger.info("HTTP Status: " + e.getStatus());
            logger.info("Error Code: " + e.getErrorCode());
            logger.info("Error Message: " + e.getErrorMessage());
            logger.info("Msg ID: " + e.getMsgId());
            flag = false;
        }
        return flag;
    }
}

 注意:appKey和masterSecret是從你創建應用的時候,生成的。

做完測試之后,推送的原理是什么?

極光推送原理

 推送是手機中非常常見的功能了,可是在實現上iOS和Andriod卻有很大的差別。

如上圖:

1.你的IOS應用需要去注冊APNS消息推送功能。

2.當蘋果APNS推送服收到來自你應用的注冊消息就會返回一串device token給你(很重要)

3.將應用收到的device Token傳給你本地的Push服務器。

4.當你需要為應用推送消息的時候,你本地的推送服務器會將消息,以及Device Token打包發送到蘋果的APNS服

5.APNS再將消息推送給目的iphone


免責聲明!

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



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