百度云推送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) {
/**apiKey/secretKey申请下来之后会有固定的 不同手机推送相同的情况下直接这样不用修改*/
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()));
}


}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值