天天看點

JPush極光推送 Java調用伺服器端API開發

   極光推送是:使得開發者可以即時地向其應用程式的使用者推送通知或者消息,與使用者保持互動,進而有效地提高留存率,提升使用者體驗。簡單的說就是通過JPush背景管理網站進行app消息的推送。可以讓使用者及時的收到最新的消息提示。

       但是往往有時候需要我們自己開發自己的背景管理網站實作推送的功能,這個時候就需要調用JPush提供的API接口,來進行消息的推送。這裡我隻講一些核心API接口,用戶端的網站上有例子大家可以自己下載下傳下來看看。

下面是java背景的代碼部分:

public class JPushClientExample {  
        //在極光注冊上傳應用的 appKey 和 masterSecret  
        private static final String appKey ="a148767f7440ff9daf56457f";////必填,例如466f7032ac604e02fb7bda89  
        private static final String masterSecret = "731e374afd796d5942ba1363";//必填,每個應用都對應一個masterSecret  
        private static JPushClient jpush = null;  
        /* 
         * 儲存離線的時長。秒為機關。最多支援10天(864000秒)。 
         * 0 表示該消息不儲存離線。即:使用者線上馬上發出,目前不線上使用者将不會收到此消息。 
         * 此參數不設定則表示預設,預設為儲存1天的離線消息(86400秒 
         */  
        private static long timeToLive =  60 * 60 * 24;    
        public static void main(String[] args) {  
            /* 
             * Example1: 初始化,預設發送給android和ios,同時設定離線消息存活時間 
             * jpush = new JPushClient(masterSecret, appKey, timeToLive); 
             *  
             * Example2: 隻發送給android         *  
             * Example3: 隻發送給IOS 
             * jpush = new JPushClient(masterSecret, appKey, DeviceEnum.IOS); 
             *  
             * Example4: 隻發送給android,同時設定離線消息存活時間 
             * jpush = new JPushClient(masterSecret, appKey, timeToLive, DeviceEnum.Android); 
             */  
            jpush = new JPushClient(masterSecret, appKey, timeToLive);  
            /*  
             * 是否啟用ssl安全連接配接, 可選 
             * 參數:啟用true, 禁用false,預設為非ssl連接配接 
             */  
            jpush.setEnableSSL(true);  
      
            //測試發送消息或者通知  
            testSend();  
        }  
        private static void testSend() {  
            // 在實際業務中,建議 sendNo 是一個你自己的業務可以處理的一個自增數字。  
            // 除非需要覆寫,請確定不要重複使用。詳情請參考 API 文檔相關說明。  
    //      Integer num= getRandomSendNo();  
            String sendNo="1900192560";  
            String msgTitle = "JPush測試資訊";  
            String msgContent = "我是JPush測試資訊,已經成功發送給你,請查收。";      
            /* 
             * IOS裝置擴充參數, 
             * 設定badge,設定聲音 
             */  
            Map<String, Object> extra = new HashMap<String, Object>();  
            IOSExtra iosExtra = new IOSExtra(1, "WindowsLogonSound.wav");  
            extra.put("id1",iosExtra);  
            extra.put("id2","I am extra infomation");  
            //IOS和安卓一起  
            MessageResult msgResult = jpush.sendNotificationWithAppKey(sendNo, msgTitle, msgContent, 0, extra);  
            //對所有使用者發送通知, 更多方法請參考文檔  
        //  MessageResult msgResult = jpush.sendCustomMessageWithAppKey(sendNo,msgTitle, msgContent);  
            if (null != msgResult) {  
                System.out.println("伺服器傳回資料: " + msgResult.toString());  
                if (msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {  
                    System.out.println("發送成功, sendNo=" + msgResult.getSendno());  
                } else {  
                    System.out.println("發送失敗, 錯誤代碼=" + msgResult.getErrcode() + ", 錯誤消息=" + msgResult.getErrmsg());  
                }  
            } else {  
                System.out.println("無法擷取資料");  
            }     
        }  


        public static final int MAX = Integer.MAX_VALUE;  
        public static final int MIN = (int) MAX/2;  
        /** 
         * 保持 sendNo 的唯一性是有必要的 
         * It is very important to keep sendNo unique. 
         * @return sendNo 
         */  
        public static int getRandomSendNo() {  
            return (int) (MIN + Math.random() * (MAX - MIN));  
        }  
    }        
//發送自定義消息
    private static PushPayload push_Android(String message) {  
        Map<String,String> map = new HashMap<>();
        map.put("測試", "測試1");
        Builder msg = Message.newBuilder();
        msg.setTitle("衡雲title");
        msg.setMsgContent("衡雲content");
        msg.setContentType("type:1");
        msg.addExtra("type", 1);
//        Message.newBuilder();
        return PushPayload.newBuilder()  
                .setPlatform(Platform.android())  
                .setAudience(Audience.tag("200000249hykj"))  
                .setMessage(msg.build())
                .build();  
    }      

開發者可以自己定義發送的标題,内容,附加資訊,離線等待時間等消息。用起來特别友善。這裡我就添加這個demo核心的代碼。有興趣的可以把整個demo下載下傳下來研究研究。

歡迎打擾各位程式猿打擾交流