码迷,mamicode.com
首页 > 其他好文 > 详细

关于极光推送Jpush的demo

时间:2017-06-02 23:07:30      阅读:593      评论:0      收藏:0      [点我收藏+]

标签:bsp   static   mod   程序   9.png   tcp   列表   ==   配置文件   

关于极光推送Jpush

  推送是手机app必不可少的一样功能,这次由于公司项目需要研究了一下。由于推送一般写于服务端,所以对于不会Android的javaweb程序员要写出一个完整的demo是一件很头痛的事情。所以我就在这里从头到尾写一个例子以示参考。由于我也不懂Android 只是由于项目需要百度了一个demo,当中有很多不足的地方忘各位大神指正。

一、首先先简单的介绍一下什么是极光推送

    ①为什么需要推送:为了解决数据同步的问题,在手机平台上,常用的方法有2种。一种是定时去服务器上查询数据,也叫Polling,还有一种手机跟服务器之间维护一个 TCP 长连接,当服务器有数据时,实时推送到客户端,也就是我们说的 Push。由于做推送在移动网络下维持长连接有一定的技术难度,而且当用户量上去的时候,需要很多服务器来维持长连接,成本也是很大的。因此我们需要一个第三方的jar包来支持我们的推送服务。如果想了解更多内容可参看极光推送官方文档。

    ②极光推送的通俗解答:就是把需要推送的内容通过调用jpush的api的发送到极光推送的服务器上,极光推送的服务器根据我们推送过去的内容选择推送到具体的app。

 

二、源码

服务端代码:http://pan.baidu.com/s/1bpH55uR

安卓客户端代码:http://pan.baidu.com/s/1jIqmN7S

安卓环境为:ADT+SDK+eclipse

ADT下载:http://pan.baidu.com/s/1dFmZlZF

SDK下载:http://pan.baidu.com/s/1kU53AUr

所需JDK的环境安装就不一一叙述了。

 注:代码可直接导入只需要改几个地方后面会阐述。首先教大家搭下安卓环境先保证代码能导入成功

三、操作步骤

①  安装插件

技术分享

 

 技术分享

技术分享

 

 ②安装sdk

技术分享

技术分享

技术分享

此步骤完成后页面会切换到Android的开发界面,页面会多两个东西

技术分享

选择你要安装的一般选择Tools里面的和Android你所需要的版本,然后带你安装,我这里已经没有选,所以示灰色的。

 

技术分享

注:关于安装很慢或者安装失败

有两种方法:

①  使用FQ软件  我用的蓝灯

②  使用配置镜像服务器  如图:

技术分享

技术分享

 

 ③然后导入安卓工程(服务端的工程就不叙述怎么导入了)

 在工程列表的空白处右键--选择import--然后操作如图

技术分享

 

 然后是运行安卓程序(最好是插上数据线--打开手机的usb调试--然后程序运行的时候回下到手机上)

具体的可以百度下。

 

四、极光推送的使用(肯定要到官网注册一个账号)

具体使用请百度,或者参照官网,下面是主要截图

技术分享

appkey和master secret 后面会用到

五、修改Android配置文件,将appkey改成你所添加的应用的appkey

技术分享

 

六、服务端代码展示,代码均有注释就不一一解释了

只用的到这三个代码

APPKET和MASTERSECRET 改成你在极光开发者服务添加的应用时所生产的字符串

技术分享

 

技术分享
  1  package com.uxun.serviceImpl;
  2 
  3 import com.uxun.service.JPushService;
  4 
  5 import cn.jiguang.common.resp.APIConnectionException;
  6 import cn.jiguang.common.resp.APIRequestException;
  7 import cn.jpush.api.JPushClient;
  8 import cn.jpush.api.push.PushResult;
  9 import cn.jpush.api.push.model.Platform;
 10 import cn.jpush.api.push.model.PushPayload;
 11 import cn.jpush.api.push.model.audience.Audience;
 12 import cn.jpush.api.push.model.notification.AndroidNotification;
 13 import cn.jpush.api.push.model.notification.IosNotification;
 14 import cn.jpush.api.push.model.notification.Notification;
 15 
 16 public class JPushServiceImpl implements JPushService {
 17 
 18     private final static String APPKET = "44262636e2afd75d9b9f7932";
 19     
 20     private final static String  MASTERSECRET = "ae5c0ab5f093b2aba1f8ce25";
 21  
 22     private static JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKET);//通知默认保留24小时。
 23     
 24     @Override
 25     public int sendToRegistrationId(String registrationId, String notification_alert, String notification_title,
 26             String extrasparam) {
 27         int result = 0;
 28         try {
 29             PushPayload pushPayload= JPushServiceImpl.buildPushObjectWithRegistrationId(registrationId,
 30                     notification_alert, notification_title, extrasparam);
 31             System.out.println(pushPayload);
 32             PushResult pushResult=jPushClient.sendPush(pushPayload);  //发送推送对象
 33             //System.out.println(pushResult);
 34             if(pushResult.getResponseCode() == 200) {  //状态码等于200 为成功
 35                 result=1;
 36             }
 37         } catch (APIConnectionException e) {
 38             e.printStackTrace();
 39         } catch (APIRequestException e) {
 40             e.printStackTrace();
 41         }
 42         
 43          return result;
 44     }
 45 
 46     @Override
 47     public int sendToAll(String notification_alert, String notification_title, String extrasparam) {
 48         int result = 0;
 49         try {
 50             PushPayload pushPayload= JPushServiceImpl.buildPushObjectWithAll(notification_alert,
 51                     notification_title, extrasparam);
 52             System.out.println(pushPayload);
 53             PushResult pushResult=jPushClient.sendPush(pushPayload);  //发送推送对象
 54             //System.out.println(pushResult);
 55             if(pushResult.getResponseCode() == 200) {  //状态码等于200 为成功
 56                 result=1;
 57             }
 58         } catch (APIConnectionException e) {
 59             e.printStackTrace();
 60         } catch (APIRequestException e) {
 61             e.printStackTrace();
 62         }
 63         
 64          return result;
 65     }
 66     
 67     @Override
 68     public int sendToAllIos(String notification_alert, String notification_title, String extrasparam) {
 69         
 70         int result = 0;
 71         try {
 72             PushPayload pushPayload= JPushServiceImpl.buildPushObjectWithIos(notification_alert,
 73                     notification_title, extrasparam);
 74             System.out.println(pushPayload);
 75             PushResult pushResult=jPushClient.sendPush(pushPayload);  //发送推送对象
 76             //System.out.println(pushResult);
 77             if(pushResult.getResponseCode() == 200) {  //状态码等于200 为成功
 78                 result=1;
 79             }
 80         } catch (APIConnectionException e) {
 81             e.printStackTrace();
 82         } catch (APIRequestException e) {
 83             e.printStackTrace();
 84         }
 85         
 86          return result;
 87     }
 88 
 89     @Override
 90     public int sendToAllAndroid(String notification_alert, String notification_title, String extrasparam) {
 91         
 92         int result = 0;
 93         try {
 94             PushPayload pushPayload= JPushServiceImpl.buildPushObjectWithAndroid(notification_alert,
 95                     notification_title, extrasparam);
 96             System.out.println(pushPayload);
 97             PushResult pushResult=jPushClient.sendPush(pushPayload);  //发送推送对象
 98             //System.out.println(pushResult);
 99             if(pushResult.getResponseCode() == 200) {  //状态码等于200 为成功
100                 result=1;
101             }
102         } catch (APIConnectionException e) {
103             e.printStackTrace();
104         } catch (APIRequestException e) {
105             e.printStackTrace();
106         }
107         
108          return result;
109     }
110 
111     /**
112      * 建立以唯一设备标识符推送的对象
113      * @param registrationId  唯一设备标识
114      * @param notification_alert  通知内容
115      * @param notification_title  通知标题
116      * @param extrasparam  扩展字段
117      * @return  返回推送对象
118      */
119     private static PushPayload buildPushObjectWithRegistrationId(String registrationId, String notification_alert, String notification_title,
120             String extrasparam) {
121         return PushPayload.newBuilder()
122                 //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
123                 .setPlatform(Platform.all())
124                 //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
125                 .setAudience(Audience.registrationId(registrationId))
126                 //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
127                 .setNotification(Notification.newBuilder()
128                 //指定当前推送的android通知
129                 .addPlatformNotification(AndroidNotification.newBuilder()
130                     .setAlert(notification_alert)   //设置通知内容(必填)
131                     .setTitle(notification_title)    //设置通知标题(可选)
132                     //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
133                     .addExtra("androidNotification extras key",extrasparam)
134                     .build())
135                     
136                 //指定当前推送的iOS通知
137                 .addPlatformNotification(IosNotification.newBuilder()
138                     //传一个IosAlert对象,指定apns title、title、subtitle等
139                     .setAlert(notification_alert)
140                     //直接传alert
141                     //此项是指定此推送的badge(应用角标)自动加1
142                     .incrBadge(1)
143                     //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
144                     // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
145                     .setSound("sound.caf")
146                     //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
147                     .addExtra("iosNotification extras key",extrasparam)
148                     //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
149                     //取消此注释,消息推送时ios将无法在锁屏情况接收
150                     // .setContentAvailable(true)
151                     .build())
152                 
153                 //指定当前推送的winPhone通知
154                 /*.addPlatformNotification(WinphoneNotification.newBuilder()
155                       .setAlert(notification_alert)
156                       //.setTitle(""))  //设置通知标题(可选)此标题将取代显示app名称的地方
157                     .build())*/
158                     .build())
159                     .build();
160     }
161     
162     /**
163      * 建立推送所有用户的推送对象
164      * @param notification_alert  通知内容
165      * @param notification_title  通知标题
166      * @param extrasparam  扩展字段
167      * @return  返回推送对象
168      */
169     private static PushPayload buildPushObjectWithAll(String notification_alert,
170             String notification_title, String extrasparam) {
171         return PushPayload.newBuilder()
172                 //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
173                 .setPlatform(Platform.all())
174                 //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
175                 .setAudience(Audience.all())
176                 //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
177                 .setNotification(Notification.newBuilder()
178                 //指定当前推送的android通知
179                 .addPlatformNotification(AndroidNotification.newBuilder()
180                     .setAlert(notification_alert)   //设置通知内容(必填)
181                     .setTitle(notification_title)    //设置通知标题(可选)
182                     //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
183                     .addExtra("androidNotification extras key",extrasparam)
184                     .build())
185                     
186                 //指定当前推送的iOS通知
187                 .addPlatformNotification(IosNotification.newBuilder()
188                     //传一个IosAlert对象,指定apns title、title、subtitle等
189                     .setAlert(notification_alert)
190                     //直接传alert
191                     //此项是指定此推送的badge(应用角标)自动加1
192                     .incrBadge(1)
193                     //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
194                     // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
195                     .setSound("sound.caf")
196                     //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
197                     .addExtra("iosNotification extras key",extrasparam)
198                     //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
199                     //取消此注释,消息推送时ios将无法在锁屏情况接收
200                     // .setContentAvailable(true)
201                     .build())
202                 
203                 //指定当前推送的winPhone通知
204                 /*.addPlatformNotification(WinphoneNotification.newBuilder()
205                       .setAlert(notification_alert)
206                       //.setTitle(""))  //设置通知标题(可选)此标题将取代显示app名称的地方
207                     .build())*/
208                     .build())
209                     .build();
210     }
211     
212     /**
213      * 建立推送所有ios用户的推送对象
214      * @param notification_alert  通知内容
215      * @param notification_title  通知标题
216      * @param extrasparam  扩展字段
217      * @return  返回推送对象
218      */
219     private static PushPayload buildPushObjectWithIos(String notification_alert,
220             String notification_title, String extrasparam) {
221         return PushPayload.newBuilder()
222                 //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
223                 .setPlatform(Platform.ios())
224                 //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
225                 .setAudience(Audience.all())
226                 //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
227                 .setNotification(Notification.newBuilder()
228                     
229                 //指定当前推送的iOS通知
230                 .addPlatformNotification(IosNotification.newBuilder()
231                     //传一个IosAlert对象,指定apns title、title、subtitle等
232                     .setAlert(notification_alert)
233                     //直接传alert
234                     //此项是指定此推送的badge(应用角标)自动加1
235                     .incrBadge(1)
236                     //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
237                     // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
238                     .setSound("sound.caf")
239                     //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
240                     .addExtra("iosNotification extras key",extrasparam)
241                     //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
242                     //取消此注释,消息推送时ios将无法在锁屏情况接收
243                     // .setContentAvailable(true)
244                     .build())
245                     .build())
246                     .build();
247     }
248     
249     /**
250      * 建立推送所有安卓用户的推送对象
251      * @param notification_alert  通知内容
252      * @param notification_title  通知标题
253      * @param extrasparam  扩展字段
254      * @return  返回推送对象
255      */
256     private static PushPayload buildPushObjectWithAndroid(String notification_alert,
257             String notification_title, String extrasparam) {
258         return PushPayload.newBuilder()
259                 //指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
260                 .setPlatform(Platform.android())
261                 //指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
262                 .setAudience(Audience.all())
263                 //jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
264                 .setNotification(Notification.newBuilder()
265                     //指定当前推送的android通知
266                     .addPlatformNotification(AndroidNotification.newBuilder()
267                         .setAlert(notification_alert)   //设置通知内容(必填)
268                         .setTitle(notification_title)    //设置通知标题(可选)
269                         //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
270                         .addExtra("androidNotification extras key",extrasparam)
271                         .build())
272                         .build())
273                         .build();
274     }
275 
276     
277 
278 }
View Code

 

 运行服务端代码后手机会受到如下通知:

其实就是服务端通过唯一识别的appkey和master secret将通知发送到极光服务器,服务器再转发至对应的app(上面app的配置文件里也配置了appkey

技术分享

 

 具体操作就是这么多,可能不是很详细,但是操作步骤实在太多,就不一一截图了,这里只给大部分的具体方向。

 

关于极光推送Jpush的demo

标签:bsp   static   mod   程序   9.png   tcp   列表   ==   配置文件   

原文地址:http://www.cnblogs.com/chenfei-java/p/6935342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!