天天看點

JPush極光推送Java伺服器端API

轉載自:http://www.cnblogs.com/zhanghaoh/archive/2013/02/20/2919282.html

// 對android和ios裝置發送

JPushClient jpush =

new

JPushClient(masterSecret, appKey);

// 對android和ios裝置發送,同時指定離線消息儲存時間

JPushClient jpush =

new

JPushClient(masterSecret, appKey, timeToLive);

// 指定某種裝置發送

JPushClient jpush =

new

JPushClient(masterSecret, appKey, DeviceEnum.Android);

// 指定某種裝置發送,并且指定離線消息儲存時間

JPushClient jpush =

new

JPushClient(masterSecret, appKey, timeToLive, DeviceEnum.IOS);

參數名稱 參數類型 選項 内容說明
masterSecret      
String 必須 Portal上注冊應用時生成的 masterSecret
appKey Portal上注冊應用時生成的 appKey
timeToLive long 可選

儲存離線消息的時長。秒為機關。最多支援10天(864000秒)。

0 表示該消息不儲存離線。即:使用者線上馬上發出,目前不線上使用者将不會收到此消息。

此參數不設定則表示預設,預設為儲存1天的離線消息(86400秒)。

DeviceEnum Enum

指定的裝置。

可選值:DeviceEnum.Android, DeviceEnum.IOS。

不填或者null值為同時支援 Android 與 iOS。

發送消息

JPushClient公共方法

方法名稱 參數清單(必須) 方法說明
setEnableSSL boolean enableSSL (true為使用ssl, 預設為不使用ssl) 是否啟動ssl安全連接配接
sendNotificationWithImei

int sendNo(發送編号),

String imei (IMEI字元串) ,

String msgTitle (消息标題/通知标題) ,

String msgContent (消息内容/通知内容) 

發送帶IMEI的通知

int sendNo , 

String imei ,

String msgTitle ,

String msgContent ,

int builderId (自定義通知欄樣式Id) ,

Map<String, Object>extra (附屬資訊)

自定義通知欄(沒有則填寫0)

以及傳遞附屬資訊 

sendCustomMessageWithImei String msgContent  發送帶IMEI的消息

String msgContent, 

String msgContentType (消息内容類型,原樣傳回),

Map<String, Object> extra 

使用者自定義消息類型,
sendNotificationWithTag

String tag (Tag字元串) ,

String msgContent

發送帶Tag的通知

String tag ,

String msgContent , 

int builderId ,

Map<String, Object>extra

sendCustomMessageWithTag 發送帶Tag的消息

String msgContent ,

String msgContentType ,

sendNotificationWithAlias

String alias (Alias字元串) ,

String msgTitle , 

發送帶Alias的通知
sendCustomMessageWithAlias String alias , 發送帶Alias的消息
sendNotificationWithAppKey 發送通知給AppKey的所有使用者
sendCustomMessageWithAppKey 發送帶AppKey的消息
Map<String, Object> extra  

代碼示例

代碼示例-發送帶IMEI的通知

JPushClient jpush =

new

JPushClient(masterSecret, appKey);

//jpush.setEnableSSL(true);

int

sendNo =

1

;

String imei =

""

;

String msgTitle =

""

;

String msgContent =

""

;

MessageResult msgResult = jpush.sendNotificationWithImei(sendNo, imei, msgTitle, msgContent);

if

(

null

!= msgResult) {

if

(msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {

System.out.println(

"發送成功, sendNo="

+ msgResult.getSendno());

}

else

{

System.out.println(

"發送失敗, 錯誤代碼="

+ msgResult.getErrcode() +

", 錯誤消息="

+ msgResult.getErrmsg());

}

}

else

{

System.out.println(

"無法擷取資料"

);

}

代碼示例-IOS設定通知鈴聲和badge

JPushClient jpush =

new

JPushClient(masterSecret, appKey);

Map<String, Object> extra =

new

HashMap<String, Object>();

IOSExtra iosExtra =

new

IOSExtra(

1

,

"Windows_Logon_Sound.wav"

);

//badge and sound

extra.put(

"ios"

, iosExtra);

MessageResult msgResult = jpush.sendNotificationWithAppKey(sendNo, msgTitle, msgContent,

, extra);

MessageResult 類

公共方法 方法用途
 消息發送成功後,按用戶端傳輸的sendNo原樣傳回
 錯誤代碼,代碼定義參考ErrorCodeEnum
getErrmsg  傳回錯誤消息的描述

ErrorCode 類

錯誤代碼-ErrorCodeEnum

package

cn.jpush.api;

public

enum

ErrorCodeEnum {

//沒有錯誤,發送成功

NOERROR(

),

//系統内部錯誤

SystemError(

10

),

//不支援GET請求

NotSupportGetMethod(

1001

),

//缺少必須參數

MissingRequiredParameters(

1002

),

//參數值不合法

InvalidParameter(

1003

),

//驗證失敗

ValidateFailed(

1004

),

//消息體太大

DataTooBig(

1005

),

//IMEI不合法

InvalidIMEI(

1007

),

//appkey不合法

InvalidAppKey(

1008

),

//msg_content不合法

InvalidMsgContent(

1010

),

//沒有滿足條件的推送目标

InvalidPush(

1011

),

//IOS不支援自定義消息

CustomMessgaeNotSupportIOS(

1012

);

private

final

int

value;

private

ErrorCodeEnum(

final

int

value) {

this

.value = value;

}

public

int

value() {

return

this

.value;

}

}