web项目jpush配置

来源:互联网 发布:精易编程助手生成模块 编辑:程序博客网 时间:2024/06/05 11:03

1.在配置文件中配置参数

jpush.aunt.AppKey=a3935ef8a7744dee7b91e9f8jpush.aunt.MasterSecret=1fd647c7b0f73de17bcfba02jpush.apns.Production=true

2.java调用jpush

构造推送对象

public class JpushBeanDto implements Serializable {    private static final long   serialVersionUID = 2017475296277514323L;    private Long                messageId;                                 // 订单ID    private String              type;                                      // 通知类型    private String              title;                                     // 通知标题    private String              alia;                                      // 别名标记    private Set<String>         tagValues = new HashSet<String>();         // 标签标记    private Map<String, String> extras    = new HashMap<String, String>(); // 推送的数据信息,用于前后台交互    private String              alert;                                     // 推送消息的内容

转发层调用业务层

 // 推送阿姨端jpush消息    @Scheduled(cron = "0/5 * *  * * ?")    public void pushAuntMessage() {        jpushService.pushAuntMessage();    }

业务层注入参数

    @Value("${jpush.aunt.AppKey}")    private String                      auntAppKey;    @Value("${jpush.aunt.MasterSecret}")    private String                      auntAppSecret;    @Value("${jpush.apns.Production}")    private String                      isProduction;    private JPushClient                 auntJpushClient;

构造jpush bean

    private JpushBeanDto convertMessage2JpushBean(MessageEntity message) {        String type = message.getType();        JpushBeanDto jpushBeanDto = new JpushBeanDto();        jpushBeanDto.setMessageId(message.getId());        jpushBeanDto.setAlia("employee_new_" + message.getObjectId().toString());        jpushBeanDto.setType(type);        jpushBeanDto.setTitle(message.getTitle());        jpushBeanDto.setAlert(message.getContent());        // 设置交互信息        jpushBeanDto.getExtras().put("messageId", jpushBeanDto.getMessageId().toString());        jpushBeanDto.getExtras().put("type", type);        if (MessageTypeEnum.AUNT_BILL_MESSAGE.getKey().equals(type)) {            jpushBeanDto.getExtras().put("orderId", parseString2Map(message.getParams()).get("orderId"));        }        return jpushBeanDto;            }

构造 pushPayload对象

  pushPayload = pushAuntNotificationByTag(jpushBean);  .  .  .     private PushPayload pushAuntNotificationByTag(JpushBeanDto jpushBean) {        Platform platform = Platform.all();        Audience audience = Audience.tag(jpushBean.getTagValues());        Notification notification = Notification.newBuilder().setAlert(jpushBean.getAlert())                .addPlatformNotification(AndroidNotification.newBuilder().setTitle(jpushBean.getTitle()).addExtras(jpushBean.getExtras()).build()).build();        Options options = Options.newBuilder().setApnsProduction(Boolean.valueOf(isProduction)).build();        return buildPushObject(platform, audience, notification, null, options);    }

构造JpushClient

    private JPushClient getAuntJPushClient() {        if (auntJpushClient == null) {            synchronized (JpushService.class) {                if (auntJpushClient == null) {                    auntJpushClient = new JPushClient(auntAppSecret, auntAppKey);                }            }        }        return auntJpushClient;    }

推送信息

 auntJpushClient.sendPush(pushPayload);
0 0