极光推送实例

2019-11-07 19:26:34 浏览数 (1)

极光网址:https://www.jiguang.cn/

需求:先需要给手机推送一个消息,或者一个连接,点击消息唤醒APP,打开APP进入相对应的界面。

推送原理:极光可根据每个登陆者生成一个UUID 即 RegistrationID 作为标识 然后 根据这个 RegistrationID 去推送消息。

1、引入jar

代码语言:javascript复制
<!-- 极光推送jar -->
		<dependency>
		    <groupId>cn.jpush.api</groupId>
		    <artifactId>jpush-client</artifactId>
		    <version>3.2.19</version>
		</dependency>
		
		<dependency>
	        <groupId>cn.jpush.api</groupId>
	        <artifactId>jiguang-common</artifactId>
	        <version>1.1.1</version>
	    </dependency>

工具类:

代码语言:javascript复制
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class JiguangPush {
        //登录极光后台的生成的
	    private static String masterSecret = "";
        //极光后台获取的key
	    private static String appKey = "";
	    
	    private static int time =86400 ;
	    /**
	     * 极光推送
	     */
	    public static PushResult jiguangPush(String[] arr,String notification_title, String msg_title, String msg_content,String url,String accptMd){
	        log.info("设备id"   arr.toString()   "的用户推送信息ALERT=" msg_content);
	        PushResult result = push(arr,notification_title,msg_title,msg_content,url,accptMd);
	        if(result != null && result.isResultOK()){
	            log.info("设备id"   result.toString()   "的信息推送成功!");
	        }else{
	            log.info("设备id"   result.toString()   "的信息推送失败!");
	        }
	        return result;
	    }
	    
	    /**
	     * 生成极光推送对象PushPayload(采用java SDK)ios
	     * @param alias
	     * @param alert
	     * @param url 
	     * @return PushPayload
	     */
	    private static PushPayload buildPushObject_ios_alias_alert(String[] arr,String notification_title, String msg_title, String msg_content, String url){
	        return PushPayload.newBuilder()
	                .setPlatform(Platform.ios())//此处表明是什么设备,如 android ,ios ,winphone。
	                .setAudience(Audience.registrationId(arr)) 
	                .setNotification(Notification.newBuilder()
	                        .addPlatformNotification(IosNotification.newBuilder()
	                                .addExtra("pushUrl", url)
	                                .setAlert(IosAlert.newBuilder().setTitleAndBody("",msg_title, msg_content).build())
	                                .setSound("default")
	                                .incrBadge(1)
	                                .build())
	                        .build())//推送内容
	               /* .setMessage(Message.newBuilder()
	                        .setMsgContent(msg_content)
	                        .setTitle(msg_title)
	                        .build())*/
	                .setOptions(Options.newBuilder()
	                        .setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)
	                        .setTimeToLive(time)//消息在JPush服务器的失效时间(测试使用参数)
	                        .build())
	                .build();
	    }
	    
	    /**
         * 生成极光推送对象PushPayload(采用java SDK)android
         * @param alias
         * @param alert
         * @param url 
         * @return PushPayload
         */
        private static PushPayload buildPushObject_android_alias_alert(String[] arr,String notification_title, String msg_title, String msg_content, String url){
            return PushPayload.newBuilder()
                    .setPlatform(Platform.android())//此处表明是什么设备,如 android ,ios ,winphone。
                    .setAudience(Audience.registrationId(arr)) 
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(AndroidNotification.newBuilder()
                                    .setTitle(msg_title)
                                    .setAlert(msg_content)
                                    .addExtra("pushUrl", url)
                                    .build())
                            .build())//推送内容
                   /* .setMessage(Message.newBuilder()
                            .setMsgContent(msg_content)
                            .setTitle(msg_title)
                            .build())*/
                    .setOptions(Options.newBuilder()
                            .setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)
                            .setTimeToLive(time)//消息在JPush服务器的失效时间(测试使用参数)
                            .build())
                    .build();
        }
        
        /**
         * 生成极光推送对象PushPayload(采用java SDK)all 不指定设备
         * @param alias
         * @param alert
         * @param url 
         * @return PushPayload
         */
        private static PushPayload buildPushObject_android_ios_alias_alert(String[] arr,String notification_title, String msg_title, String msg_content, String url){
            return PushPayload.newBuilder()
                    .setPlatform(Platform.android_ios())//此处表明是什么设备,如 android ,ios ,winphone。
                    .setAudience(Audience.all()) 
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(AndroidNotification.newBuilder()
                                    .setTitle(msg_title)
                                    .setAlert(msg_content)
                                    .addExtra("pushUrl", url)
                                    .build())
                            .addPlatformNotification(IosNotification.newBuilder()
                                    .addExtra("pushUrl", url)
                                    .setAlert(IosAlert.newBuilder().setTitleAndBody("",msg_title, msg_content).build())
                                    .setSound("default")
                                    .incrBadge(1)
                                    .build())
                            .build())//推送内容
                   /* .setMessage(Message.newBuilder()
                            .setMsgContent(msg_content)
                            .setTitle(msg_title)
                            .build())*/
                    .setOptions(Options.newBuilder()
                            .setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)
                            .setTimeToLive(time)//消息在JPush服务器的失效时间(测试使用参数)
                            .build())
                    .build();
        }
        
        /**
         * 生成极光推送对象PushPayload(采用java SDK)all 指定设备
         * @param alias
         * @param alert
         * @param url 
         * @return PushPayload
         */
        private static PushPayload buildPushObject_android_ios_alias_alert_appoint_device(String[] arr,String notification_title, String msg_title, String msg_content, String url){
            return PushPayload.newBuilder()
                    .setPlatform(Platform.android_ios())//此处表明是什么设备,如 android ,ios ,winphone。
                    .setAudience(Audience.registrationId(arr)) 
                    .setNotification(Notification.newBuilder()
                            .addPlatformNotification(AndroidNotification.newBuilder()
                                    .setTitle(msg_title)
                                    .setAlert(msg_content)
                                    .addExtra("pushUrl", url)
                                    .addExtra("push_type", "2")
                                    .build())
                            .addPlatformNotification(IosNotification.newBuilder()
                                    .addExtra("pushUrl", url)
                                    .addExtra("push_type", "2")
                                    .setAlert(IosAlert.newBuilder().setTitleAndBody("",msg_title, msg_content).build())
                                    .setSound("default")
                                    .incrBadge(1)
                                    .build())
                            .build())//推送内容
                    /*.setMessage(Message.newBuilder()
                            .setMsgContent(msg_content)
                            .setTitle(msg_title)
                            .build())*/
                    .setOptions(Options.newBuilder()
                            .setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)
                            .setTimeToLive(time)//消息在JPush服务器的失效时间(测试使用参数)
                            .build())
                    .build();
        }
        
	    /**
	     * 极光推送方法(采用java SDK)
	     * @param alias
	     * @param alert
	     * @param url 
	     * @return PushResult
	     */
	    private static PushResult push(String[] arr,String notification_title, String msg_title, String msg_content, String url ,String accptMd){
	        PushResult pushResult = new PushResult();
	        ClientConfig clientConfig = ClientConfig.getInstance();
	        JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
	        PushPayload payload = null;
	        if("2".equals(accptMd)){
	            payload = buildPushObject_ios_alias_alert(arr,notification_title,msg_title,msg_content,url);
	        }else if("3".equals(accptMd)){
	            payload = buildPushObject_android_alias_alert(arr,notification_title,msg_title,msg_content,url);
	        }else if("23".equals(accptMd)){
	            payload = buildPushObject_android_ios_alias_alert_appoint_device(arr, notification_title, msg_title, msg_content, url);
	        }else if("all".equals(accptMd)){
	            payload = buildPushObject_android_ios_alias_alert(arr,notification_title,msg_title,msg_content,url);
	        }
	        try {
	            pushResult = jpushClient.sendPush(payload);
	        } catch (APIConnectionException e) {
	            log.error("Connection error. Should retry later. ", e);
	        } catch (APIRequestException e) {
	            log.error("Error response from JPush server. Should review and fix it. ", e);
	            log.info("HTTP Status: "   e.getStatus());
	            log.info("Error Code: "   e.getErrorCode());
	            log.info("Error Message: "   e.getErrorMessage());
	            log.info("Msg ID: "   e.getMsgId());
	        }    
	        return pushResult;
	    }

}

调用:

代码语言:javascript复制
       //根据客户查询 ID
     JGpustDto  dtoDec = jGpushMapper.qryDeviceNo("");
		
		 String[] devideNoStr = new String[0];
		 List<String> devideNoList = new ArrayList<String>();
		 devideNoList.add("");
         devideNoStr = devideNoList.toArray(new String[devideNoList.size()]);
         // 推送
         PushResult result = JiguangPush.jiguangPush(devideNoStr, "测试", "测试", "测试", "url", "23");
         /**
          * 对推送结果入库
          */
        if (result != null && result.isResultOK()) {
        	JGpustDto dto = new JGpustDto();
	         dto.setStatus("0");
	         dto.setContent("");
	         dto.setCustno("");
	         dto.setDeviceno("");
	         dto.setTitle("");
	         dto.setUrl("");
	     } else {
	    	 JGpustDto dto = new JGpustDto();
	         dto.setStatus("1");
	         dto.setContent("");
	         dto.setCustno("");
	         dto.setDeviceno("");
	         dto.setTitle("");
	         dto.setUrl("");
	     }

0 人点赞