Java如何给极光推送发消息

一,项目背景,我是负责App端开发的,不过由于公司小人手不足,因此也要写一些后台的小程序,国内的推送平台还是比较多的,有极光、个推、信鸽等等,下面聊一下服务端如何来给极光的服务端推送消息,以达到让极光服务端正常接收并推送消息到我们自己的App的开发流程。

二、文档参考,http://docs.jiguang.cn/,这里有App端、服务端的开发文档,本文只涉及服务端的开发。服务端的文档参考http://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/。开发之前我先用Postman测通了http的请求,如下图。


认证方式HTTP Basic Authentication,如果不清楚的可以参考极光文档或者搜索引擎。填上认证的用户名和密码,接下来是请求的body。如下图,


body为一个json字符串,我这里只推送给iOS的,没有给推给Android,需要的话加上即可。extra并非必须字段。其他字段的相关意义,这里也不赘述。然后点击send,发送成功的话返回的数据如下:

测试没有问题,下面直接上程序,

这里是推送的主体

  1. {  
  2. "platform": "all",  
  3. "audience": {  
  4. "alias":["这里填上注册设备的别名,当然也可以通过tag或者registerId发送"]},  
  5. "notification": {  
  6.     "android": {  
  7.         "alert": "我是消息内容",  
  8.         "title": "peter",  
  9.         "builder_id": "1",  
  10.         "extras": {//自定义字段,可不要  
  11.             "senderId": "peter",  
  12.             "pushUrl":"http://www.baidu.com"  
  13.         }  
  14.    },  
  15.    "ios": {  
  16.             "alert": "我是消息内容",  
  17.             "title": "peter",  
  18.             "sound": "default",  
  19.             "badge": "0",  
  20.             "extras": {  
  21.                 "senderId": "peter",  
  22.                 "pushUrl":"http://www.baidu.com"  
  23.             }  
  24.         }  
  25.     },  
  26.     "options": {  
  27.         "time_to_live": 60,  
  28.         "apns_production": true   
  29.     }  
  30. }  
下面是java代码
  1. import java.io.IOException;  
  2. import java.io.UnsupportedEncodingException;  
  3.   
  4. import org.apache.http.HttpEntity;  
  5. import org.apache.http.HttpResponse;  
  6. import org.apache.http.client.ClientProtocolException;  
  7. import org.apache.http.client.HttpClient;  
  8. import org.apache.http.client.methods.HttpPost;  
  9. import org.apache.http.entity.StringEntity;  
  10. import org.apache.http.impl.client.DefaultHttpClient;  
  11. import org.apache.http.protocol.HTTP;  
  12. import org.apache.http.util.EntityUtils;  
  13.   
  14.   
  15. public class TestJpush {  
  16.       
  17.     private static String JPUSH_URL = "https://api.jpush.cn/v3/push";  
  18.     private static String JPUSH_AUTH = "这里填上你的验证信息,格式大致为Basic xxxxxxxxxxxxxxxxxxxxxxxxxxx";  
  19.   
  20.     public static void main (String args[]) {  
  21.         TestJpush test = new TestJpush();  
  22.         test.sendHttpsRequest("Json串");//这里替换为上面的json  
  23.     }  
  24.       
  25.     public void sendHttpsRequest(String param) {  
  26.         if (param == null) {  
  27.             return;  
  28.         }  
  29.         long responseLength = 0;  
  30.         String responseContent = null;  
  31.         HttpClient httpClient = new DefaultHttpClient();  
  32.         HttpPost httpPost = new HttpPost(JPUSH_URL);  
  33.           
  34.         try{  
  35.             httpPost.setHeader("Authorization", JPUSH_AUTH);  
  36.             httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");  
  37.             HttpEntity requestEntity = new StringEntity(param, "UTF-8");  
  38.             httpPost.setEntity(requestEntity);  
  39.               
  40.             HttpResponse response = httpClient.execute(httpPost);  
  41.             HttpEntity entry = response.getEntity();  
  42.             if (entry != null) {  
  43.                 responseLength = entry.getContentLength();  
  44.                 responseContent = EntityUtils.toString(entry, "UTF-8");  
  45.                 EntityUtils.consume(entry);  
  46.             }  
  47.               
  48.         } catch(ClientProtocolException e) {  
  49.             e.printStackTrace();  
  50.         } catch(UnsupportedEncodingException e) {  
  51.             e.printStackTrace();  
  52.         } catch(IOException e) {  
  53.             e.printStackTrace();  
  54.         } finally {  
  55.             httpClient.getConnectionManager().shutdown();  
  56.         }  
  57.         System.out.println("content="+responseContent);  
  58.         //推送成功返回字段  {"sendno":"0","msg_id":"4161322531"}  
  59.           
  60.     }  
  61. }  

编译,运行,如果成功的话,控制台会输出如下数据。

OK,这里简单的推送程序介绍完毕。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值