小米推送java服务器端开发工具类

最近项目上用到了小米push,需求是安卓和ios都使用小米推送,这里仅记录服务器端(java)的集成工具类

准备工作:

1.让安卓和ios的开发人员提供包名和密钥

2.下载官方的 json-simple和MiPush_SDK_Server 的jar包,我这里使用的是json-simple-1.1.1.jar和MiPush_SDK_Server_2_2_19.jar版本

3.商量好用什么字段标志做为推送标识,我这里使用的是登录app的用户userId

4.这里安卓使用userAccount推送,ios使用alias推送,两者有什么区别?可以看下官方文档,个人觉得alias推送比较精确。

一、下载jar包,集成到自己的工程里

小米推送java服务器端开发工具类_第1张图片

二、编写工具类MipushUtils

package com.zte.job.executor.util;

import com.xiaomi.push.sdk.ErrorCode;
import com.xiaomi.xmpush.server.Constants;
import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Result;
import com.xiaomi.xmpush.server.Sender;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MipushUtils {
	public static final Logger logger = Logger.getLogger(MipushUtils.class);
	
	
	/**
	 * 安卓单用户UserAccount推送
	 * @param content
	 * @param title
	 * @param targetAccount
	 * @param secret
	 * @param packageName
	 * @return
	 */
	public static String  sendMiPushMessage(String content,String title,String targetAccount,String secret,String packageName){
		Constants.useOfficial();
		//ios 测试环境,不支持安卓
		//Constants.useSandbox();
		Sender sender = new Sender(secret);
	    Message message = new Message.Builder()
	        .title(title)
	        .description(content)
	        .restrictedPackageName(packageName)
	        .notifyType(1)     // 使用默认提示音提示
	        .notifyId(new Random().nextInt(Integer.MAX_VALUE))
	        .build();
	    String code=null;
		try {
			Result result = sender.sendToUserAccount(message, targetAccount, 2);
			logger.info("[MipushUtil][sendMiPushMessage] result :"+result.toString());
			code=result.getErrorCode().getValue()+"";
			ErrorCode ec=result.getErrorCode();
			logger.info("[MipushUtil][sendMiPushMessage] resultCode:"+ec.getDescription());
			logger.info("[MipushUtil][sendMiPushMessage] result user:"+targetAccount
					+",result description :"+result.getErrorCode().getDescription()
					+",result code :"+code);
		} catch (Exception e) {
			logger.error("[MipushUtil][sendMiPushMessage] error", e);
			e.printStackTrace();
		}
		return code;
	 
	}

	/**
	 * 安卓多用户UserAccount推送
	 * @param content
	 * @param title
	 * @param targetAccount
	 * @param secret
	 * @param packageName
	 * @return
	 */
	public static String  sendMiPushMessage(String content, String title, List targetAccount, String secret, String packageName){
		Constants.useOfficial();
		//ios 测试环境,不支持安卓
		//Constants.useSandbox();
		Sender sender = new Sender(secret);
		Message message = new Message.Builder()
				.title(title)
				.description(content)
				.restrictedPackageName(packageName)
				.notifyType(1)     // 使用默认提示音提示
				.notifyId(new Random().nextInt(Integer.MAX_VALUE))
				.build();
		String code=null;
		try {
			Result result = sender.sendToUserAccount(message, targetAccount, 2);

			logger.info("[MipushUtil][sendMiPushMessage] result :"+result.toString());
			code=result.getErrorCode().getValue()+"";
			ErrorCode ec=result.getErrorCode();
			logger.info("[MipushUtil][sendMiPushMessage] resultCode:"+ec.getDescription());
			logger.info("[MipushUtil][sendMiPushMessage] result user:"+targetAccount
					+",result description :"+result.getErrorCode().getDescription()
					+",result code :"+code);
		} catch (Exception e) {
			logger.error("[MipushUtil][sendMiPushMessage] error", e);
			e.printStackTrace();
		}
		return code;

	}
	/**
	 * IOS单用户UserAccount推送
	 * @param content
	 * @param title
	 * @param targetAccount
	 * @param clientType
	 * @param secret
	 * @param packageName
	 * @return
	 */
	public static String  sendMiPushIOSMessage(String content,String title,String targetAccount,String clientType,String secret,String packageName){
		//Constants.useOfficial();
		//ios 测试环境,不支持安卓
		Constants.useSandbox();
		Sender sender = new Sender(secret);
		Message message = new Message.IOSBuilder()
		.description(content)
		.build();
	    String code=null;
		try {
			Result result = sender.sendToUserAccount(message, targetAccount, 2);
			logger.info("[MipushUtil][sendMiPushMessage] result :"+result.toString());
			code=result.getErrorCode().getValue()+"";
			ErrorCode ec=result.getErrorCode();
			logger.info("[MipushUtil][sendMiPushMessage] resultCode:"+ec.getDescription());
			logger.info("[MipushUtil][sendMiPushMessage] result user:"+targetAccount
					+",result description :"+result.getErrorCode().getDescription()
					+",result code :"+code);
		} catch (Exception e) {
			logger.error("[MipushUtil][sendMiPushMessage] error", e);
			e.printStackTrace();
		}
		return code;
	 
	}
	
	/**
	 * IOS多用户UserAccount推送
	 * @param content
	 * @param title
	 * @param targetAccount
	 * @param secret
	 * @param packageName
	 * @return
	 */
	public static String  sendMiPushIOSMessage(String content, String title, List targetAccount, String secret, String packageName){
		Constants.useSandbox();
		Sender sender = new Sender(secret);
		Message message = new Message.IOSBuilder()
				.description(content)
				.build();
		String code=null;
		try {
			Result result = sender.sendToUserAccount(message, targetAccount, 2);

			logger.info("[MipushUtil][sendMiPushIOSMessage] result :"+result.toString());
			code=result.getErrorCode().getValue()+"";
			ErrorCode ec=result.getErrorCode();
			logger.info("[MipushUtil][sendMiPushIOSMessage] resultCode:"+ec.getDescription());
			logger.info("[MipushUtil][sendMiPushIOSMessage] result user:"+targetAccount
					+",result description :"+result.getErrorCode().getDescription()
					+",result code :"+code);
		} catch (Exception e) {
			logger.error("[MipushUtil][sendMiPushIOSMessage] error", e);
			e.printStackTrace();
		}
		return code;

	}
	
	
	/**
	 * IOS单用户alias别名推送
	 * @param content
	 * @param title
	 * @param alias
	 * @param clientType
	 * @param secret
	 * @param packageName
	 * @return
	 */
	public static String  sendMiPushIOSMessageByAlias(String content,String title,String targetAccount,String clientType,String secret,String packageName){
		Constants.useOfficial();
		//ios 测试环境,不支持安卓
		//Constants.useSandbox();
		Sender sender = new Sender(secret);
		Message message = new Message.IOSBuilder()
		.description(content)
		.build();
	    String code=null;
		try {
			Result result = sender.sendToAlias(message, targetAccount, 3);
			logger.info("[MipushUtil][sendMiPushMessage] result :"+result.toString());
			code=result.getErrorCode().getValue()+"";
			ErrorCode ec=result.getErrorCode();
			logger.info("[MipushUtil][sendMiPushMessage] resultCode:"+ec.getDescription());
			logger.info("[MipushUtil][sendMiPushMessage] result user:"+targetAccount
					+",result description :"+result.getErrorCode().getDescription()
					+",result code :"+code);
		} catch (Exception e) {
			logger.error("[MipushUtil][sendMiPushMessage] error", e);
			e.printStackTrace();
		}
		return code;
	 
	}
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		String content="测试内容3";
		String title="测试标题3";
		//用户id
		String userId="20190729";
		//密钥(替换自己的)
		String secret="xxxx";
		//包名(替换自己的)
		String packageName="xxx";
		String  result2 = MipushUtils.sendMiPushIOSMessageByAlias(content, title, userId,"1", secret, packageName);
		System.out.println("result1:"+result2);
	}
}

三、执行main方法,替换自己的密钥和包名,执行,若result返回为0则推送成功,否则失败!在执行之前请在代码中选择测试或者生产环境。

四、最后说下,son-simple-1.1.1.jar和MiPush_SDK_Server_2_2_19.jar这两个包在maven工程打包的时候会丢失,造成class not found错误,如果出现这种情况,需要将jar包手动放入工程的class下。

你可能感兴趣的:(小米推送java服务器端开发工具类)