极光推送服务端向手机app即时推送消息或通知

以前一直在做后台开发,对消息的推送很感兴趣,正好项目中要用到极光推送,今天抽空来记录下这两天的研究成果。

我们知道IOS有自己的推送服务,但很遗憾Android没有原生的推送服务,现在有很多第三方的推送服务,比如个推、极光、亚马逊、百度云、聚能等。今天我们就来研究下极光推送的后台服务器如何实现。

关键点:

        1.首先最好是把极光官网java后台服务器的demo下载下来,里面有我们需要的jar包,以及example.

        2.极光推送的关键jpushClient = new JPushClient(masterSecret, appKey, 3);就是这个类。其中的参数需要我们从极光官网注册开发者,然后创建具体项目获取相应的两个key值。其中appKey值就是我们手机端对应的key值

        3.极光推送给我们提供了很多种推送的方式,我们可以选择某一个平台进行推送(Android ,IOS ,Windows Phone),也可以全部推送;我们可以针对某个特别的用户进行推送(设置alisa),也可以针对特别的群体进行推送(设置tag),第三个参数是设置推送保留的时间,只要在有效时间内上线就可以收到推送信息

        4. 极光推送现在都用https连接,提交请求是post,获取数据为get

// 对android和ios设备发送

JPushClient jpush = new JPushClient(masterSecret, appKey);
 
// 对android和ios设备发送,同时指定离线消息保存时间
JPushClient jpush = new JPushClient(masterSecret, appKey, timeToLive);
 
// 指定某种设备发送
JPushClient jpush = new JPushClient(masterSecret, appKey, DeviceEnum.Android);
 
// 指定某种设备发送,并且指定离线消息保存时间

JPushClient jpush =
new JPushClient(masterSecret, appKey, timeToLive, DeviceEnum.IOS);


package com.test.action;


import java.util.Set;

import org.apache.log4j.Logger;

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.Notification;

public class MessagePush {
	/**
	 * 日志常量
	 */
	private static final Logger logger = Logger.getLogger(MessagePush.class);
	private static final String appKey = "d3a485586264be07f7c00b15";
	private static final String masterSecret = "e0ac721eae1169cf29d1e49a";
	private JPushClient jpushClient ;
	private String title;
	private String message;
	public MessagePush(String message) {
		this.title=title;  
		this.message = message;    
		jpushClient = new JPushClient(masterSecret, appKey,0);
	}

	/**
	 * 向所有人发送消息
	 * @return 消息id 状态码:0为成功
	 */
	public long sendPushAll(){
		PushPayload payload=buildPushObject_all_all_alert();
		long msgId=0;
		try {
			PushResult result=jpushClient.sendPush(payload);
			msgId=result.msg_id;
		} catch (APIConnectionException e) {
			// TODO Auto-generated catch block
			logger.error("Connection error. Should retry later. ", e);
		} catch (APIRequestException e) {
			logger.info("HTTP Status: " + e.getStatus());
			msgId=e.getMsgId();
		}
		return msgId;
	}
	/**
	 * 向指定别名的客户端发送消息
	 * @param alias 所有别名信息集合,这里表示发送所有学生编号
	 * @return 消息id 状态码:0为成功
	 */
	public long sendPushAlias(Set<String> alias){
		PushPayload payloadAlias=buildPushObject_android_alias_alertWithTitle(alias);
		long msgId=0;
		try {
			PushResult result=jpushClient.sendPush(payloadAlias);
			msgId=result.msg_id;

		} catch (APIConnectionException e) {
			logger.error("Connection error. Should retry later. ", e);
		} catch (APIRequestException e) {
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
			logger.info("Msg ID: " + e.getMsgId());
			msgId=e.getMsgId();

		}
		return msgId;
	}
	/**
	 * 向指定组发送消息
	 * @param tag 组名称
	 * @return 消息id  状态码:0为成功   
	 */
	public  long sendPushTag(String tag) {
		PushPayload payloadtag = buildPushObject_android_tag_alertWithTitle(tag);
		long msgId=0;
		try {
			PushResult result = jpushClient.sendPush(payloadtag);
			msgId=result.msg_id;
			logger.info("Got result - " + result);
		} catch (APIConnectionException e) {
			logger.error("Connection error. Should retry later. ", e);

		} catch (APIRequestException e) {
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
			logger.info("Msg ID: " + e.getMsgId());
			msgId=e.getMsgId();

		}
		return msgId;
	}
	
	/**
	 * 
	 * @Description: 向指定devicetoken用户发送消息
	 * @param @param session
	 * @param @return   
	 * @return 消息id 状态码:0为成功 
	 */
	public long sendPushDevicetoken(String session){
		PushPayload payloadAlias=buildPushObject_alertWithTitle(session);
		long msgId=0;
		try {
			PushResult result=jpushClient.sendPush(payloadAlias);
			msgId=result.msg_id;
		} catch (APIConnectionException e) {
			logger.error("Connection error. Should retry later. ", e);
		} catch (APIRequestException e) {
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
			logger.info("Msg ID: " + e.getMsgId());
			msgId=e.getMsgId();

		}
		return msgId;
	}

	/**
	 * 
	 * @Description: 向android和ios用户一一推送消息
	 * @param @param alias
	 * @param @return   
	 * @return PushPayload  
	 */
	public  PushPayload buildPushObject_alertWithTitle(String useId) {
		return PushPayload.newBuilder().setPlatform(Platform.all())
				.setAudience(Audience.alias(useId)).setNotification(Notification.alert(message)).build();
	}

	/**
	 * 下列封装了三种获得消息推送对象(PushPayload)的方法
	 *  buildPushObject_android_alias_alertWithTitle、
	 *  buildPushObject_android_tag_alertWithTitle、
	 *  buildPushObject_all_all_alert
	 */
	public  PushPayload buildPushObject_android_alias_alertWithTitle(Set<String> alias) {
		return PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.alias(alias)).setNotification(Notification.android(message,title,null)).build();
	}

	public  PushPayload buildPushObject_android_tag_alertWithTitle(String tag){
		return PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.tag(tag))
				.setNotification(Notification.android(message, title, null)).build();}

	public  PushPayload buildPushObject_all_all_alert() {
		return PushPayload.alertAll(message);
	}

	/**
	 * @return the jpushClient
	 */
	public JPushClient getJpushClient() {
		return jpushClient;
	}
	/**
	 * @param jpushClient the jpushClient to set
	 */
	public void setJpushClient(JPushClient jpushClient) {
		this.jpushClient = jpushClient;
	}
	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}
	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}
    
}



 测试一下: 
 

    public static void main(String[] args) {
        MessagePush messagePush = new MessagePush("Test","我推送的第一条消息");
        long msgId = messagePush.sendPushDevicetoken(" 53459gfihfgh95459gdf9056");
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值