JPush极光推送个人理解

本文档详细介绍了如何使用极光推送Java SDK进行消息推送,包括针对不同目标(如别名、标签)的消息定制及统计查询等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人代码例子
package com.lchy.xwx.mq.common.Jdpush;

import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lchy.xwx.mq.util.ReadConfigUtil;
import cn.jpush.api.JPushClient;
import cn.jpush.api.common.TimeUnit;
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.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;
import cn.jpush.api.push.model.notification.WinphoneNotification;
import cn.jpush.api.report.ReceivedsResult;
import cn.jpush.api.report.ReceivedsResult.Received;
import cn.jpush.api.report.UsersResult;

public class Jdpush {
	protected static final Logger log = LoggerFactory.getLogger(Jdpush.class);

	// demo App defined in resources/jpush-api.conf
	private static final ReadConfigUtil config = ReadConfigUtil.getInstance();
	private static final String APPKEY = config.getValue("jpush.appkey");
	private static final String MASTERSECRET = config.getValue("jpush.mastersecret");
	private static final String DAY = config.getValue("jpush.offlineday");
	public static JPushClient jpushClient = null;

	/**
	 * 推送通知接口
	 * @param alias 别名
	 * @param tags tag数组
	 * @param title 推送标题
	 * @param btype 推送类型
	 * @param content 推送内容
	 */
	public static void sendPushNotice(String alias, String[] tags, String title, String btype, String content) {
		jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));
		PushPayload payload = null;
		// 生成推送的内容,这里我们先测试全部推送
		// 通知提示信息
		if (content != null) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("btype", btype);
			// 根据别名推送
			if (alias != null && tags == null) {
				payload = buldPushObject_all_all_alias(alias, title, content, map);
			} else if (alias == null && tags != null) { // 根据tag[]推送
				payload = buldPushObject_all_all_tag(tags, title, content, map);
			} else if (alias != null && tags != null) { // 别名和tags[] 推送通知
				payload = buldPushObject_all_all_aliasAndTag(alias, tags, title, content, map);
			} else if (alias == null && tags == null) {
				payload = buldPushObject_all_all(title, content, map);
			}
		} else {
			log.info("No notification - " + content);
		}
		try {
			System.out.println(payload.toString());
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result.msg_id+ "................................");
			log.info("Got result - " + result);
		} catch (APIConnectionException e) {
			log.error("Connection error. Should retry later. ", e);
		} catch (APIRequestException e) {
			log.error("Error response from JPush server. Should review and fix it. ", e);
			log.info("HTTP Status: " + e.getStatus());
			log.info("Error Code: " + e.getErrorCode());
			log.info("Error Message: " + e.getErrorMessage());
			log.info("Msg ID: " + e.getMsgId());
		}
	}

	/**
	 * 推送自定义消息接口.根据别名修改标签(tag)
	 * @param alias 别名
	 * @param content 推送内容
	 */
	public static void sendPushMessage(String alias, String content) {
		jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));
		PushPayload payload = null;
		// For push, all you need do is to build PushPayload object.
		// PushPayload payload = buildPushObject_all_all_alert();
		// 判断用户别名和tag不为空的情况下才推送修改标签(tag)
		if (content != null && alias != null) {
			payload = PushPayload.newBuilder()
					.setAudience(Audience.alias(alias))
					.setPlatform(Platform.all())
					.setMessage(Message.content(content)).build();
		} else {
			log.info("No notification - " + content);
		}
		try {
			System.out.println(payload.toString());
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result + "................................");
			log.info("Got result - " + result);
		} catch (APIConnectionException e) {
			log.error("Connection error. Should retry later. ", e);
		} catch (APIRequestException e) {
			log.error("Error response from JPush server. Should review and fix it. ", e);
			log.info("HTTP Status: " + e.getStatus());
			log.info("Error Code: " + e.getErrorCode());
			log.info("Error Message: " + e.getErrorMessage());
			log.info("Msg ID: " + e.getMsgId());
		}
	}

	/**
	 * 查询记录推送成功条数
	 * @param mid
	 */
	public static void countPush(String mid) {
		jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));
		PushPayload payload = null;
		try {
			ReceivedsResult result = jpushClient.getReportReceiveds(mid);
			Received received = result.received_list.get(0);
			System.out.println("android_received:" + received.android_received
					+ "\nios:" + received.ios_apns_sent);
			log.debug("Got result - " + result);
		} catch (APIConnectionException e) {
			// Connection error, should retry later
			log.error("Connection error, should retry later", e);
		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			log.error("Should review the error, and fix the request", e);
			log.info("HTTP Status: " + e.getStatus());
			log.info("Error Code: " + e.getErrorCode());
			log.info("Error Message: " + e.getErrorMessage());
		}
	}

	/**
	 * 统计用户数据。需要vip用户才能访问
	 */
	public static void getReportUser() {
		jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));
		PushPayload payload = null;
		try {
			UsersResult result = jpushClient.getReportUsers(TimeUnit.DAY, "2015-04-28", 8);
			// Received received =result
			// System.out.println("android_received:"+received.android_received+"\nios:"+received.ios_apns_sent);
			log.debug("Got result - " + result);
		} catch (APIConnectionException e) {
			// Connection error, should retry later
			log.error("Connection error, should retry later", e);
		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			log.error("Should review the error, and fix the request", e);
			log.info("HTTP Status: " + e.getStatus());
			log.info("Error Code: " + e.getErrorCode());
			log.info("Error Message: " + e.getErrorMessage());
		}
	}

	/**
	 * 根据别名通知推送
	 * @param alias 别名
	 * @param alert 推送内容
	 * @return
	 */
	public static PushPayload buldPushObject_all_all_alias(String alias, String title, String content, Map<String, String> map) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.alias(alias))
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										IosNotification.newBuilder()
												.setAlert(content)
												.addExtras(map).build())
								.addPlatformNotification(
										AndroidNotification.newBuilder()
												.setAlert(content)
												.setTitle(title).addExtras(map)
												.build())
								.addPlatformNotification(
										WinphoneNotification.newBuilder()
												.setAlert(content)
												.addExtras(map).build())
								.build()).build();
	}

	/**
	 * 根据tag通知推送
	 * @param alias 别名
	 * @param alert 推送内容
	 * @return
	 */
	public static PushPayload buldPushObject_all_all_tag(String[] tags, String title, String content, Map<String, String> map) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.tag(tags))
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										IosNotification.newBuilder()
												.setAlert(content)
												.addExtras(map).build())
								.addPlatformNotification(
										AndroidNotification.newBuilder()
												.setAlert(content)
												.setTitle(title).addExtras(map)
												.build())
								.addPlatformNotification(
										WinphoneNotification.newBuilder()
												.setAlert(content)
												.addExtras(map).build())
								.build()).build();
	}

	/**
	 * 根据tag通知推送 
	 * @param alias  别名
	 * @param alert  推送内容
	 * @return
	 */
	public static PushPayload buldPushObject_all_all_aliasAndTag(String alias, String[] tags, String title, String content, Map<String, String> map) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.alias(alias))
				.setAudience(Audience.tag(tags))
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										IosNotification.newBuilder()
												.setAlert(content)
												.addExtras(map).build())
								.addPlatformNotification(
										AndroidNotification.newBuilder()
												.setAlert(content)
												.setTitle(title).addExtras(map)
												.build())
								.addPlatformNotification(
										WinphoneNotification.newBuilder()
												.setAlert(content)
												.addExtras(map).build())
								.build()).build();
	}

	/**
	 * 根据通知推送
	 * @param alias 别名
	 * @param alert 推送内容
	 * @return
	 */
	public static PushPayload buldPushObject_all_all(String title, String content, Map<String, String> map) {
		return PushPayload
				.newBuilder()
				.setPlatform(Platform.all())
				.setAudience(Audience.all())
				.setNotification(
						Notification
								.newBuilder()
								.addPlatformNotification(
										IosNotification.newBuilder()
												.setAlert(content)
												.addExtras(map).build())
								.addPlatformNotification(
										AndroidNotification.newBuilder()
												.setAlert(content)
												.setTitle(title).addExtras(map)
												.build())
								.addPlatformNotification(
										WinphoneNotification.newBuilder()
												.setAlert(content)
												.addExtras(map).build())
								.build()).build();
	}

}

MASTERSECRET、APPKEY //在Jpush申请应用时产生的

Integer.valueOf(DAY)  //离线天数

jpushClient = new JPushClient(MASTERSECRET, APPKEY, Integer.valueOf(DAY));  //创建jpush对象


PushPayload
                .newBuilder()
                .setPlatform(Platform.all())   //推送设备类型,比如android、ios、Winphone等设备
                .setAudience(Audience.alias(alias))  //设备的别名
                .setNotification(     //推送通知消息的几种设备中,需要提供标题(title),内容(content),以及附加字段(map)。
                        Notification
                                .newBuilder()
                                .addPlatformNotification(
                                        IosNotification.newBuilder()
                                                .setAlert(content)
                                                .addExtras(map).build())
                                .addPlatformNotification(
                                        AndroidNotification.newBuilder()
                                                .setAlert(content)
                                                .setTitle(title).addExtras(map)
                                                .build())
                                .addPlatformNotification(
                                        WinphoneNotification.newBuilder()
                                                .setAlert(content)
                                                .addExtras(map).build())
                                .build()).build();


极光api


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值