前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >极光推送实例

极光推送实例

作者头像
斯文的程序
发布2019-11-07 19:26:34
1.3K0
发布2019-11-07 19:26:34
举报
文章被收录于专栏:带你回家带你回家

极光网址: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("");
	     }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档