推送的原理其实也很简单,拿极光推送来说,首先,APP登录的时候,调用极光的API,设置当前登录的信息到极光;服务器发送推送时,只要将需要推送的信息和相应的唯一标识传给极光,就可以了。
附上服务器端简单代码:
public static void testSendPush(String appKey ,String masterSecret,
String jsonString,String alias) {
try {
MSG_CONTENT = jsonString;
jpushClient = new JPushClient(masterSecret, appKey, 3);
PushPayload payload = buildPushObject_android_and_ios(jsonString, alias);
PushResult result = jpushClient.sendPush(payload);
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_android_and_ios(String jsonString,String alias) {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.newBuilder()
.addAudienceTarget(AudienceTarget.alias(alias))
.build())
.setMessage(Message.newBuilder()
.setMsgContent(jsonString)
.addExtra("from", "JPush")
.build())
.build();
}