首先自己尝试过自己发http请求,调用极光推消息,虽然消息成功了,但是app收不到,尴尬,还是用官网现成的jar吧
流程原理:
第一是创建一个链接对象, 即 PushClient;
第二是创建推送对象 。即 PushPayload 。
步骤:
第一步导入jar包:
执行main:
package com.test.core.commons.utils;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
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.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
public class JPushClientUtil {
//在极光注册上传应用的 appKey 和 masterSecret
private static final String appKey ="7b6c132dd2xxxxff37bfxxx0";必填,例如466f7032ac604e02fb7bda89
private static final String masterSecret = "284f35c2xxxxc912a74dxxx0";//必填,每个应用都对应一个masterSecret
public static void main(String[] args) throws Exception {
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, ClientConfig.getInstance());
// For push, all you need do is to build PushPayload object.
PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
try {
PushResult result = jpushClient.sendPush(payload);
System.out.println(result.msg_id);
System.out.println(result);
} catch (APIConnectionException e) {
// Connection error, should retry later
//LOG.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
//LOG.error("Should review the error, and fix the request", e);
//LOG.info("HTTP Status: " + e.getStatus());
//LOG.info("Error Code: " + e.getErrorCode());
//LOG.info("Error Message: " + e.getErrorMessage());
}
}
public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
return PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.alias("1647af0e50e4444dbe05b3d7b79dadfd"))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert("大家好,我来推个消息")
.setSound("happy")
.addExtra("issueId", "xxx")
.addExtra("type","我是问题")
.build())
.build())
.setMessage(Message.content("hgg推车了"))
.setOptions(Options.newBuilder()
.setApnsProduction(false)
.build())
.build();
}
}