极光推送-JPush java_sdk 的使用

1:引入maven

<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>

2.极光推送工具类

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 工具类
 */
public class JPushUtil {
	
	private static  String AppKey = "06947ff1ce4a86f1b0b1xxxx";
	private static  String Maste_Secret = "9b23669ef51f98xxxx";

	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推送给所有用户
	 *  @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
	 * @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("消息推送成功!");
	}
	
		/**
		 *  @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
		 *  @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("消息推送成功!");
		}
		
	 /**
	  * 商品成交工具类   物流通知
	  *  @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();
	     }
	 
	 
	
}

 

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值