資源描述:
《百度云推送Java端》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、packagenet;importcom.baidu.yun.channel.auth.ChannelKeyPair;importcom.baidu.yun.channel.client.BaiduChannelClient;importcom.baidu.yun.channel.exception.ChannelClientException;importcom.baidu.yun.channel.exception.ChannelServerException;importcom.baidu.y
2、un.channel.model.PushBroadcastMessageRequest;importcom.baidu.yun.channel.model.PushBroadcastMessageResponse;importcom.baidu.yun.channel.model.PushUnicastMessageRequest;importcom.baidu.yun.channel.model.PushUnicastMessageResponse;importcom.baidu.yun.cor
3、e.log.YunLogEvent;importcom.baidu.yun.core.log.YunLogHandler;publicclassTestPushMessage{/***測試推送*/publicstaticvoidmain(String[]args){Stringstring="{"title":"你有新消息","description":"今天下午全體員工發(fā)獎(jiǎng)金","+""notification_builder_id":0,"notification_basi
4、c_style":2,"custom_content":{"id":"485446"},"open_type":"2"}";//Stringstring="hello";//intstatus=pushBroadcastMessage(string,3,1);intstatus=pushOnlyPleploMessage(string,3,1);System.out.println(status);}/***初始化百度推送*/privatestaticBaiduChannelC
5、lientinitPushClient(){//這兩個(gè)key自己申請StringapiKey="自己申請的apikey";StringsecretKey="自己申請的secretKey";//1.設(shè)置developer平臺的ApiKey/SecretKeyChannelKeyPairpair=newChannelKeyPair(apiKey,secretKey);//2.創(chuàng)建BaiduChannelClient對象實(shí)例BaiduChannelClientchannelClient=newBaiduC
6、hannelClient(pair);//3.若要了解交互細(xì)節(jié),請注冊YunLogHandler類channelClient.setChannelLogHandler(newYunLogHandler(){@OverridepublicvoidonHandle(YunLogEventevent){System.out.println(event.getMessage());}});returnchannelClient;}/***用百度推送向所有人發(fā)送消息或者通知*/publicstaticintp
7、ushBroadcastMessage(StringContent,intdevType,intpushType){BaiduChannelClientchannelClient=initPushClient();try{//4.創(chuàng)建請求類對象PushBroadcastMessageRequestrequest=newPushBroadcastMessageRequest();//devType=>1:web2:pc3:android4:ios5:wprequest.setDeviceType(de
8、vType);//pushType為0設(shè)置發(fā)送消息,為1設(shè)置發(fā)送通知request.setMessageType(pushType);request.setMessage(Content);//5.調(diào)用pushMessage接口PushBroadcastMessageResponseresponse=channelClient.pushBroadcastMessage(request);//6.認(rèn)證推送成功System.out.println("認(rèn)證推送成功pusha