Send Push Notification In Android And iOS Using Spring Boot Application

By | April 5, 2018

To send a push notification in android device we are going to use FCM and for iOS device we will use APNS.


Android push notification:

FCM stands for Firebase Cloud Messaging its a service that is provided by Google. Inorder to use FCM you need an API key which you can get through firebase console and a device token or registration token of your device from FCM. If you are a backend developer and push notification is used in your project then you can request  your android developer to register your project app with FCM and provide API key and device token respectively. If you are ready with API key and device token you can use the below code to send push notification to a android device.

try {
   String androidFcmKey="XXXXXXXXXXXX";
   String androidFcmUrl="";

   RestTemplate restTemplate = new RestTemplate();
   HttpHeaders httpHeaders = new HttpHeaders();
   httpHeaders.set("Authorization", "key=" + androidFcmKey);
   httpHeaders.set("Content-Type", "application/json");
   JSONObject msg = new JSONObject();
   JSONObject json = new JSONObject();

   msg.put("title", "Title");
   msg.put("body", "Message");
   msg.put("notificationType", "Test");

   json.put("data", msg);
   json.put("to", deviceToken);

   HttpEntity<String> httpEntity = new HttpEntity<String>(json.toString(),httpHeaders);
   String response = restTemplate.postForObject(androidFcmUrl,httpEntity,String.class);
} catch (JSONException e) {

To send push notifications to multiple android devices just change this line finalMsg.put(“to”, deviceToken); to this finalMsg.put(“registration_ids”, deviceTokenArray);


iOS push notification:

APNS stands for Apple Push Notification Service. For using this service Apple provides some cryptographic certificates which can be a pem or p12 file. In your spring boot application you need to add below written maven dependency for using APNS.


For Apple push notification certificate(p12 file) and device token you need to ask your project’s iOS developer.

Save Apple push notification certificate into resources folder of your spring boot application. If you are ready with certificate and device token you can use the below code to send push notification to a iOS device.

Environment env;
try {
   InputStream inputStream = new ClassPathResource("push.p12").getInputStream();
   ApnsService service;
   if (Arrays.asList(env.getActiveProfiles()).contains("pro")) {
     service = APNS.newService().withCert(inputStream, "Password@123")
   } else {
     service = APNS.newService().withCert(inputStream, "Password@123")
   String payload = APNS.newPayload().customField("customData","customData")
   ApnsNotification apns = service.push(deviceToken, payload);
} catch (IOException e) {

We have configured push notification based on environment thats why we have autowired Environment interface. For sending notifications to multiple iOS devices you need to iterate over ApnService’s push method.