极光推送(给手机端推送消息)

一、注册应用

1.去极光推送开发者服务网站注册账号

https://www.jiguang.cn/

2.注册完毕,登陆后创建应用

3.创建完毕获取应用信息

获取到appKey和masterSecret

*appKey是极光平台应用的唯一标识

*masterSecret是用于服务器端API调用时与appKey配合使用达到鉴权的目的


4.下载服务器端SDK(jar)

jiguang-common-1.0.8.jar

jpush-client-3.3.3.jar

gson-2.2.4.jar

log4j-1.2.17.jar

slf4j-api-1.7.7.jar

slf4j-log4j12-1.7.7.jar

将下载的jar包放入到lib下

后台Java实现(代码)

package sdcncsi.ict.app.push;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.jiguang.common.ClientConfig;
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.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 LSH
 * @date 2018-5-11
 */
public class JPush {

	private static final Log log = LogFactory.getLog(JPush.class);

	private static final String appKey = "d1007969827bf01a77a1111";
	private static final String masterSecret = "63fe786a6aae23d37022222";

	/**
	 * 消息推送类型:专家消息
	 */
	public static final int JPUSH_TYPE_ALERT = 1;
	/**
	 * 消息推送类型:投诉消息
	 */
	public static final int JPUSH_TYPE_INFO = 2;
	/**
	 * 消息推送类型:通知公告
	 */
	public static final int JPUSH_TYPE_NOTICE = 3;

	/**
	 * 推送服务
	 * 
	 * @param appId 专家ID【别名(alias)】
	 * @param alertid 下发 id
	 * @param alert 标题(问题类型,问题描述)
	 */
	public static void sendPush(String appId, String alertid, String alert) {
		Map extra = new HashMap();
		extra.put("alertid", alertid);

		String title = "专家消息";
		JPushClient jpushClient = new JPushClient(masterSecret, appKey);
		// maxRetryTime:最大的尝试次数,设为3表示:跟服务器进行建立连接若失败会尝试再进行两次尝试
//		JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.alias(appId))
				.setNotification(Notification.android(alert, title, extra))
				.build();

		try {
			PushResult result = jpushClient.sendPush(payload);
		} catch (Exception e) {
			log.error("Connection error. Should retry later. ", e);

			// } catch (Exception 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("policeId: " + policeId);
			// log.info("alertid: " + alertid);
			// log.info("alert: " + alert);
			// log.info("title: " + title);
		}
	}

	public static PushPayload buildPushObject_all_alias_alert() {
		return PushPayload.newBuilder().setPlatform(Platform.android())// 设置接受的平台
				.setAudience(Audience.all())// Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
				.setNotification(Notification.android("ssssss第一条测试消息", null, null)).build();
	}

	/**
	 * 确定推送消息的目标,包括推送的平台(Android、IOS)、消息内容和目标(所有人、别名、标签),
	 * 构建简单的推送对象:向所有平台,所有人,推送内容为 content 的通知。
	 * 
	 * @param content
	 * @return
	 */
	public static PushPayload buildPushObject_all_all_alert(String content) {
		return PushPayload.alertAll(content);
	}

	/**
	 * 构建推送对象:所有平台,推送目标是别名为alias,通知内容为 content。
	 * 
	 * @param alias
	 * @param content
	 * @return
	 */
	public static PushPayload buildPushObject_all_alias_alert(String alias, String content) {
		return PushPayload.newBuilder().setPlatform(Platform.all())// 所有平台:Platform.all();Platform.android();Platform.android_ios()
				.setAudience(Audience.alias(alias))// 向选定的人推送(alias也可以是一个集合Set alias)
				.setNotification(Notification.alert(content))// 消息内容
				.build();
	}

	/**
	 * 构建推送对象:向android平台,向目标标签tag,通知标题title,内容为 content。
	 * 
	 * @param alias
	 * @param title
	 * @param content
	 * @return
	 */
	public static PushPayload buildPushObject_android_tag_alertWithTitle(String alias, String title, String content) {
		return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.tag("tag"))// Audience.tag_and("tag1""tag_all")向指定的组推送
				.setNotification(Notification.android("alert", title, null)).build();
	}

	/**
	 * Android和IOS
	 * @return
	 */
	public static PushPayload buildPushObject_android_and_ios() {
		Map extras = new HashMap();
		extras.put("test", "https://community.jiguang.cn/push");
		return PushPayload.newBuilder().setPlatform(Platform.android_ios())
				.setAudience(Audience.all())
				.setNotification(Notification.newBuilder().setAlert("alert content")
					.addPlatformNotification(AndroidNotification.newBuilder().setTitle("Android Title").addExtras(extras).build())
					.addPlatformNotification(IosNotification.newBuilder().incrBadge(1).addExtra("extra_key", "extra_value").build())
					.build())
				.build();
	}

	public static void main(String[] arg) {
		JPush.sendPush("2018041816030928313455", "12345", "信息11");
	}
}




https://blog.csdn.net/gebitan505/article/details/46812841
https://blog.csdn.net/gebitan505/article/details/46812879

1.下载服务端SDK

你可能感兴趣的:(java,APP)