java整合jpush實現用戶端推送

來源:互聯網
上載者:User

標籤:html   fhe   能力   ras   null   開發   ati   解決   java   

代碼地址如下:
http://www.demodashi.com/demo/13700.html

前言

java 整合jpush 實現用戶端推送

一、準備工作

開發環境:
jdk1.6
Eclipse Luna Service Release 1 (4.4.1)
運行環境:
eclipse

二、jpush 推送說明

jpush推送是國內的服務廠商提供的一站式push服務(同時支援iOS、android),後面也加入了即時通訊的能力供app使用。致力於打造簡單、可靠、價格有競爭力的服務(簡易功能全免費,進階版才收費),讓應用開發商可以聚焦業務開發,push相關的技術實現全部通過極光推送來解決,僅需調用極光推送的api即可

三、推送原理

安卓用戶端推送原理

JPush WP Push 包括 1個部分,MPNs 推送(代理)。

紅色部分是 MPNs 推送,JPush 代理開發人員的應用,向微軟 MPNs 伺服器推送。由 Microsoft MPNs Server 推送到 WP 裝置上。

藍色部分是 JPush 應用內推送部分,但目前暫不支援應用內訊息。

蘋果用戶端推送原理

從圖可以看出,JPush iOS Push 包括 2 個部分,APNs 推送(代理),與 JPush 應用內訊息。

紅色部分是 APNs 推送,JPush 代理開發人員的應用(需要基於開發人員提供的應用認證),向蘋果 APNs 伺服器推送。由 APNs Server 推送到 iOS 裝置上。

藍色部分是 JPush 應用內推送部分,即 App 啟動時,內嵌的 JPush SDK 會開啟長串連到 JPush Server,從而 JPush Server 可以推送訊息到 App 裡。

四、代碼結構

mysql 表結構

五、服務端程式實現

1、推送基本分為安卓與蘋果。

Android("android"),IOS("ios"),WinPhone("winphone");private final String value;private DeviceType(final String value) {    this.value = value;}public String value() {    return this.value;}

2、定義推送介面推送單個使用者、多個使用者、單個裝置、整個app。

public void pushToUser(String type,String userId, PushEntity pushEntity);public void pushToUserList(String type,List<String> userIdList, PushEntity pushEntity);public void pushToDevice(String type,List<String> deviceTokenList, PushEntity pushEntity);public void pushToApp(String type,PushEntity pushEntity);

3、推送環境區分、安卓不區分開發與生產環境,蘋果需要區分。

        List<String[]> JpushInfoList = MobilePushService.getJpushKeyInfo(type,pushEntity.getJpushApiMasterSecret(),                pushEntity.getJpushAppKey());        // 如果配置mobile.notify.ios.production=false,則是開發模式        boolean iosMode = true;        // 設定平台        payloadBuilder.setPlatform(deviceType.equals(DeviceType.IOS) ? Platform.ios() : Platform.android());        Map<String, Object> extrasMap = new HashMap<String, Object>();

4、整合jpush api 實現推送功能。

            try {                JPushClient jPushClient = new JPushClient(jpushInfo[0], jpushInfo[1], iosMode,                        (pushEntity.getJpushTimeToLive() == null ? 86400 : pushEntity.getJpushTimeToLive()));                jPushClient.sendPush(pushPayload);            } catch (Exception e) {                // 個推時如果手機端沒有註冊使用者,不打錯誤記錄檔                if (e.getMessage().indexOf("\"code\": 1011") == -1) {                    logger.error("JPUSH推送訊息時發生異常:[" + e.getMessage() + "]", e);                }            }        

5、通過mysql配置jpush key與secret 動態更換配置。

        // 如果設定了自訂key,則使用自訂,否則進行資料庫查詢        if (StringUtils.isNotBlank(apiMasterSecret) && StringUtils.isNotBlank(appKey)) {            resultA.add(new String[] { apiMasterSecret, appKey });        } else {            resultList = CptNotifyJpush.dao.findCptNotifyJpush(type);            if (resultList != null && resultList.size() > 0) {                for (CptNotifyJpush result : resultList) {                    resultA.add(new String[] {result.getStr("api_master_secret"),result.getStr("app_key") });                }            }        }
六、用戶端整合步驟

step1:去極光推送註冊帳號:https://www.jpush.cn/,並註冊應用。
step2:上傳apns認證到極光,apns認證的產生步驟參考:
http://docs.jpush.io/client/ios_tutorials/#ios_1
仔細閱讀該文檔,上傳成功後控制台的應用詳情裡面會顯示“已驗證”,說明認證有效。

step3:對你的app工程重新設定,使用新的支援apns的provision檔案(若此前應用已支援apns,可以不用換),否則後面無法正常獲得device token.
step4:整合其sdk,包括一個.a和一個.h檔案,最新版本大約是2.1,其sdk中也包含了demo,注意在2.1版本之前需要建立一個plist檔案用於儲存秘鑰資訊等,在最新版本不需要此檔案。
sdk:https://www.jpush.cn/common/products#product-download

step5:主要代碼修改如下:
appdelegate.h:

static NSString appKey = @"Your_app_key";
static NSString
channel = @"Your_channel_identifier";
static BOOL isProduction = NO;

七、運行方法
public static void main(String args[]){        JPushService pushService=new JPushService();        PushEntity pushEntity=new PushEntity();    //修改密鑰    pushEntity.setJpushApiMasterSecret("0a35d2fabea1df2dfc36d32l");    pushEntity.setJpushAppKey("a344debcf41e5542b291d52f");    pushEntity.setMsgContent("test jpush");    pushEntity.setMsgTitle("test jpush");        pushService.sendNotification2App("",pushEntity, DeviceType.IOS);    } pushEntity.setJpushAppKey("a344debcf41e5542b291d52f");    pushEntity.setMsgContent("test jpush");    pushEntity.setMsgTitle("test jpush");        pushService.sendNotification2App("",pushEntity, DeviceType.IOS);java整合jpush實現用戶端推送

代碼地址如下:
http://www.demodashi.com/demo/13700.html

註:本文著作權歸作者,由demo大師代發,拒絕轉載,轉載需要作者授權

java整合jpush實現用戶端推送

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.