Java服务端调用百度推送接口推送消息

参考 Java服务端向苹果APNS推送消息, 请关注之前的文章 :http://blog.csdn.net/qq_37022150/article/details/73991913


不废话,请直接看代码,向所有设备推送消息,注释非常的详细:

public class AndroidPushMsgToAll {
	public static void main(String[] args) 
			throws PushClientException,PushServerException {
		// 1. get apiKey and secretKey from developer console
                //  从百度推送开发者控制台获取apiKey与 secretKey;
                String apiKey = "WKqKYQyAhOLg2L4QlcwiWbAg";
	        String secretKey = "gSdkkKDkP397up4Kfmp7HGCuL7RoimQv";
                //  创建PushKeyPair对象,使用到apiKey与secretKey;
               PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

		// 2. build a BaidupushClient object to access released interfaces
                // 创建 BaiduPushClient 对象,使用到PushKeyPair对象,与接口;              
                BaiduPushClient pushClient = new BaiduPushClient(pair,
				BaiduPushConstants.CHANNEL_REST_URL);

		// 3. register a YunLogHandler to get detail interacting information
		// in this request.
                // 书写日志文件
              pushClient.setChannelLogHandler(new YunLogHandler() {
			@Override
			public void onHandle(YunLogEvent event) {
				System.out.println(event.getMessage());
			}
		});

		try {
			
			// 4. specify request arguments
			//创建 Android的通知
			JSONObject notification = new JSONObject();
			notification.put("title", "xxxxxxxx");
			notification.put("description","xxxxxxx");
			notification.put("notification_builder_id", 0);
			notification.put("notification_basic_style",7);
			notification.put("open_type", 2);
			notification.put("pkg_content", null);
			JSONObject jsonCustormCont = new JSONObject();
			jsonCustormCont.put("key", "value"); //自定义内容,key-value
			notification.put("custom_content", jsonCustormCont);
			
			// 4. specify request arguments
                        // 具体的请求参数
                       PushMsgToAllRequest request = new PushMsgToAllRequest()
					.addMsgExpires(new Integer(3600)).addMessageType(1)
					// 透传
//					.addMsgExpires(new Integer(3600)).addMessageType(0)
					.addMessage(notification.toString()) //添加透传消息
//					.addSendTime(System.currentTimeMillis() / 1000 + 120) // 设置定时推送时间,必需超过当前时间一分钟,单位秒.实例2分钟后推送
					.addDeviceType(3);
			// 5. http request
			PushMsgToAllResponse response = pushClient.pushMsgToAll(request);
			// Http请求结果解析打印
			System.out.println("msgId: " + response.getMsgId() + ",sendTime: "
					+ response.getSendTime() + ",timerId: "
					+ response.getTimerId());
		} catch (PushClientException e) {
			if (BaiduPushConstants.ERROROPTTYPE) {
				throw e;
			} else {
				e.printStackTrace();
			}
		} catch (PushServerException e) {
			if (BaiduPushConstants.ERROROPTTYPE) {
				throw e;
			} else {
				System.out.println(String.format(
						"requestId: %d, errorCode: %d, errorMessage: %s",
						e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
			}
		}
	}
}

如果有任何疑问,可以留言,关于单个设备推送,单个设备批量推送,标签推送,标签组合推等接口,如有需要请到该链接下载:http://download.csdn.net/my


更多文章,请关注:http://blog.csdn.net/qq_37022150?viewmode=list


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Boot是一个开发微服务的框架,MQTT(Message Queuing Telemetry Transport)是一种轻量级的消息传输协议。在Spring Boot中,可以通过使用MQTT协议来实现服务端消息推送和订阅。 首先,在Spring Boot项目中引入相应的MQTT依赖,比如paho-mqtt-client。然后,需要配置MQTT的连接参数,包括服务器地址、端口、客户端ID等。可以通过在application.properties文件中进行配置,或通过编写Java代码进行配置。 接下来,创建一个MQTT服务端对象,并设置相应的回调函数。回调函数是在特定事件发生时触发的代码,比如连接成功、收到消息等。可以在回调函数中编写处理逻辑,比如将消息推送给订阅者。 在服务端需要推送消息时,可以调用MQTT服务端对象的publish方法,将消息发送给特定的主题。订阅者可以通过订阅相应的主题来接收消息服务端发布消息后,订阅了该主题的客户端会收到相应的消息。 为了演示服务端推送消息的功能,可以创建一个Spring Boot的控制器,并在其中编写相应的逻辑。比如,可以创建一个/publish的接口,接收消息并将其发布给指定的主题。 需要注意的是,为了保证消息的可靠性,可以在服务端实现一些机制,比如消息的持久化、重发等。另外,还可以对消息进行加密和认证,以确保消息的安全性。 总结来说,通过使用Spring Boot和MQTT协议,可以实现服务端消息推送和订阅功能。这对于实时通信、消息传递、数据同步等场景非常有用。希望以上回答能帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值