2

I have been trying to send GCM message to the android device from my server API. But getting 401 invalidrequestexception. Code used is:

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;
}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
gaurav
  • 189
  • 2
  • 6
  • 401 response means request is unauthorized. – seenukarthi Sep 10 '15 at 07:07
  • yes buddy..but the app key is correct here..what is wrong in the request here? – gaurav Sep 10 '15 at 07:13
  • Post your `Sender` code, if using `HttpURLConnection`, you need to call `urlConnection.setRequestProperty("Authorization", "key=" + API_KEY);` – BNK Sep 10 '15 at 08:20
  • Above code is the one sending the message. Sender class is from goggle api. imports are: import com.google.android.gcm.server.InvalidRequestException; import com.google.android.gcm.server.Message; import com.google.android.gcm.server.Result; import com.google.android.gcm.server.Sender; – gaurav Sep 10 '15 at 08:36
  • Does the response body have anything? – e4c5 Sep 10 '15 at 09:03
  • Can you run this curl command and tell us its output? `curl --header "Authorization: key=" \ --header Content-Type:"application/json" \ https://gcm-http.googleapis.com/gcm/send \ -d "{\"registration_ids\":[\"ABC\"]}"` – Koh Sep 10 '15 at 16:42
  • Also make sure that your project in the Dev Console has the "Google Cloud Messaging for Android" API enabled. – Koh Sep 10 '15 at 16:42
  • you can refer my answer posted [here](http://stackoverflow.com/a/35055871/3142342). Hope it helps :) – Rahul Ahuja Jan 29 '16 at 10:50
  • Most of the time the Authorization key we are using could be wrong. Check http://dev.tapjoy.com/faq/how-to-find-sender-id-and-api-key-for-gcm/ to see you are using the correct key or not . I also got confused while selecting API key for firebase We have to use SENDER ID - API KEY pair in Cloud messaging tab under firebase setting. i.e. Go to firebase App--> Go to app setting --> Cloud Messaging there you can find Sender Id <==> API key and this API key you can use to send FCM. – Rahul Feb 14 '17 at 08:56

0 Answers0