极光服务调用推送接口报错:"app_key does not exist"

相关资料

极光推送文档地址: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();
    }

参数介绍:

  1. alias ----接收极光消息的用户账号
  2. alert ----发送的内容(会显示APP端的通知栏)
  3. content----消息对象的JSON形式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值