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;
}