Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, May 9, 2017

Send Notification to Mobile device using java



This is mobile era. Most of portal have it's own mobile app. Many time we have situation to access portal data from mobile app using web services. Also need to notify mobile user by notification after processing request successfull. I spend more then a day to find the api and implement to achive this requirement. So here i am sharing how we can send mobile notification from java.


public static Response sendAndroidNotification(String deviceToken,String message,String title) {
       OkHttpClient client = new OkHttpClient();
       MediaType mediaType = MediaType.parse("application/json");
       JSONObject obj = new JSONObject();
       JSONObject msgObject = new JSONObject();
       msgObject.put("body", message);
       msgObject.put("title", title);
      // msgObject.put("icon", ANDROID_NOTIFICATION_ICON);
       msgObject.put("color", "#ff0000");

       obj.put("to", deviceToken);
       obj.put("notification",msgObject);

       RequestBody body = RequestBody.create(mediaType, obj.toString());
       Request request = new Request.Builder().url(FIRE_BASE_SERVER_URL).post(body)
               .addHeader("Content-Type", "application/json")
               .addHeader("Authorization", "key="+SERVER_KEY).build();

       Response response;
try {
response = client.newCall(request).execute();
_log.info( "Response : " + response.body().string());
return response;
} catch (IOException e) {
_log.info(e.getMessage(),e);
}

return null;
}


Note:
FIRE_BASE_SERVER_URL = https://fcm.googleapis.com/fcm/send
SERVER_KEY = Your app server key. you can find it from firebase portal.
deviceToken = Your deviceId is generate when you install app. you can find it from firebase portal.


This is very simple implementation to send notification to mobile device using java.

HTH!!

Thank you,
Ketan Savaliya

Friday, August 23, 2013

Map Iterator/loop in java

Many time some little things goes our of our mind. Same thing happen in developer life also.So, here i am going to write a small piece of java code which is used daily routine.

Here i am talking about Collection framework Iteration of MAP.

When ever developer need Map almost all time they go to google for how to iterate MAP? so, ans for this question i am going to write small blog about MAP iteration. Bellow is the example for the same.

About Map : Map is part of collection framework. Mainly use to store data in key,value manner. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key. now we go through step by step how to use MAP.



1) Declaration of MAP
        Map<String,String> stringMap = new HashMap<String, String>();

2) Store data into MAP

        stringMap.put("1","Apple" );
        stringMap.put("2","Ball");
        stringMap.put("3","Cat");

3) Iteration/Loop/Use of MAP

There are so many ways you can iterate MAP in java. but here i am going to simple way to iterate MAP in java as per my knowledge.
       - by Iterator
       - by EnterySet
       - by keySet

Here we use keySet to iterator MAP. this is very simple way as per my development experiance. like...

         for(String key : stringMap.keySet()){
                 System.out.println("Key : " + key + ", Value : " + stringMap.get(key) );
         }


4) Exmaple

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class test {

    public static void main(String[] s){
       
        Map<String,String> stringMap = new HashMap<String, String>();
        stringMap.put("3","Cat");
        stringMap.put("1","Apple" );
        stringMap.put("2","Ball");
       
        for(String key : stringMap.keySet()){
            System.out.println("Key : " + key + ", Value : " + stringMap.get(key) );
        }
 }

}


5) Output

Key : 3, Value : Cat
Key : 2, Value : Ball
Key : 1, Value : Apple

It's simple right? still we have to go googling for every time to iterate map. i ma sure this blog visit help you remember iteration of map.

That's it!!

Enjoy the code!!


Thank you,
 Ketan Savaliya