客户器端集成
1、注册app,生成appKey 和 Master Secret
2、导入SDK开发包到你自己的应用程序项目
3、配置 AndroidManifest.xml ,这部分内容太长,官方有明确说明,就不粘贴了
主要步骤为:
(1)复制备注为 "Required" 的部分
(2)将备注中所有 您应用的包名 的部分,替换为当前应用程序的包名
(3)将appKey替换为注册时的appKey
4、新建一个Application,如MyApp,重写onCreate(),进行参数初始化
public void onCreate()
{
super.onCreate();
JPushInterface.setDebugMode(true);
JPushInterface.init(this);
}
5、AndroidManifest.xml中配置上面的application,即:
<application android:name="com.abc.MyApp">
官方参考:http://docs.jpush.io/guideline/android_guide/
服务器端集成
极光推送默认有个管理后台,但是需要每次登录到它的网站进行推送,觉得不是很方便,所以按照官方文档集成一个简单的服务器端,步骤如下:
1、下载服务端SDK,准备好如下jar包,一个不能少
2、封装消息和推送方法
MessagePush.java
/** * 封装消息推送方法 */
public class MessagePush {
private static final String appKey = "你申请的appkey";
private static final String masterSecret = "你申请时生成的masterSecret ";
private JPushClient jpushClient;
private String title;//推送的标题
private String message;//推送的内容
public MessagePush(String message) {
this.message = message;
//创建JPushClient
jpushClient = new JPushClient(masterSecret, appKey, 3);
}
public MessagePush(String message, String title) {
this(message);
this.title = title;
}
/**
* 向所有人发送消息
*
* @return 消息id
*/
public long sendPushAll() {
//构建一个PushPayload对象(推送对象)
PushPayload payload = buildPushObject_all_all_alert();
long msgId = 0;
try {
//推送消息
PushResult result = jpushClient.sendPush(payload);
msgId = result.msg_id;
} catch (APIConnectionException e) {
} catch (APIRequestException e) {
msgId = e.getMsgId();
}
return msgId;
}
/**
* 向指定别名的客户端发送消息
*
* @param alias
* 所有别名信息集合,这里表示发送所有学生编号
* @return 消息id
*/
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) {
} catch (APIRequestException e) {
msgId = e.getMsgId();
}
return msgId;
}
/**
* 向指定组发送消息
*
* @param tag
* 组名称
* @return 消息id
*/
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;
} catch (APIConnectionException e) {
} catch (APIRequestException e) {
msgId = e.getMsgId();
}
return msgId;
}
/**
* 封装了三种获得消息推送对象(PushPayload)的方法
*/
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);
}
}
3、模拟服务器端推送,先全部推送,然后按Tag推送
Main.java
public class Main {
public static void main(String[] args) {
// 发送三次网络推送
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 3; i++) {
// MessagePush push = new MessagePush("这是网络推送" + i, "测试"
// + i);
MessagePush push = new MessagePush("这是按Tag进行推送" + i, "测试" + i);
long msgId = push.sendPushTag("ABC");
System.out.println("********" + msgId);
}
}
}).start();
}
}
4、测试效果
- 全部推送
- 按Tag推送