这是我的第一个博客文章!!!
原文:
1.下面这个是关于后台集成极光JPushClient的源码(来自极光官网);
1.1:极光推送的关键jpushClient = new JPushClient(masterSecret, appKey, 3);就是这个类。其中的参数(masterSecret和appKey)需要我们从极光官网注册开发者来获得,
import java.io.IOException;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.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.Notification;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JPush {
protected static final Logger LOG = LoggerFactory.getLogger(JPush.class);
// demo App defined in resources/jpush-api.conf
public static final String TITLE = "阿瑞王";
public static final String ALERT = "今天是情人节";
public static final String MSG_CONTENT = "2017/2/14";
public static final String REGISTRATION_ID = "**********";
public static final String TAG = "tag_api";
public static JPushClient jpushClient=null;
public static void testSendPush(String appKey ,String masterSecret) {
jpushClient = new JPushClient(masterSecret, appKey);
// HttpProxy proxy = new HttpProxy("localhost", 3128);
// Can use this https proxy: https://github.com/Exa-Networks/exaproxy
// For push, all you need do is to build PushPayload object.
//PushPayload payload = buildPushObject_all_all_alert();
//生成推送的内容,这里我们先测试全部推送
PushPayload payload=buildPushObject_all_alias_alert();
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());
}
}
public static PushPayload buildPushObject_all_all_alert() {
return PushPayload.alertAll(ALERT);
}
public static PushPayload buildPushObject_all_alias_alert() {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
.setNotification(Notification.alert(ALERT))
.build();
}
public static PushPayload buildPushObject_android_tag_alertWithTitle() {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.all())
.setNotification(Notification.android(ALERT, TITLE, null))
.build();
}
public static PushPayload buildPushObject_android_and_ios() {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.tag("tag1"))
.setNotification(Notification.newBuilder()
.setAlert("alert content")
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle("Android Title").build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.addExtra("extra_key", "extra_value").build())
.build())
.build();
}
public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
return PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.tag_and("tag1", "tag_all"))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(ALERT)
.setBadge(5)
.setSound("happy")
.addExtra("from", "JPush")
.build())
.build())
.setMessage(Message.content(MSG_CONTENT))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
}
public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.newBuilder()
.addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
.addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
.build())
.setMessage(Message.newBuilder()
.setMsgContent(MSG_CONTENT)
.addExtra("from", "JPush")
.build())
.build();
}
}
1.2:servlet很简单,只要传入两个key值,调用该方法就可以
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ;
/**
* Servlet implementation class TuiSong
*/
@WebServlet("/TuiSong")
public class TuiSong extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String appKey ="718a2255bcca83d50305c***";// appKey 可以在极光官网上注册申请到
private static final String masterSecret = "2e04dc982bd94e3e47e87***";// masterSecret 可以在极光官网上注册申请到
/**
* @see HttpServlet#HttpServlet()
*/
public TuiSong() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
JPush.testSendPush(appKey,masterSecret);
System.out.println("sucess");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
摘要
我的其他文章:**