天天看点

百度云推送demo

1、推送需要

public static final String apiKey = "##";

public static final String secretKey = "###";

2、单播需要long channelId,String userId,指定具体的设备或者人

广播不需要

public class AndroidPushTest {

public static void main(String[] args) {

AndroidPushNotificationImpl impl = new AndroidPushNotificationImpl();

//单播

impl.pushUnicastNotification(4358942456535669255L, "1089766349723212435", "!!!!!!!!!!!!!!!!!", "@@@@@@@@@@@@@@@@@", "111", "222");

//广播

//impl.pushUnicastNotification("云推送测试", "云推送集成测试", "111", "222");

}

}

public class AndroidPushNotificationImpl implements AndroidPushNotification {

public void pushUnicastNotification(long channelId,String userId,String title,

String description, String orderId, String custId) {

ChannelKeyPair pair = new ChannelKeyPair(BaiduConstants.apiKey, BaiduConstants.secretKey);

BaiduChannelClient channelClient = new BaiduChannelClient(pair);

channelClient.setChannelLogHandler(new YunLogHandler() {

public void onHandle(YunLogEvent event) {

System.out.println(event.getMessage());

}

});

try {

PushUnicastMessageRequest request = new PushUnicastMessageRequest();

request.setDeviceType(3);//手机通道

request.setChannelId(channelId);//通道ID 此处针对不同手机在进行修改

request.setUserId(userId);//用户ID 此处针对不同手机在进行修改

request.setMessageType(1);//消息类型

StringBuilder msg = new StringBuilder();

msg.append("{");

msg.append("\"title\":\"" + title + "\",");

msg.append("\"description\":\"" + description + "\",");

msg.append("\"custom_content\":{");

msg.append("\"orderId\":\"" + orderId + "\",");

msg.append("\"custId\":\"" + custId + "\"");

msg.append("}}");

request.setMessage(msg.toString());

PushUnicastMessageResponse response = channelClient

.pushUnicastMessage(request);

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()));

}

}

public void pushUnicastNotification(String title,String description, String orderId, String custId){

ChannelKeyPair pair = new ChannelKeyPair(BaiduConstants.apiKey, BaiduConstants.secretKey);

BaiduChannelClient channelClient = new BaiduChannelClient(pair);

channelClient.setChannelLogHandler(new YunLogHandler() {

public void onHandle(YunLogEvent event) {

System.out.println(event.getMessage());

}

});

try {

PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();

// PushUnicastMessageRequest request = new PushUnicastMessageRequest();

request.setDeviceType(3);//手机通道

request.setMessageType(1);//消息类型

StringBuilder msg = new StringBuilder();

msg.append("{");

msg.append("\"title\":\"" + title + "\",");

msg.append("\"description\":\"" + description + "\",");

msg.append("\"custom_content\":{");

msg.append("\"orderId\":\"" + orderId + "\",");

msg.append("\"custId\":\"" + custId + "\"");

msg.append("}}");

request.setMessage(msg.toString());

// PushUnicastMessageResponse response = channelClient

// .pushUnicastMessage(request);

PushBroadcastMessageResponse response = channelClient

                   .pushBroadcastMessage(request);

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()));

}

}

}