Java极光推送

首先需要在极光推送官网推送控制台创建AppKey和Master Secret(包名随便写一个会自动生成测试包),并下载对应的APP安装包测试Java代码能否正确推送

 

集成maven依赖

<dependency>
        <groupId>cn.jpush.api</groupId>
        <artifactId>jiguang-common</artifactId>
        <version>1.1.3</version>
</dependency>
<dependency>
    <groupId>cn.jpush.api</groupId>
    <artifactId>jpush-client</artifactId>
    <version>3.3.9</version>
</dependency>

由于官方也没有将Java对应API整理好文档,故为方便以后使用特此整理如下

 功能方法
Push API推送sendPush(PushPayload pushPayload)
推送校验sendPushValidate(PushPayload payload)
推送唯一标识符getCidList(int count, String type)
Report API送达统计getReportReceiveds(String msgIds)
送达状态查询getMessageStatus(CheckMessagePayload payload)
消息统计(vip可用)getReportMessages(String msgIds)
用户统计(vip可用)getReportUsers(TimeUnit timeUnit, String start, int duration)
Device API查询设备的别名与标签getDeviceTagAlias(String registrationId)
设置设备的别名与标签updateDeviceTagAlias(String registrationId, String alias,Set<String> tagsToAdd, Set<String> tagsToRemove)
清楚所有别名和标签updateDeviceTagAlias(String registrationId, boolean clearAlias, boolean clearTag)
查询别名 getAliasDeviceList(String alias,String platform)
删除别名deleteAlias(String alias, String platform)
查询标签列表getTagList()
更新标签addRemoveDevicesFromTag(String theTag,Set<String> toAddUsers, Set<String> toRemoveUsers)
删除标签deleteTag(String theTag, String platform)
判断设备与标签绑定关系isDeviceInTag(String theTag, String registrationID)
获取用户在线状态(vip可用)getUserOnlineStatus(String... registrationIds)

demo代码

package com.jpush;

import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.connection.HttpProxy;
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.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;

public class Jpush {
	
	protected static final Logger LOG = LoggerFactory.getLogger(Jpush.class);
	protected static final String APP_KEY = "3c3aac234abd0d66cba05ed7";
	protected static final String MASTER_SECRET = "1a0a7a22ac56b272d86b4452";
	protected ClientConfig clientConfig = ClientConfig.getInstance();
	protected HttpProxy proxy = null;
	protected JPushClient jPushClient = null;
	
	public static void main(String[] args) {
		Jpush jpush = new Jpush();
		jpush.push("消息标题", "哈哈哈", "", Audience.all(), new HashMap<String, String>());
	}
	
	public JPushClient getJPushClient(){
		//代理设置
		proxy = new HttpProxy("192.168.100.36", 8088);
		clientConfig = ClientConfig.getInstance();
		jPushClient = new JPushClient(MASTER_SECRET, APP_KEY, proxy, clientConfig);
		return jPushClient;
	}
	
	public PushResult push(String title, String message, String extras,
			Audience audience, Map<String, String> map) {		
		PushResult result = null;
		try {
			result = getJPushClient().sendPushValidate(buildPushObject_android_and_ios(
					title, message, extras, audience, map));
			System.out.println(result);
			return result;
		} catch (APIConnectionException e) {
			LOG.error(e.getMessage(), e);
		} catch (APIRequestException e) {
			LOG.error(e.getMessage(), e);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
		return result;

	}
	
	/**
	 * 
	 *  @Description: 
	 *  @param title 标题
	 *  @param message 消息内容
	 *  @param extras 额外字段
	 *  @param audience	推送设备指定
	 *  @param map 自定义消息map集合
	 *  @return
	 */
	public PushPayload buildPushObject_android_and_ios(String title,
			String message, String extras, Audience audience,
			Map<String, String> map) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(audience)
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										AndroidNotification.newBuilder()
												.setTitle(title)
												.setAlert(message)
												.addExtra("extras", extras)
												.build())
								.addPlatformNotification(
										IosNotification.newBuilder()
												.setAlert(message)
												.setMutableContent(true)
												.addExtra("extras", extras)
												.build()).build())
				.setMessage(
						Message.newBuilder().setMsgContent("自定义消息内容")
								.setTitle("自定义消息标题").addExtras(map)
								.build())
				.setOptions(Options.newBuilder().setApnsProduction(false)// 仅仅对于IOS生效.false开发环境
						.build())
				.build();
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值