Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send push notification GCM by java [closed]

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?

like image 583
user3302527 Avatar asked Feb 18 '14 12:02

user3302527


People also ask

Can I send push notifications without FCM?

How it is possible? It's definitely possible -- you don't have to use Firebase to deliver push notifications.

Is GCM and FCM same?

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.

What is GCM push notification?

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 ...


1 Answers

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;
like image 98
sinujohn Avatar answered Oct 11 '22 15:10

sinujohn