首先,使用jpush需要先完成一下操作:
我们公司已经买过了极光推送的服务,拿过来直接用就可以了。
pom中添加依赖:
<!--极光推送start--> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jpush-client</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>cn.jpush.api</groupId> <artifactId>jiguang-common</artifactId> <version>1.0.8</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.6.Final</version> <scope>compile</scope> </dependency> <!--极光推送end-->
yml文件属性配置
jPush: #极光推送 appKey: masterSecret:
获取属性实体:
public class JPush { @Value("${jPush.appKey:''}") private String appKey; @Value("${jPush.masterSecret:''}") private String masterSecret; public String getAppKey() { return appKey; } public void setAppKey(String appKey) { this.appKey = appKey; } public String getMasterSecret() { return masterSecret; } public void setMasterSecret(String masterSecret) { this.masterSecret = masterSecret; } }
下面贴出jpush推送方式通用类,里面包含了大多数推送方法:
/** * 推送给设备标识参数的用户 * @param registrationId 设备标识 * @param notification_title 通知内容标题 * @param msg_title 消息内容标题 * @param msg_content 消息内容 * @param id 扩展字段 * @param type 扩展字段 * @return 0推送失败,1推送成功 */ public static int sendToRegistrationId( String registrationId,String notification_title, String msg_title, String msg_content, String id, String type, int days) { int result = 0; try { PushPayload pushPayload= JpushClientUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,notification_title,msg_title,msg_content,id,type,days); //System.out.println(pushPayload); PushResult pushResult=jPushClient.sendPush(pushPayload); //System.out.println(pushResult); if(pushResult.getResponseCode()==200){ result=1; } } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIRequestException e) { e.printStackTrace(); } return result; } /** * 发送给所有安卓用户 * @param notification_title 通知内容标题 * @param msg_title 消息内容标题 * @param msg_content 消息内容 * @param id 扩展字段 * @param type 扩展字段 * @return 0推送失败,1推送成功 */ public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String id, String type, int days) { int result = 0; try { PushPayload pushPayload= JpushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,id,type,days); //System.out.println(pushPayload); PushResult pushResult=jPushClient.sendPush(pushPayload); //System.out.println(pushResult); if(pushResult.getResponseCode()==200){ result=1; } } catch (Exception e) { e.printStackTrace(); } return result; } /** * 发送给所有IOS用户 * @param notification_title 通知内容标题 * @param msg_title 消息内容标题 * @param msg_content 消息内容