該示例是使用百度雲推送給iOS和安卓Push消息,SDK版本為:Baidu-Push-SDK-Java-1.1.2;
----------------------------------iOS---------------------------------
import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
public class IosPushNotificationSample {
public static void main(String[] args) {
// 1. 設定developer平台的ApiKey/SecretKey
// 需自己注冊推送賬号
String apiKey = "eFG2TqcUzaFeejQ5z4VrgcR";
String secretKey = "yDjYrK3vcslCFsp33o9A8gaABIxhzOHi";
ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);
// 2. 建立BaiduChannelClient對象執行個體
BaiduChannelClient channelClient = new BaiduChannelClient(pair);
// 3. 若要了解互動細節,請注冊YunLogHandler類
channelClient.setChannelLogHandler(new YunLogHandler() {
@Override
public void onHandle(YunLogEvent event) {
System.out.println(event.getMessage());
}
});
try {
// 4. 建立請求類對象
// 手機端的ChannelId, 手機端的UserId, 先用1111111111111代替,使用者需替換為自己的
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
request.setDeviceType(4); // device_type => 1: web 2: pc 3:android
// 4:ios 5:wp
request.setDeployStatus(2); // DeployStatus => 1: Developer 2:
// Production
request.setChannelId(562599783353482L);
request.setUserId("62990424331551961005");
request.setMessageType(1);
request.setMessage("{\"aps\":{\"alert\":\"555\"}}");
// 5. 調用pushMessage接口
PushUnicastMessageResponse response = channelClient
.pushUnicastMessage(request);
// 6. 認證推送成功
System.out.println("push amount : " + response.getSuccessAmount());
} catch (ChannelClientException e) {
// 處理用戶端錯誤異常
e.printStackTrace();
} catch (ChannelServerException e) {
// 處理服務端錯誤異常
System.out.println(String.format(
"request_id: %d, error_code: %d, error_message: %s",
e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
}
}
}
-------------------------------Android----------------------------------
package com.baidu.yun.channel.sample;
import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushUnicastMessageRequest;
import com.baidu.yun.channel.model.PushUnicastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
public class AndroidPushNotificationSample {
public static void main(String[] args) {
// 1. 設定developer平台的ApiKey/SecretKey
// 需自己注冊推送賬号
String apiKey = "eFG2TqcUzaFeejQ5z4VrgcR";
String secretKey = "yDjYrK3vcslCFsp33o9A8gaABIxhzOHi";
ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);
// 2. 建立BaiduChannelClient對象執行個體
BaiduChannelClient channelClient = new BaiduChannelClient(pair);
// 3. 若要了解互動細節,請注冊YunLogHandler類
channelClient.setChannelLogHandler(new YunLogHandler() {
@Override
public void onHandle(YunLogEvent event) {
System.out.println(event.getMessage());
}
});
try {
// 4. 建立請求類對象
// 手機端的ChannelId, 手機端的UserId, 先用1111111111111代替,使用者需替換為自己的
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
// 4:ios 5:wp
request.setChannelId(562599783353482L);
request.setUserId("62990424331551961005");
request.setMessageType(1);
request.setMessage("{\"title\":\"測試應用\",\"description\":\"555\"}");
// 5. 調用pushMessage接口
PushUnicastMessageResponse response = channelClient
.pushUnicastMessage(request);
// 6. 認證推送成功
System.out.println("push amount : " + response.getSuccessAmount());
} catch (ChannelClientException e) {
// 處理用戶端錯誤異常
e.printStackTrace();
} catch (ChannelServerException e) {
// 處理服務端錯誤異常
System.out.println(String.format(
"request_id: %d, error_code: %d, error_message: %s",
e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
}
}
}