百度云推送服务端SDK怎么用
1个回答
推荐于2016-12-02
展开全部
package test;
import org.junit.Test;
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 BaiduNSTest {
@Test
public void testPushUnicastAndroidNotification() {
/*
* @brief 向Android端设备推送单播消息
* message_type = 1
* device_type = 3
*/
// 1. 设置developer平台的ApiKey/SecretKey
String apiKey = "xxxx";
String secretKey = "xxxxx";
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. 创建请求类对象
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
request.setDeviceType(3);
//通过Android客户端获取channelId、UserId
request.setChannelId(4153470738761065185L);
request.setUserId("945673997787368997");
request.setMessageType(1);
request.setMessage("{\"title\":\"Notify_title_danbo\",\"description\":\"Notify_description_content\",\"notification_basic_style\":2}");
// 5. 调用pushMessage接口
PushUnicastMessageResponse response = channelClient
.pushUnicastMessage(request);
// Assert.assertEquals(1, 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()));
}
}
}
import org.junit.Test;
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 BaiduNSTest {
@Test
public void testPushUnicastAndroidNotification() {
/*
* @brief 向Android端设备推送单播消息
* message_type = 1
* device_type = 3
*/
// 1. 设置developer平台的ApiKey/SecretKey
String apiKey = "xxxx";
String secretKey = "xxxxx";
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. 创建请求类对象
PushUnicastMessageRequest request = new PushUnicastMessageRequest();
request.setDeviceType(3);
//通过Android客户端获取channelId、UserId
request.setChannelId(4153470738761065185L);
request.setUserId("945673997787368997");
request.setMessageType(1);
request.setMessage("{\"title\":\"Notify_title_danbo\",\"description\":\"Notify_description_content\",\"notification_basic_style\":2}");
// 5. 调用pushMessage接口
PushUnicastMessageResponse response = channelClient
.pushUnicastMessage(request);
// Assert.assertEquals(1, 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()));
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
蓝色航线
2024-12-05 广告
2024-12-05 广告
腾讯云合作伙伴-资深代理商-新买续费优惠促销中,腾讯云合作伙伴-蓝色航线科技,新买续费均优惠, 提供云架构方案,助您优惠上云.优惠提供云产品/云数据库等产品, 技术咨询服务,大客户可信赖的腾讯云合作伙伴...
点击进入详情页
本回答由蓝色航线提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询