jpush极光推送

jpush极光推送消息案例:

准备工作:

eclipse4.5 64位

jdk1.7 64位

所需jar:gson-2.2.4.jar,jiguang-common-0.1.4.jar,jpush-client-3.2.10.jar,json-lib-2.2.3-jdk13.jar,log4j-1.2.14.jar,slf4j-api-1.7.7.jar,slf4j-log4j12-1.7.7.jar

类PropertyUtil:资源属性工具类

package jpush;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * @class:PropertyUtil 
 *@descript:获得properties文件的值
 *@date:2016年10月26日 下午8:20:10
 *@author sanghaiqin
 *@version:V1.0
 */
public class PropertyUtil {
	/**
	 * @descript:加载资源文件
	 * @param resources  资源文件
	 * @return
	 * @throws FileNotFoundException
	 */
	private Properties loadProperties(String resources) {
		InputStream inputstream = null;
		Properties properties = new Properties();
		// 使用InputStream得到一个资源文件
		try {
			inputstream = new FileInputStream(resources);
			 // 加载配置文件
		     properties.load(inputstream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(inputstream!=null){
				try {
					inputstream.close();
				} catch (IOException e) {
					e.printStackTrace();
				} 
		    }
		}
	   return properties;
	}
	
	/**
	 * @descript:读属性文件
	 * @return
	 * @throws FileNotFoundException
	 */
	public Properties readProperties(){
		String resources = PropertyUtil.class.getClassLoader().getResource("prop.properties").getPath();
		Properties properties = loadProperties(resources);
		return properties;
	}
	
	/**
	 * @descript:测试
	 * @param args
	 */
	public static void main(String[] args)  {
		PropertyUtil p=new PropertyUtil();
		Properties pro=p.readProperties();
		String mailSenderUsername=(String) pro.get("mail.sender.username");
		System.out.println("邮件发送者用户名:"+mailSenderUsername);//neo_lifes@163.com
		String path = PropertyUtil.class.getClassLoader().getResource("prop.properties").getPath();
		System.out.println(path);// /G:/Workspaces4.4/test/bin/prop.properties
	}
	
}


类JPushUtil:极光推送消息

package jpush;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.alibaba.fastjson.JSONObject;

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.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * @class:JPushUtil 
 *@descript:极光推送
 *所需jar包:
 *gson-2.2.4.jar
 *jiguang-common-0.1.4.jar
 *jpush-client-3.2.10.jar
 *json-lib-2.2.3-jdk13.jar
 *log4j-1.2.14.jar
 *slf4j-api-1.7.7.jar
 *slf4j-log4j12-1.7.7.jar
 *备注:必须有log4j.properties否则测试看不到效果
 *@date:2016年10月28日 下午2:19:21
 *@author sanghaiqin
 *@version:V1.0
 */
public class JPushUtil {
    //资源属性工具类类
	private static PropertyUtil propertyUtil = new PropertyUtil();
	
	/**
	 * @descript:司机用户推送
	 * @param message 推送信息
	 * @param json  推送数据
	 * @param alias  多个参数,参数为string字符串或者string数组
	 * @return  int pushStatus=1推送成功pushStatus=0推送失败
	 */
	public static int jpushToDriverByAlias(String message,String json,String... alias) {
		//推送标识默认为1表示推送成功
		int pushStatus = 1;
		 try {
			//读取司机用户的资源文件
			Properties properties = propertyUtil.readProperties();
			//司机用户appkey和secret
		    JPushClient jpushClient = new JPushClient(properties.getProperty("jPush.driver.masterSecret"), properties.getProperty("jPush.driver.appkey"));
            //推送信息
		    PushPayload payload = buildPushObject_ios_audienceMore_messageWithExtras(message,json,alias);
			//发送推送信息
		    PushResult result = jpushClient.sendPush(payload);
			//推送结果
		    pushStatus = result.getResponseCode()==200?1:0;
		}  catch (APIConnectionException e) {
			pushStatus= 0;
			e.printStackTrace();
		} catch (APIRequestException e) {
			pushStatus= 0;
			e.printStackTrace();
		}
		 return pushStatus;
	}
	
	/**
	 * @descript:门店用户推送
	 * @param message  推送信息
	 * @param json  推送数据
	 * @param alias  alias  多个参数,参数为string字符串或者string数组
	 * @return  int pushStatus=1推送成功pushStatus=0推送失败
	 */
	public static int jpushToStoreByAlias(String message,String json,String... alias){
		//推送标识默认为1表示推送成功
		int pushStatus = 1;
		 try {
			//读取门店用户的资源文件
			Properties properties = propertyUtil.readProperties();
			//门店用户appkey和secret
		    JPushClient jpushClient = new JPushClient(properties.getProperty("jPush.store.masterSecret"), properties.getProperty("jPush.store.appkey"));
            //推送信息
		    PushPayload payload = buildPushObject_ios_audienceMore_messageWithExtras(message,json,alias);
			//发送推送信息
		    PushResult result = jpushClient.sendPush(payload);
			//推送结果
		    pushStatus = result.getResponseCode()==200?1:0;
		}  catch (APIConnectionException e) {
			pushStatus= 0;
			e.printStackTrace();
		} catch (APIRequestException e) {
			pushStatus= 0;
			e.printStackTrace();
		}
		 return pushStatus;
	}
	
