相关资料
极光推送文档地址:https://docs.jiguang.cn/jpush/guideline/intro/
极光推送项目开源地址:https://github.com/jpush
错误代码
cn.jiguang.common.resp.APIRequestException: {"error":{"code":1004,"message":"app_key does not exist"}}
at cn.jiguang.common.connection.NativeHttpClient._doRequest(NativeHttpClient.java:234) ~[jiguang-common-1.0.3.jar:na]
at cn.jiguang.common.connection.NativeHttpClient.doRequest(NativeHttpClient.java:99) ~[jiguang-common-1.0.3.jar:na]
at cn.jiguang.common.connection.NativeHttpClient.sendPost(NativeHttpClient.java:86) ~[jiguang-common-1.0.3.jar:na]
at cn.jpush.api.push.PushClient.sendPush(PushClient.java:163) ~[jpush-client-3.2.17.jar:na]
at cn.jpush.api.JPushClient.sendPush(JPushClient.java:194) ~[jpush-client-3.2.17.jar:na]
at com.ylz.packcommon.common.util.JiguangPushUtils.push_cyfwdwMsg(JiguangPushUtils.java:197) ~[classes/:na]
at com.ylz.task.async.MsgPhoneAsyncBean.push_cyfwdwMsg(MsgPhoneAsyncBean.java:80) [classes/:na]
at com.ylz.task.async.MsgPhoneAsyncBean$$FastClassBySpringCGLIB$$310b2778.invoke(<generated>) [classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) [spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:108) [spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_66]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_66]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_66]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_66]
根据以上的代码可以看出app_key这个不存在,在调用极光推送接口时,会实例化JPushClient这个构造函数,传入masterSecret和appKey这两个参数:
public JPushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
this._pushClient = new PushClient(masterSecret, appKey, proxy, conf);
this._reportClient = new ReportClient(masterSecret, appKey, proxy, conf);
this._deviceClient = new DeviceClient(masterSecret, appKey, proxy, conf);
this._scheduleClient = new ScheduleClient(masterSecret, appKey, proxy, conf);
}
在极光开发者平台上,极光会提供masterSecret和appKey两个值,在实例化构造函数的时候,将两个参数分别赋值为对应的值即可解决该问题。
以下是调用极光推送的方法:
private static final Logger log = LoggerFactory.getLogger(JiguangPushUtils.class);
/**
* 极光推送方法(采用java SDK)
* @param alias
* @param alert
* @return PushResult
*/
public static PushResult push_cyfwdwMsg(String alias, String alert, String content){
try {
ClientConfig clientConfig = ClientConfig.getInstance();
JPushClient jpushClient = new JPushClient(PropertiesUtil.getConfValue("MasterSecret"),
PropertiesUtil.getConfValue("AppKey"), null, clientConfig);
PushPayload payload = buildPushObject_android_ios_alias_alert(alias,alert,content);
if(payload != null){
return jpushClient.sendPush(payload);
}
return null;
} catch (APIConnectionException e) {
log.error("Connection error. Should retry later. ", e);
return null;
} catch (APIRequestException e) {
log.error("Error response from JPush server. Should review and fix it. ", e);
return null;
} catch (Exception e) {
log.error("Error response from JPush server. Should review and fix it. ", e);
return null;
}
}
/**
* 生成极光推送对象PushPayload(采用java SDK)
* @param alias
* @param alert
* @return PushPayload
*/
public static PushPayload buildPushObject_android_ios_alias_alert(String alias, String alert, String content){
boolean result = true;
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.addPlatformNotification(AndroidNotification.newBuilder()
.addExtra("content", content)
.setAlert(alert)
.build())
.addPlatformNotification(IosNotification.newBuilder()
.addExtra("content", content)
.setAlert(alert)
.setSound("default")
.incrBadge(1)
.build())
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)
.setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数)
.build())
.build();
}
参数介绍:
- alias ----接收极光消息的用户账号
- alert ----发送的内容(会显示APP端的通知栏)
- content----消息对象的JSON形式