jpush(极光)java服务端异步推送消息

public class JpushServiceImpl implements JpushService {

    @Override
    @Async
    public void pushMsg(Entity entity) throws Exception{
        String AppKey ="123dggdsfd4567fhsshshdsf";//极光创建的appkey
        String MasterSecret="dsag121233dfdx";//密码

        Map jpushparam = new HashMap();
        jpushparam.put("registrationid",userinfo.getRegistrationid());
        jpushparam.put("msg","通知公告-"+messtitle());
        jpushparam.put("title","通知公告-"+messtitle());
        jpushparam.put("msgtype","3");//可以自定义
        jpushparam.put("msgjson", JSON.toJSONStringWithDateFormat(entity,"yyyy-MM-dd"));//可自定义内容
        jpushparam.put("msgurl","");    
    
        JpushUtil.jpushAll(appkey,secret,jpushparam);//安卓、ios通用的推送方法

     }
}

 

public class JpushUtil {

    //极光推送>>All所有平台
    public static void jpushAll(String APP_KEY,String MASTER_SECRET,Map parm) {

        //创建JPushClient
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //创建option
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())  //所有平台的用户
                .setAudience(Audience.registrationId(parm.get("registrationid")))//registrationId指定用户
                //.setAudience(Audience.registrationId("100d855909206b94558"))//registrationId指定用户
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder() //发送ios
                                .setAlert(parm.get("msg")) //消息体
                                .setBadge(+1)
                                .setSound("happy") //ios提示音
                                .addExtras(parm) //附加参数
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder() //发送android
                                .addExtras(parm) //附加参数
                                .setAlert(parm.get("msg")) //消息体
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境 true为生产模式 false        为测试模式 (android不区分模式,ios区分模式)               .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).setTitle("1234567").addExtras(parm).build()).build();//自定义信息
                
        try {
            PushResult pu = jpushClient.sendPush(payload);
            System.out.println("成功了");
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }

    }

}

需要实现异步,需要在springboot的启动类加上@EnableAsync注解,并且方法要写在service层中,且service方法加@Async注解

你可能感兴趣的:(工具类,java,java,app,spring,boot)