	/**
	 * @descript:推送所有司机信息
	 * @param message  推送信息
	 * @param json  推送数据
	 * @return  int pushStatus=1推送成功pushStatus=0推送失败
	 */
	public static int jpushAllDriver(String message,String json){
		//推送标识默认为1表示推送成功
		int pushStatus = 1;
		try {
			//读取司机用户的资源文件
			Properties properties = propertyUtil.readProperties();
			//司机用户appkey和secret
			JPushClient jpushClient = new JPushClient(properties.getProperty("jPush.driver.masterSecret"), properties.getProperty("jPush.driver.appkey"));
			//推送信息
			PushPayload payload = buildPushObject_all_alias_alert(message,json);
			//发送推送信息
			PushResult result = jpushClient.sendPush(payload);
			//推送结果
			pushStatus = result.getResponseCode()==200?1:0;
		} catch (APIConnectionException e) {
			pushStatus = 0;
			e.printStackTrace();
		} catch (APIRequestException e) {
			pushStatus = 0;
			e.printStackTrace();
		}
		return pushStatus;
	}
	
	/**
	 * @descript:推送所有门店用户信息
	 * @param message  推送信息
	 * @param json   推送数据
	 * @return  int pushStatus=1推送成功pushStatus=0推送失败
	 */
	public static int jpushAllStore(String message,String json) {
		推送标识默认为1表示推送成功
		int pushStatus = 1;
		try {
			//读取门店用户的资源文件
			Properties properties = propertyUtil.readProperties();
			//门店用户appkey和secret
			JPushClient jpushClient = new JPushClient(properties.getProperty("jPush.store.masterSecret"), properties.getProperty("jPush.store.appkey"));
			//推送信息
			PushPayload payload = buildPushObject_all_alias_alert(message,json);
			//发送推送信息
			PushResult result = jpushClient.sendPush(payload);
			//推送结果
			pushStatus = result.getResponseCode()==200?1:0;
		} catch (APIConnectionException e) {
			pushStatus = 0;
			e.printStackTrace();
		} catch (APIRequestException e) {
			pushStatus = 0;
			e.printStackTrace();
		}
		return pushStatus;
	}
	
	/**
	 * @descript:极光推送封装数据
	 * @param message
	 * @param json
	 * @param alias
	 * @return
	 */
	 public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String message,String json, String... alias) {
		  return PushPayload.newBuilder()
				  .setPlatform(Platform.all())//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
				  .setAudience(Audience.alias(alias))//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应用客户端调用接口获取到的registration id
				  .setNotification(Notification.newBuilder()//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
				  .addPlatformNotification(AndroidNotification.newBuilder()//指定当前推送的android通知
				  .setAlert(message)
				  .addExtra("json", json)//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
				  .build())
				  .addPlatformNotification(IosNotification.newBuilder()//指定当前推送的iOS通知
				  .setAlert(message)
				  .addExtra("json", json)//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
				  .setContentAvailable(true)//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
				  .build()).build())
				  .setOptions(Options.newBuilder()
				  .setApnsProduction(false)//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
				  .build())
				  .build();
	   }
	   
	 /**
	  * @descript:极光推送封装数据
	  * @param message
	  * @param json
	  * @return
	  */
	   public static PushPayload buildPushObject_all_alias_alert(String message,String json) {
			  return PushPayload.newBuilder()
					  .setPlatform(Platform.all())//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
					  .setAudience(Audience.all())//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应用客户端调用接口获取到的registration id
					  .setNotification(Notification.newBuilder()//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
					  .addPlatformNotification(AndroidNotification.newBuilder()//指定当前推送的android通知
					  .setAlert(message)
					  .addExtra("json", json)//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
					  .build())
					  .addPlatformNotification(IosNotification.newBuilder()//指定当前推送的iOS通知
					  .setAlert(message)
					  .addExtra("json", json)//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
					  .setContentAvailable(true)//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
					  .build()).build())
					  .setOptions(Options.newBuilder()
					  .setApnsProduction(false)//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
					  .build())
					  .build();
		}
	  
	  /**
	   * @descript:
	   * @param args
	   */
	   public static void main(String[] args)  {
		   //测试1司机用户userId=713
		  /* String message="亲,你的订单已经被处理,请及时查看!";
		   Map<String, Object> map=new HashMap<String, Object>();
		   map.put("司机1", "司机1买了篮球");
		   map.put("司机2", "司机2中了500万");
		   JSONObject jsonObject =(JSONObject) JSONObject.toJSON(map);	
		   String driveId="713";
		   jpushToDriverByAlias(message, jsonObject.toJSONString(),driveId);*/
		   
		   //测试2司机用户userId=713
		   //jpushAllDriver("我是jpush测试机","713");
		   
		   //测试1门店用户userId=522
		   /*String message2="亲,您有新的订单,请及时处理!";
		   Map<String, Object> map2=new HashMap<String, Object>();
		   map2.put("门店1", "门店1买了篮球");
		   map2.put("门店2", "门店2中了500万");
		   String storeId="188";
		   JSONObject jsonObject2 =(JSONObject) JSONObject.toJSON(map2);	
		   jpushToStoreByAlias(message2,jsonObject2.toJSONString(),storeId);*/
		  
		   //测试2门店用户
		   jpushAllStore("门店用户推送订单","188");
		  
	   }
	  
	  
}


资源文件log4j.properties

log4j.rootLogger=debug, stdout, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t]  ***** %5p ****** %c{1}:%L ***** - %m%n

log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.File = log/sysMng.log
log4j.appender.file.MaxFileSize=5MB
# Keep one backup file
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout.ConversionPattern=%d [%t]  %5p %c{1}:%L - %m%n

资源文件pro.properties

#-------------------极光推送功能------------------
#司机用户appkey和secret
jPush.driver.appkey=870893abd9a89ec5a97f9efc
jPush.driver.masterSecret =647736f15583e8fbfae6b22a
#门店用户appkey和secret
jPush.store.appkey=951800c747a694bc0fc39115
jPush.store.masterSecret =cd260922eea73ecbaa80a22d

测试截图:
测试1司机用户userId=713


测试2司机用户userId=713



测试1门店用户userId=522



测试2门店用户


项目结构截图:








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值