極光推送是:使得開發者可以即時地向其應用程序的用戶推送通知或者消息,與用戶保持互動,從而有效地提高留存率,提升用戶體驗。簡單的說就是通過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下載下來研究研究。
歡迎打擾各位程序猿打擾交流