java集成oppo推送sdk

准备工作

1.申请推送服务需要在oppo官网上架自己的app(比较蛋疼),需要一些资料,上架以后app评级为A/B的应用方可申请推送服务,获取到app_key和app_secret。

2.oppo官网下载sdk包,api文档,将jar包引入到自己的项目即可,sdk内部已经维护每次请求所需的auth_token(有效期默认1天),所以不需要开发者自己维护。

测试代码块

/**
 * oppo推送测试类
 * 
 */
public class Oppo {

	private static final String APP_KEY = "APP_KEY";

	private static final String APP_SECRET = "APP_SECRET";

	/**
	 * 通知栏消息tile
	 */
	private static final String TITLE = "通知栏消息tile";

	/**
	 * 通知栏消息内容
	 */
	private static final String CONTENT = "通知栏消息内容";

	public static void main(String[] args) {
		// 单推
		unicastNotification("regId");
		// 群推
		List<String> regIds = new ArrayList<String>();
		regIds.add("regId1");
		regIds.add("regId2");
		unicastBatchNotification(regIds);
		// 广播(多个用;号隔开)
		broadcastNotification("regId1;regId2;regId3");
	}

	/**
	 * 创建通知栏消息体
	 * 
	 * @return
	 */
	private static Notification getNotification() {
		Notification notification = new Notification();

		/**
		 * 以下参数必填项
		 */
		notification.setTitle(TITLE);
		notification.setContent(CONTENT);

		/**
		 * 以下参数非必填项, 如果需要使用可以参考OPPO push服务端api文档进行设置
		 */
		// subTitle - 子标题 设置在通知栏展示的通知栏标题, 【非必填,字数限制1~10,中英文均以一个计算】
		notification.setSubTitle("sub tile");

		// App开发者自定义消息Id,OPPO推送平台根据此ID做去重处理,对于广播推送相同appMessageId只会保存一次,对于单推相同appMessageId只会推送一次
		notification.setAppMessageId("test");

		// 应用接收消息到达回执的回调URL,字数限制200以内,中英文均以一个计算
		notification.setCallBackUrl("http://www.test.com");
		// App开发者自定义回执参数,字数限制50以内,中英文均以一个计算
		notification.setCallBackParameter("");

		// 点击动作类型0,启动应用;1,打开应用内页(activity的intent
		// action);2,打开网页;4,打开应用内页(activity);【非必填,默认值为0】;5,Intent scheme URL
		notification.setClickActionType(4);
		// 应用内页地址【click_action_type为1或4时必填,长度500】
		notification.setClickActionActivity("com.coloros.push.demo.component.InternalActivity");
		// 网页地址【click_action_type为2必填,长度500】
		notification.setClickActionUrl("http://www.test.com");

		// 动作参数,打开应用内页或网页时传递给应用或网页【JSON格式,非必填】,字符数不能超过4K,示例:{"key1":"value1","key2":"value2"}
		notification.setActionParameters("{\"key1\":\"value1\",\"key2\":\"value2\"}");

		// 展示类型 (0, “即时”),(1, “定时”)
		notification.setShowTimeType(1);
		// 定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数
		notification.setShowStartTime(System.currentTimeMillis() + 1000 * 60 * 3);
		// 定时展示结束时间(根据time_zone转换成当地时间),时间的毫秒数
		notification.setShowEndTime(System.currentTimeMillis() + 1000 * 60 * 5);

		// 是否进离线消息,【非必填,默认为True】
		notification.setOffLine(true);
		// 离线消息的存活时间(time_to_live) (单位:秒), 【off_line值为true时,必填,最长3天】
		notification.setOffLineTtl(24 * 3600);

		// 时区,默认值:(GMT+08:00)北京,香港,新加坡
		notification.setTimeZone("GMT+08:00");

		// 0:不限联网方式, 1:仅wifi推送
		notification.setNetworkType(0);

		return notification;
	}

	/**
	 * 发送单推通知栏消息
	 */
	public static void unicastNotification(String regId) {
		try {
			Sender sender = new Sender(APP_KEY, APP_SECRET);
			// 创建通知栏消息体
			Notification notification = getNotification();
			// 创建发送对象
			Target target = Target.build(regId);
			// 发送单推消息
			Result result = sender.unicastNotification(notification, target);
			// 获取http请求状态码
			System.out.println(result.getStatusCode());
			// 获取平台返回码
			System.out.println(result.getReturnCode());
			// 获取平台返回的messageId
			System.out.println(result.getMessageId());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送批量单推通知栏消息
	 * 
	 * @param regIds
	 *            需要推送的regId
	 */
	public static void unicastBatchNotification(List<String> regIds) {
		try {
			Sender sender = new Sender(APP_KEY, APP_SECRET);
			Map<Target, Notification> batch = new HashMap<Target, Notification>();
			// batch最大为1000
			for (String regId : regIds) {
				batch.put(Target.build(regId), getNotification());
			}
			// 发送批量单推消息
			Result result_unicast = sender.unicastBatchNotification(batch);
			// 获取http请求状态码
			System.out.println(result_unicast.getStatusCode());
			// 获取平台返回码
			System.out.println(result_unicast.getReturnCode());

			List<Result.UnicastBatchResult> batchResult = result_unicast.getUnicastBatchResults(); // 批量单推结果
			for (Result.UnicastBatchResult record : batchResult) {
				record.getErrorCode();
				record.getErrorMessage();
				record.getMessageId();
				record.getTargetValue();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送广播消息
	 */
	public static void broadcastNotification(String regIds) {
		try {
			Sender sender = new Sender(APP_KEY, APP_SECRET);
			// 创建通知栏消息体
			Notification broadNotification = getNotification();
			// 发送保存消息体请求
			Result saveResult = sender.saveNotification(broadNotification);
			// 获取http请求状态码
			System.out.println(saveResult.getStatusCode());
			// 获取平台返回码
			System.out.println(saveResult.getReturnCode());
			// 获取messageId
			String messageId = saveResult.getMessageId();
			// 创建广播目标
			Target target = new Target();
			target.setTargetValue(regIds);
			// 发送广播消息
			Result broadResult = sender.broadcastNotification(messageId, target);
			// 获取广播taskId
			broadResult.getTaskId();
			List<Result.BroadcastErrorResult> errorList = broadResult.getBroadcastErrorResults();
			if (errorList.size() > 0) { // 如果大小为0,代表所有目标发送成功
				for (Result.BroadcastErrorResult error : errorList) {
					error.getErrorCode(); // 错误码
					error.getTargetValue(); // 目标
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值