6

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.

user3573403
  • 1,780
  • 5
  • 38
  • 64
  • Do you have a GCM service setup? – OneCricketeer May 09 '16 at 02:51
  • No, not yet. I don't know about GCM actually. Can we use GCM to send to both Android and iPhone devices? – user3573403 May 09 '16 at 03:00
  • I don't think so. You'll need an APNs for that. One is Google, the other is Apple. Sadly, the push notification service isn't a well defined standard – OneCricketeer May 09 '16 at 03:04
  • If you'd like to manage your own push notification service, then [Parse Server has documentation](https://github.com/ParsePlatform/parse-server/wiki/Push), otherwise AWS can be setup similarly to send push notifications – OneCricketeer May 09 '16 at 03:09
  • I don't understand what you mean by "manage your own push notification service". How is Parse Server different from GCM and APNS? – user3573403 May 09 '16 at 03:27
  • Compared to paying for AWS, you can setup Parse Server on your own hardware (if you have it). Parse is a frontend to MongoDB, but it contains GCM and APNs services – OneCricketeer May 09 '16 at 03:57
  • You can use GCM to send to both Android and ios devices. I have done that before. – sonngaytho May 09 '16 at 04:06

5 Answers5

1

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 :)

Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
  • Hi, two questions: (1) Your sample codes is only applicable for Android? It is said that GCM now supports both Android and iOS, so would your codes be applicable to both or just Android? (2) Your codes don't specify any target server hostname/IP or port of the Google GCM Server. When we send a notification via GCM, how does the GCM API know how to communicate with the Google GCM Server? – user3573403 May 09 '16 at 08:42
  • Hello, See my edited answer. Here this code for server side not for client. using this code, you don't need to know who is client eg, anroid or ios. You just need client GcmId. I added a link where you can get description about GCM server, client communication mechanism. Thanks – Md. Sajedul Karim May 09 '16 at 09:59
  • Hi, your codes didn't specify any hostname/IP of the Google GCM Server. How does it know how to communicate with the Google GCM Server? – user3573403 May 10 '16 at 03:10
  • 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. – Md. Sajedul Karim May 10 '16 at 18:34
  • Hi, there is now FCM, which is an upgrade of GCM. Will your sample code still the same for FCM? And where can I download the jar for FCM? Thanks. – user3573403 May 26 '16 at 11:30
1

You can use Google Cloud Messaging to do this, it now supports Android and iOS. Its free and has no limits to number of notification.

How it works?

Simply, you need to generate Registration ID from your mobile and send it to your server, then when you need to send a notification just send the registration ID and the message to GCM.

Please check this link from Google about GCM:

https://developers.google.com/cloud-messaging/

and this link just take a quick look about server code (written in PHP):

http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

there is some changes you can notice it in first link.

Finally, check this link it explains the client code step by step:

http://www.androidwarriors.com/2015/10/push-notification-using-gcm-in-android.html

I hope this helps you, do not hesitate to ask any question.

Ahmed M. Abed
  • 599
  • 3
  • 9
  • 22
  • Hi, (1) Is the Registration ID applicable only for Android? (2) Are the codes the same for sending notification to Android and iOS? – user3573403 May 09 '16 at 09:04
  • @user3573403 (1) no, Registration ID required and applicable for Android and iOS also because reg ID is the only way to connect your device with GCM. (2) I think you need to use different code, please check this PHP script to send push notification to Android and iOS: https://gist.github.com/joashp/b2f6c7e24127f2798eb2 – Ahmed M. Abed May 09 '16 at 09:49
0

If you want to send Push Notification on multiple platform use Parse.com but problem here is Parse.com is going to stop its services in January 28, 2017. You have alternatives like urban airship and many more. I personally suggest urban airship.

Krishna
  • 343
  • 5
  • 22
0

You can try Sever Sent Event(SSE) from Jersey to see if that meets your requirement.

liweis
  • 61
  • 1
  • 7
0

Send push notifications to Android

 private void pushNotification(String handlename) {
        StringEntity entity = null;
        JSONObject jsonObject = new JSONObject();
        String referesedtoken = FirebaseInstanceId.getInstance().getToken();
        try {
            jsonObject.put("to", clientToken);
            jsonObject.put("priority", "high");
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(referesedtoken);
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("body", handlename);
            jsonObject1.put("commonContact", commonContact);
            jsonObject1.put("profileType", profileType);
            jsonObject1.put("notification-type", "chat");
            jsonObject1.put("target", handlename);
            jsonObject1.put("email", authPreferences.getEmailAddress());
            jsonObject1.put("UniqueId", authPreferences.getUserUid());
            jsonObject1.put("profilePicUrl", authPreferences.getMyProfilePicImageUrl());
            jsonObject1.put("title", profileBeenClasss.getFirstname() + " " + profileBeenClasss.getLastname() + " has sent message to you");
            jsonObject.put("registrationIDs", jsonArray);
            jsonObject.put("notification", jsonObject1);
            entity = new StringEntity(jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
        client.setTimeout(80000);
        client.addHeader("Authorization", "key=AIzaSyCxKiM-LlNHfUOZ3QiiZfSGTo3vTAZdAAI");
        client.post(this, "https://fcm.googleapis.com/fcm/send", entity, "application/json", new TextHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, String res) {
                        System.out.println("Send Notification sucessfully" + res.toString());
                   }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
                        System.out.println("notification fail", res.toString());
                    }

                }
        );

    }
Ashutosh Srivastava
  • 597
  • 1
  • 9
  • 13