Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java com.google.android.gcm.server.InvalidRequestException:发送gcm消息时的HTTP状态代码:401_Java_Google Cloud Messaging - Fatal编程技术网

Java com.google.android.gcm.server.InvalidRequestException:发送gcm消息时的HTTP状态代码:401

Java com.google.android.gcm.server.InvalidRequestException:发送gcm消息时的HTTP状态代码:401,java,google-cloud-messaging,Java,Google Cloud Messaging,我一直在尝试从我的服务器API向android设备发送GCM消息。但是得到401无效的请求例外。使用的代码是: public static Map<String, Object> prepareGCMMessage(String loginUserName, String loginUserPhone, String destinationPhone){ Map<

我一直在尝试从我的服务器API向android设备发送GCM消息。但是得到401无效的请求例外。使用的代码是:

public static Map<String, Object> prepareGCMMessage(String loginUserName, String loginUserPhone,                                                       String destinationPhone){

    Map<String, Object> messageMap = new HashMap<String, Object>();

    String message= generateInviteMessage(loginUserName,loginUserPhone);

    messageMap.put(ApplicationConstants.TEXT_MESSAGE, message);
    messageMap.put(ApplicationConstants.SOURCE_NUMBER, loginUserPhone);
    messageMap.put(ApplicationConstants.TO_NUMBER, destinationPhone);

    return messageMap;
}

public static Boolean sendGCMNotification(Map<String, Object> messageMap, String contactDeviceId) {

    Sender sender = new Sender(ApplicationConstants.GCM_APP_KEY);

    Message.Builder messageBuilder = new Message.Builder();

    for (Map.Entry<String, Object> messageEntry : messageMap.entrySet()) {
        messageBuilder.addData(messageEntry.getKey(), String.valueOf(messageEntry.getValue()));
    }

    Message notificationMessage = messageBuilder.build(); //add timeToLive here

    try {
        Result result = sender.send(notificationMessage, contactDeviceId, 3);

        if (StringUtils.isEmpty(result.getErrorCodeName())) {
            LOGGER.info( "GCM Notification is sent successfully to android for device Id: {0}", contactDeviceId);
            return true;
        }
        LOGGER.info( "Error occurred while sending push notification: {0}", result.getErrorCodeName());

    }
    catch (InvalidRequestException e) {
        LOGGER.error("Invalid Request: {0}", e);
    }
    catch (IOException e) {
        LOGGER.error("IO Exception: {0}", e);
    }
    return false;
}
publicstaticmap prepareGCMMessage(字符串loginUserName、字符串loginUserPhone、字符串destinationPhone){
Map messageMap=newhashmap();
字符串消息=generateInviteMessage(loginUserName、loginUserPhone);
messageMap.put(ApplicationConstants.TEXT_消息,消息);
messageMap.put(ApplicationConstants.SOURCE_编号,loginUserPhone);
messageMap.put(ApplicationConstants.TO_号码,destinationPhone);
返回消息映射;
}
公共静态布尔sendGCMNotification(映射消息映射,字符串contactDeviceId){
发送者发送者=新发送者(ApplicationConstants.GCM\u应用程序密钥);
Message.Builder messageBuilder=new Message.Builder();
对于(Map.Entry messageEntry:messageMap.entrySet()){
messageBuilder.addData(messageEntry.getKey(),String.valueOf(messageEntry.getValue());
}
Message notificationMessage=messageBuilder.build();//在此处添加timeToLive
试一试{
结果=发送者.send(notificationMessage,contactDeviceId,3);
if(StringUtils.isEmpty(result.getErrorCodeName())){
info(“GCM通知已成功发送到android,设备Id:{0}”,contactDeviceId);
返回true;
}
info(“发送推送通知时出错:{0}”,result.getErrorCodeName());
}
捕获(无效请求异常e){
错误(“无效请求:{0}”,e);
}
捕获(IOE异常){
错误(“IO异常:{0}”,e);
}
返回false;
}

401响应表示请求未经授权。是的,伙计..但这里的应用程序密钥是正确的..这里的请求有什么问题?发布您的
发件人
代码,如果使用
HttpURLConnection
,您需要调用
urlConnection.setRequestProperty(“Authorization”,“key=“+API\u key”)以上代码是发送消息的代码。Sender类来自goggle api。导入内容为:导入com.google.android.gcm.server.InvalidRequestException;导入com.google.android.gcm.server.Message;导入com.google.android.gcm.server.Result;导入com.google.android.gcm.server.Sender;反应体有什么东西吗?