极光推送java服务端

首先明确一下推送的两个概念   消息(透传)和通知

通知:发送后会在系统通知栏收到展现,同时响铃或振动提醒用户。 ( mqtt不知道能不能发送通知,还在研究中...)
消息:以透传的形式传递给客户端,无显示,发送后不会在系统通知栏展现,第三方应用后需要开发者写代码才能看到。


写了一个简单工具类备用

/**
 * www.xinghansoft.com 版权所有 ©大连星汉网络有限公司
 * 
 * @author WangMeng
 * @date 2017年1月13日
 */
package xhsoft.yxd.admin.controller;

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

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;

/**
 * @author WangMeng
 * @date 2017年1月13日
 */
public class PushUtil
{
	/**
	 * 给所有平台的所有用户发通知
	 */
	public static void sendAllsetNotification(String message)
	{
		JPushClient jpushClient = new JPushClient("b6f47f289ee6dae11a529fcd",
				"21742e62e41164461c4af259");
		//JPushClient jpushClient = new JPushClient(masterSecret, appKey);//第一个参数是masterSecret 第二个是appKey
		Map<String, String> extras = new HashMap<String, String>();
		// 添加附加信息
		extras.put("extMessage", "我是额外的通知");
		PushPayload payload = buildPushObject_all_alias_alert(message, extras);
		try
		{
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
		}
		catch (APIConnectionException e)
		{
			System.out.println(e);
		}
		catch (APIRequestException e)
		{
			System.out.println(e);
			System.out.println("Error response from JPush server. Should review and fix it. " + e);
			System.out.println("HTTP Status: " + e.getStatus());
			System.out.println("Error Code: " + e.getErrorCode());
			System.out.println("Error Message: " + e.getErrorMessage());
			System.out.println("Msg ID: " + e.getMsgId());
		}
	}
	/**
	 * 给所有平台的所有用户发消息
	 * 
	 * @param message
	 * @author WangMeng
	 * @date 2017年1月13日
	 */
	public static void sendAllMessage(String message)
	{
		JPushClient jpushClient = new JPushClient("b6f47f289ee6dae11a529fcd",
				"21742e62e41164461c4af259");
		Map<String, String> extras = new HashMap<String, String>();
		// 添加附加信息
		extras.put("extMessage", "我是额外透传的消息");
		PushPayload payload = buildPushObject_all_alias_Message(message, extras);
		try
		{
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
		}
		catch (APIConnectionException e)
		{
			System.out.println(e);
		}
		catch (APIRequestException e)
		{
			System.out.println(e);
			System.out.println("Error response from JPush server. Should review and fix it. " + e);
			System.out.println("HTTP Status: " + e.getStatus());
			System.out.println("Error Code: " + e.getErrorCode());
			System.out.println("Error Message: " + e.getErrorMessage());
			System.out.println("Msg ID: " + e.getMsgId());
		}
	}
	/**
	 * 发送通知
	 * 
	 * @param message
	 * @param extras
	 * @return
	 * @author WangMeng
	 * @date 2017年1月13日
	 */
	private static PushPayload buildPushObject_all_alias_alert(String message,
			Map<String, String> extras)
	{
		return PushPayload.newBuilder()
				.setPlatform(Platform.all())
				// 设置平台
				.setAudience(Audience.all())
				// 按什么发送 tag alia
				.setNotification(
						Notification
								.newBuilder()
								.setAlert(message)
								.addPlatformNotification(
										AndroidNotification.newBuilder().addExtras(extras).build())
								.addPlatformNotification(
										IosNotification.newBuilder().addExtras(extras).build())
								.build())
				// 发送消息
				.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
				//设置ios平台环境  True 表示推送生产环境,False 表示要推送开发环境   默认是开发  
	}
	/**
	 * 发送透传消息
	 * 
	 * @param message
	 * @param extras
	 * @return
	 * @author WangMeng
	 * @date 2017年1月13日
	 */
	private static PushPayload buildPushObject_all_alias_Message(String message,
			Map<String, String> extras)
	{
		return PushPayload.newBuilder().setPlatform(Platform.all())
		// 设置平台
		.setAudience(Audience.all())
			// 按什么发送 tag alia
			.setMessage(Message.newBuilder().setMsgContent(message).addExtras(extras).build())
			// 发送通知
			.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
		//设置ios平台环境  True 表示推送生产环境,False 表示要推送开发环境   默认是开发  
	}

	/**
	 * 客户端 给所有平台的一个或者一组用户发送信息
	 */
	public static void sendAlias(String message, List<String> aliasList)
	{
		JPushClient jpushClient = new JPushClient("b6f47f289ee6dae11a529fcd",
				"21742e62e41164461c4af259");
		Map<String, String> extras = new HashMap<String, String>();
		// 添加附加信息
		extras.put("extMessage", "我是额外的消息--sendAlias");

		PushPayload payload = allPlatformAndAlias(message, extras, aliasList);
		try
		{
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
		}
		catch (APIConnectionException e)
		{
			System.out.println(e);
		}
		catch (APIRequestException e)
		{
			System.out.println(e);
			System.out.println("Error response from JPush server. Should review and fix it. " + e);
			System.out.println("HTTP Status: " + e.getStatus());
			System.out.println("Error Code: " + e.getErrorCode());
			System.out.println("Error Message: " + e.getErrorMessage());
			System.out.println("Msg ID: " + e.getMsgId());
		}
	}

	/**
	 * 极光推送:生成向一个或者一组用户发送的消息。
	 */
	private static PushPayload allPlatformAndAlias(String alert, Map<String, String> extras,
			List<String> aliasList)
	{

		return PushPayload
				.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.alias(aliasList))
				.setNotification(
						Notification
								.newBuilder()
								.setAlert(alert)
								.addPlatformNotification(
										AndroidNotification.newBuilder().addExtras(extras).build())
								.addPlatformNotification(
										IosNotification.newBuilder().addExtras(extras).build())
								.build())
				.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
	}
	/**
	 * 客户端 给平台的一个或者一组标签发送消息。
	 */
	public static void sendTag(String message, String messageId, String type, List<String> tagsList)
	{
		JPushClient jpushClient = new JPushClient("290fd77fa503ebcef8112857",
				"e1fef546e12c29c72bf8feef");
		// 附加字段
		Map<String, String> extras = new HashMap<String, String>();
		extras.put("messageId", messageId);
		extras.put("typeId", type);

		PushPayload payload = allPlatformAndTag(message, extras, tagsList);
		try
		{
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
		}
		catch (APIConnectionException e)
		{
			System.out.println(e);
		}
		catch (APIRequestException e)
		{
			System.out.println(e);
			System.out.println("Error response from JPush server. Should review and fix it. " + e);
			System.out.println("HTTP Status: " + e.getStatus());
			System.out.println("Error Code: " + e.getErrorCode());
			System.out.println("Error Message: " + e.getErrorMessage());
			System.out.println("Msg ID: " + e.getMsgId());
		}
	}

	/**
	 * 极光推送:生成向一组标签进行推送的消息。
	 */
	private static PushPayload allPlatformAndTag(String alert, Map<String, String> extras,
			List<String> tagsList)
	{

		return PushPayload
				.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(Audience.tag(tagsList))
				.setNotification(
						Notification
								.newBuilder()
								.setAlert(alert)
								.addPlatformNotification(
										AndroidNotification.newBuilder().addExtras(extras).build())
								.addPlatformNotification(
										IosNotification.newBuilder().addExtras(extras).build())
								.build())
				.setOptions(Options.newBuilder().setApnsProduction(true).build()).build();
	}

	public static void main(String[] args)
	{
		// new PushUtil().sendAll("这是java后台发送的一个通知。。。。");
//		List<String> sendAlias = new ArrayList<>();
//		sendAlias.add("1001");
//		new PushUtil().sendAlias("这是java后台发送的一个按照alia的通知", sendAlias);
		
		new PushUtil().sendAllMessage("这是后台发送的透传消息");
	}
}




更加细节的部分参考 官网文档  https://docs.jiguang.cn/jpush/server/3rd/java_sdk/



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值