前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JPush java_sdk 的使用

JPush java_sdk 的使用

作者头像
用户5927264
发布2019-08-01 10:20:56
7250
发布2019-08-01 10:20:56
举报
文章被收录于专栏:OSChinaOSChina

1 maven配置文件

<!-- 极光推送 jPush -->

代码语言:javascript
复制
<dependency>
		    <groupId>cn.jpush.api</groupId>
		    <artifactId>jpush-client</artifactId>
		    <version>3.2.7</version>
		</dependency>
		
		 <dependency>
	        <groupId>cn.jpush.api</groupId>
	        <artifactId>jiguang-common</artifactId>
	        <version>1.1.1</version>
	    </dependency>
	    <dependency>
	        <groupId>io.netty</groupId>
	        <artifactId>netty-all</artifactId>
	        <version>4.1.6.Final</version>
	        <scope>compile</scope>
	    </dependency>
	    <dependency>
	        <groupId>com.google.code.gson</groupId>
	        <artifactId>gson</artifactId>
	    </dependency>
	   <!--  <dependency>
	        <groupId>org.slf4j</groupId>
	        <artifactId>slf4j-api</artifactId>
	    </dependency> -->

	    <!-- For log4j -->
	   <!--  <dependency>
	        <groupId>org.slf4j</groupId>
	        <artifactId>slf4j-log4j12</artifactId>
	    </dependency>
	    <dependency>
	        <groupId>log4j</groupId>
	        <artifactId>log4j</artifactId>
	        <version>1.2.17</version>
	    </dependency> -->

2 极光推送工具类

代码语言:javascript
复制
package com.zjxnjz.mall.core.jPush;


import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
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.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 极光推送 Jpush 工具类
 * @author SHF
 * @version 创建时间:2018年6月15日  下午4:33:46
 */
public class JPushUtil {
	
	private static  String AppKey = "06947ff1ce4a86f1b0b1f2c1";
	private static  String Maste_Secret = "9b23669ef51f984c6ad17f8c";

	public static PushResult push(Audience audience,Notification notification,Message message) {
    	//创建jpush对象
    	JPushClient jPushClient=new JPushClient(Maste_Secret,AppKey);
    	
    	PushPayload payload = PushPayload.newBuilder()
    			.setPlatform(Platform.all())
    			.setAudience(audience)
    			.setNotification(notification)
    			.setMessage(message)
    			.build();
    	try {
			PushResult result = jPushClient.sendPush(payload);
			System.out.println("success");
			System.out.println("消息id为:"+result.msg_id);
			System.out.println("发送id为:"+result.sendno);
			// 请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。
			 //jPushClient.close();
			return result;
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
    	return null;
    }
	
	/**
	 * Jpush推送给所有用户
	 * @author SHF
	 * @version 创建时间:2018年6月19日  上午10:46:44
	 *  @param message
	 */
	public static ResultTip sendAllUser(JPushMessage message) {
		Audience audience = Audience.all();
		Notification notification = Notification.alert(message);
		Message msg = Message.content(message.getJpushMsg());
		PushResult result = JPushUtil.push(audience,notification, msg);
		return ResultTip.success("消息推送成功!");
	}
	
	/**----------------------------------------------------------店铺商品新增商品推送----------------------------------------------
	 * android_and_ios    店铺商品新增商品
	 * 推送部分用户的id
	 * @author SHF
	 * @version 创建时间:2018年6月19日  上午11:18:01
	 *  @param message
	 *  @return
	 */
	public static ResultTip sendOnlyUser(JPushMessage message) {
		//创建jpush对象
    	JPushClient jPushClient=new JPushClient(Maste_Secret,AppKey);
    	
    	PushPayload payload = buildPushObject_android_and_ios(message);
    	try {
			PushResult result = jPushClient.sendPush(payload);
			System.out.println("success");
			System.out.println("消息id为:"+result.msg_id);
			System.out.println("发送id为:"+result.sendno);
			
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
		return ResultTip.success("消息推送成功!");
	}
	
		/**
		 * 店铺商品新增商品  推送店铺商品信息
		 * type : 1
		 * @author SHF
		 * @version 创建时间:2018年6月20日  上午11:32:42
		 *  @param message
		 *  @return
		 */
	 public static PushPayload buildPushObject_android_and_ios(JPushMessage message) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.registrationId(message.getDevIds()))  //设备id集合
                .setNotification(Notification.newBuilder()
                        .setAlert(message.getJpushMsg())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                 .setAlert(message.getJpushMsg())
                                 .setTitle(message.getJpushTitle())
                                 //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                .addExtra("goodId",message.getGoodId())
                                .addExtra("shopId", message.getShopId())
                                .addExtra("goodName", message.getGoodName())
                                .addExtra("shopname", message.getShopName())
                                .addExtra("type", message.getType())
                                .build()
                         )
                         .addPlatformNotification(IosNotification.newBuilder()
                                 //传一个IosAlert对象,指定apns title、title、subtitle等
                                 .setAlert(message.getJpushMsg())
                                 //直接传alert
                                 //此项是指定此推送的badge自动加1
                                .incrBadge(1)
                                 //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
                                // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
                                .setSound("sound.caf")
                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
                                .addExtra("goodId",message.getGoodId())
                                .addExtra("shopId", message.getShopId())
                                .addExtra("goodName", message.getGoodName())
                                .addExtra("shopname", message.getShopName())
                                .addExtra("type", message.getType())
                                 //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
                                 // .setContentAvailable(true)
                                 .build()
                         )
                         .build()
                 )
                 //Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
                 // sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
                // [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
                 .setMessage(Message.newBuilder()
                         .setMsgContent(message.getJpushMsg())
                         .setTitle(message.getJpushTitle())
                         .addExtra("goodId",message.getGoodId())
                         .addExtra("shopId", message.getShopId())
                         .addExtra("goodName", message.getGoodName())
                         .addExtra("shopname", message.getShopName())
                         .addExtra("type", message.getType())
                         .build())
  
