I have configured a client android Google Cloud Messaging (GCM) to receive push notifications, but I can not configure a server in java to send notifications to devices. How could I?
How it is possible? It's definitely possible -- you don't have to use Firebase to deliver push notifications.
Firebase Cloud Messaging (FCM), formerly known as Google Cloud Messaging (GCM), is a cross-platform cloud solution for messages and notifications for Android, iOS, and web applications, which as of June 2022 can be used at no cost.
Google Cloud Messaging (GCM) was a mobile notification service developed by Google that enables third-party application developers to send notification data or information from developer-run servers to applications that target the Google Android Operating System, as well as applications or extensions developed for the ...
You can use gcm-server.jar which contains helper methods for GCM messaging.
To get this jar you can install "[Deprecated]Google Cloud Messaging for Android Library" through Android SDK Manager. Don't let the deprecated name confuse you. Only the client part is deprecated, not server side.
After install you can find it at "ADT_SDKROOT\sdk\extras\google\gcm"
. The sample folder contains a demo server which is very easy to understand.
Sending a GCM message involves only few lines of code:
final String GCM_API_KEY = "yourKey";
final int retries = 3;
final String notificationToken = "deviceNotificationToken";
Sender sender = new Sender(GCM_API_KEY);
Message msg = new Message.Builder().build();
try {
Result result = sender.send(msg, notificationToken, retries);
if (StringUtils.isEmpty(result.getErrorCodeName())) {
logger.debug("GCM Notification is sent successfully");
return true;
}
logger.error("Error occurred while sending push notification :" + result.getErrorCodeName());
} catch (InvalidRequestException e) {
logger.error("Invalid Request", e);
} catch (IOException e) {
logger.error("IO Exception", e);
}
return false;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With