Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send push notifications to Android

I am currently developing Java web services that run on WebLogic on a server. These web services are called by a mobile application, which is developed by another team. There is a requirement that I need to send push notifications to the mobile application. My current development does not require me to do any mobile development since I am only doing server side development. I don't have experience in mobile development either. How do go about developing push notifications to both Android devices?

Thanks.

like image 248
user3573403 Avatar asked May 09 '16 02:05

user3573403


People also ask

Can you send push notifications?

Web push notifications have a wider reach than mobile app notifications. You don't require a mobile application to send notifications to the user. You can send them notifications on computers, laptops, and mobile browsers. For mobile push notifications, the user has to install the app to receive messages.

Can you send mobile push notifications without an app?

Pushed allows you to send real-time notifications without developing your own app to iOs, Android and Desktop devices.


1 Answers

Prerequisite for GCM Application

  1. Google API Server Key
  2. GCM RegId of the Android Device to communicate via GCM

If you get clear concept about GCM, please visit here

Your hosted server need not need any Ip/HostName to send Message cause this message will deliberate via com.google.android.gcm.server.Sender this class. This class will internally communicate to GCM Server. You are configuring this class using this way:

Sender sender = new Sender(API_KEY);

You can send message using GCM server. This code will work for sending push notification to devices. This can send notification to any Android/IOS apps from java server.

Here is the jar of this GCM server library.

Here, this constructor get MESSAGE and deviceGcmId where have to send Push Message.

public PushNotification(String id, String message) {
            this.receiverID = id;
            this.gcmMessage = message;
        }

Here is sample code:

import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

public class PushNotification {
    private String receiverID;
    private String gcmMessage;
    String MESSAGE_KEY = "YOUR_MESSAGE_KEY";
    String API_KEY = "YOUR_API_KEY";

    Result result;
    Sender sender;
    Message message;
    MulticastResult multicastResult;

    public PushNotification() {
    }

    public PushNotification(String id, String message) {
        this.receiverID = id;
        this.gcmMessage = message;
    }

    public boolean sendSinglePushNotification() {
        try {
            sender = new Sender(API_KEY);
            message = new Message.Builder().timeToLive(30).addData(MESSAGE_KEY, gcmMessage).build();
            result = sender.send(message, receiverID, 1);
            if (result != null && result.getErrorCodeName() == null) {
                return true;
            }
        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
        return false;
    }
}

You can call this class using this way:

PushNotification notification=new PushNotification("DEVICE_GCM_ID","MESSAGE HAVE To SEND");    
notification.sendSinglePushNotification();

That's it :)

like image 165
Md. Sajedul Karim Avatar answered Sep 17 '22 10:09

Md. Sajedul Karim