1 packagecom.uxun.serviceImpl;2
3 importcom.uxun.service.JPushService;4
5 importcn.jiguang.common.resp.APIConnectionException;6 importcn.jiguang.common.resp.APIRequestException;7 importcn.jpush.api.JPushClient;8 importcn.jpush.api.push.PushResult;9 importcn.jpush.api.push.model.Platform;10 importcn.jpush.api.push.model.PushPayload;11 importcn.jpush.api.push.model.audience.Audience;12 importcn.jpush.api.push.model.notification.AndroidNotification;13 importcn.jpush.api.push.model.notification.IosNotification;14 importcn.jpush.api.push.model.notification.Notification;15
16 public class JPushServiceImpl implementsJPushService {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 @Override25 public intsendToRegistrationId(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 returnresult;44 }45
46 @Override47 public intsendToAll(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 returnresult;65 }66
67 @Override68 public intsendToAllIos(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 returnresult;87 }88
89 @Override90 public intsendToAllAndroid(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 returnresult;109 }110
111 /**
112 * 建立以唯一设备标识符推送的对象113 *@paramregistrationId 唯一设备标识114 *@paramnotification_alert 通知内容115 *@paramnotification_title 通知标题116 *@paramextrasparam 扩展字段117 *@return返回推送对象118 */
119 private staticPushPayload buildPushObjectWithRegistrationId(String registrationId, String notification_alert, String notification_title,120 String extrasparam) {121 returnPushPayload.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 //直接传alert141 //此项是指定此推送的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-notification149 //取消此注释,消息推送时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 *@paramnotification_alert 通知内容165 *@paramnotification_title 通知标题166 *@paramextrasparam 扩展字段167 *@return返回推送对象168 */
169 private staticPushPayload buildPushObjectWithAll(String notification_alert,170 String notification_title, String extrasparam) {171 returnPushPayload.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 //直接传alert191 //此项是指定此推送的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-notification199 //取消此注释,消息推送时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 *@paramnotification_alert 通知内容215 *@paramnotification_title 通知标题216 *@paramextrasparam 扩展字段217 *@return返回推送对象218 */
219 private staticPushPayload buildPushObjectWithIos(String notification_alert,220 String notification_title, String extrasparam) {221 returnPushPayload.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 //直接传alert234 //此项是指定此推送的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-notification242 //取消此注释,消息推送时ios将无法在锁屏情况接收243 //.setContentAvailable(true)
244 .build())245 .build())246 .build();247 }248
249 /**
250 * 建立推送所有安卓用户的推送对象251 *@paramnotification_alert 通知内容252 *@paramnotification_title 通知标题253 *@paramextrasparam 扩展字段254 *@return返回推送对象255 */
256 private staticPushPayload buildPushObjectWithAndroid(String notification_alert,257 String notification_title, String extrasparam) {258 returnPushPayload.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 }