                 .setOptions(Options.newBuilder()
                         //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
                         .setApnsProduction(false)
                         //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
                         //.setSendno(1)
                        //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
                        .setTimeToLive(864000)
                        .build()
                 )
                 .build();
     }
	 
	 	/**---------------------------------------------------商品成交推送/物流通知-----------------------------------------------------
		 * android_and_ios    
		 * 推送部分用户的id
		 * @author SHF
		 * @version 创建时间:2018年6月19日  上午11:18:01
		 *  @param message
		 *  @return
		 */
		public static ResultTip sendGoodsClinch(JPushMessage message) {
			//创建jpush对象
	    	JPushClient jPushClient=new JPushClient(Maste_Secret,AppKey);
	    	
	    	PushPayload payload = goodsClinch_android_and_ios(message);
	    	try {
				PushResult result = jPushClient.sendPush(payload);
				System.out.println("success");
				System.out.println("消息id为:"+result.msg_id);
				System.out.println("发送id为:"+result.sendno);
				
			} catch (APIConnectionException e) {
				e.printStackTrace();
			} catch (APIRequestException e) {
				e.printStackTrace();
			}
			return ResultTip.success("消息推送成功!");
		}
		
	 /**
	  * 商品成交工具类   物流通知
	  * type : 2 
	  * @author SHF
	  * @version 创建时间:2018年6月20日  上午11:37:39
	  *  @param message
	  *  @return
	  */
	 public static PushPayload goodsClinch_android_and_ios(JPushMessage message) {
	        return PushPayload.newBuilder()
	                .setPlatform(Platform.android_ios())
	                .setAudience(Audience.registrationId(message.getDevIds()))  //设备id集合
	                .setNotification(Notification.newBuilder()
	                        .setAlert(message.getJpushMsg())
	                        .addPlatformNotification(AndroidNotification.newBuilder()
	                                 .setAlert(message.getJpushMsg())
	                                 .setTitle(message.getJpushTitle())
	                                 //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
	                                //.addExtra("orderId",message.getOrderId())
	                                .addExtra("type",message.getType())
	                                .build()
	                         )
	                         .addPlatformNotification(IosNotification.newBuilder()
	                                 //传一个IosAlert对象,指定apns title、title、subtitle等
	                                 .setAlert(message.getJpushMsg())
	                                 //直接传alert
	                                 //此项是指定此推送的badge自动加1
	                                .incrBadge(1)
	                                 //此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
	                                // 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
	                                .setSound("sound.caf")
	                                //此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
	                                //.addExtra("orderId",message.getOrderId())
	                                .addExtra("type", message.getType())
	                                 //此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
	                                 // .setContentAvailable(true)
	                                 .build()
	                         )
	                         .build()
	                 )
	                 .setOptions(Options.newBuilder()
	                         //此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
	                         .setApnsProduction(false)
	                         //此字段是给开发者自己给推送编号,方便推送者分辨推送记录
	                         //.setSendno(1)
	                        //此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
	                        .setTimeToLive(864000)
	                        .build()
	                 )
	                 .build();
	     }
	 
	 
	
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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