Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Send Push Notification to Android Devices from Java Server Side

    • 0
    • 2
    • 1
    • 4
    • 0
    • 0
    • 0
    • 0
    • 6.77k
    Comment on it

    This blog will help you to send Push Notifications to Android devices from server side where server side code is written in Java.

    Follow the below steps in order to send Push Notification:

    1. Put the gcm-server.jar into your lib folder of your paroject.

    2. Send Push Notification to a single device:

    Write the following metthod to send Pust Notification to an Android device:

    private String sendNotification(String message, String appId)
    {
        Sender sender = new Sender("AIzaSyCz2bnoLP-TzzpS-7AQNSYO87icMwpoj_g"); // Here you will write APP key given by Android end
        Message msg = new Message.Builder().addData("message", message).build();
    
        String str=null;
        try 
        {
            Result results = sender.send(msg, appId, 5); // Where appId is given by Android end
            if(results.getMessageId() != null)
            {
                str = val_true;
            }
            else
            {
                str= val_false;
                String error = results.getErrorCodeName();
                logger.info("message sending failed:: "+error);
                if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                }
            }
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        return str;
    }
    

    3. Send Push Notification to multiple devices:

    private String sendMulptipleNotifiaction(String message, ArrayList devices)
    {
        Sender sender = new Sender("AIzaSyCz2bnoLP-TzzpS-7AQNSYO87icMwpoj_g");// Here you will write APP key given by Android end
        Message msg = new Message.Builder().addData("message", message).build();
        String str=null;
        try 
        {
            MulticastResult result = sender.send(msg, devices, 5); // where devices is the list of multiple device AppId's
            for (Result r : result.getResults()) {
                if (r.getMessageId() != null) {
                    str = val_true;
                }
                else
                {
    
                    str= val_false;
                    String error = r.getErrorCodeName();
                    if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                    }
                }
            } 
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    
        return str;
    }

    Please feel free to share the experiences you had while sending Push Notifications to an Android Device from Java Server side as this will help other people who read the post.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: