Use Spring's built in RestTemplate REST abstraction to simplify it.
The FireBasePost object just contains the required fields for the POST Message API: Firebase API- and I have verified the request Entity works by posting with String.class, so the response is unmarshalled JSON.
-----------------response object-----------
package Your package;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class FirebaseResponse {
private long multicast_id;
private Integer success;
private Integer failure;
private Object canonical_ids;
private long message_id;
public FirebaseResponse() {
}
public long getMulticast_id() {
return multicast_id;
}
public void setMulticast_id(long multicast_id) {
this.multicast_id = multicast_id;
}
public Integer getSuccess() {
return success;
}
public void setSuccess(Integer success) {
this.success = success;
}
public Integer getFailure() {
return failure;
}
public void setFailure(Integer failure) {
this.failure = failure;
}
public Object getCanonical_ids() {
return canonical_ids;
}
public void setCanonical_ids(Object canonical_ids) {
this.canonical_ids = canonical_ids;
}
public long getMessage_id() {
return message_id;
}
public void setMessage_id(long message_id) {
this.message_id = message_id;
}
@Override
public String toString() {
return "FirebaseResponse [multicast_id=" + multicast_id + ", success=" + success + ", failure=" + failure
+ ", canonical_ids=" + canonical_ids + ", message_id=" + message_id + "]";
}
}
---------------------------------Usage------------------------------------------------------
package Your package;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.conduiraonline.objects.FirebaseResponse;
import com.conduiraonline.objects.HeaderRequestInterceptor;
import com.google.gson.Gson;
@Component
public class FirebaseService {
private static final Logger logger = LoggerFactory.getLogger(FirebaseService.class);
private static final String firebaseUrl = "https://fcm.googleapis.com/fcm/send";
private static RestTemplate restTemplate = new RestTemplate();
private final String FIREBASE_SERVER_KEY = "Firebase server key";
public final static String appTestingTopic = "/topics/App-Testing";
public String sendNotification(){
restTemplate.setInterceptors(getInterceptors());
JSONObject body = new JSONObject();
// JsonArray registration_ids = new JsonArray();
// body.put("registration_ids", registration_ids);
body.put("to", "cfW930CZxxxxxxxxxxxxxxxxxxxxxxxxxxipdO-bjHLacHRqQzC0aSXlRFKdMHv_aNBxkRZLNxxxxxxxxxxx59sPW4Rw-5MtwKkZxxxxxxxgXlL-LliJuujPwZpLgLpji_");
//or some topic
//body.put("to"appTestingTopic , );
body.put("priority", "high");
// body.put("dry_run", true);
JSONObject notification = new JSONObject();
notification.put("body", "body string here");
notification.put("title", "title string here");
// notification.put("icon", "myicon");
JSONObject data = new JSONObject();
data.put("key1", "value1");
data.put("key2", "value2");
body.put("notification", notification);
body.put("data", data);
HttpEntity<String> request = new HttpEntity<>(body.toString());
FirebaseResponse firebaseResponse = restTemplate.postForObject(firebaseUrl, request, FirebaseResponse.class);
logger.info("response is: " + firebaseResponse.toString());
return firebaseResponse.getMessage_id()+"";
}
public ArrayList<ClientHttpRequestInterceptor> getInterceptors(){
ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + FIREBASE_SERVER_KEY));
interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json"));
return interceptors;
}
}
The FireBasePost object just contains the required fields for the POST Message API: Firebase API- and I have verified the request Entity works by posting with String.class, so the response is unmarshalled JSON.
-----------------response object-----------
package Your package;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class FirebaseResponse {
private long multicast_id;
private Integer success;
private Integer failure;
private Object canonical_ids;
private long message_id;
public FirebaseResponse() {
}
public long getMulticast_id() {
return multicast_id;
}
public void setMulticast_id(long multicast_id) {
this.multicast_id = multicast_id;
}
public Integer getSuccess() {
return success;
}
public void setSuccess(Integer success) {
this.success = success;
}
public Integer getFailure() {
return failure;
}
public void setFailure(Integer failure) {
this.failure = failure;
}
public Object getCanonical_ids() {
return canonical_ids;
}
public void setCanonical_ids(Object canonical_ids) {
this.canonical_ids = canonical_ids;
}
public long getMessage_id() {
return message_id;
}
public void setMessage_id(long message_id) {
this.message_id = message_id;
}
@Override
public String toString() {
return "FirebaseResponse [multicast_id=" + multicast_id + ", success=" + success + ", failure=" + failure
+ ", canonical_ids=" + canonical_ids + ", message_id=" + message_id + "]";
}
}
---------------------------------Usage------------------------------------------------------
package Your package;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import com.conduiraonline.objects.FirebaseResponse;
import com.conduiraonline.objects.HeaderRequestInterceptor;
import com.google.gson.Gson;
@Component
public class FirebaseService {
private static final Logger logger = LoggerFactory.getLogger(FirebaseService.class);
private static final String firebaseUrl = "https://fcm.googleapis.com/fcm/send";
private static RestTemplate restTemplate = new RestTemplate();
private final String FIREBASE_SERVER_KEY = "Firebase server key";
public final static String appTestingTopic = "/topics/App-Testing";
public String sendNotification(){
restTemplate.setInterceptors(getInterceptors());
JSONObject body = new JSONObject();
// JsonArray registration_ids = new JsonArray();
// body.put("registration_ids", registration_ids);
body.put("to", "cfW930CZxxxxxxxxxxxxxxxxxxxxxxxxxxipdO-bjHLacHRqQzC0aSXlRFKdMHv_aNBxkRZLNxxxxxxxxxxx59sPW4Rw-5MtwKkZxxxxxxxgXlL-LliJuujPwZpLgLpji_");
//or some topic
//body.put("to"appTestingTopic , );
body.put("priority", "high");
// body.put("dry_run", true);
JSONObject notification = new JSONObject();
notification.put("body", "body string here");
notification.put("title", "title string here");
// notification.put("icon", "myicon");
JSONObject data = new JSONObject();
data.put("key1", "value1");
data.put("key2", "value2");
body.put("notification", notification);
body.put("data", data);
HttpEntity<String> request = new HttpEntity<>(body.toString());
FirebaseResponse firebaseResponse = restTemplate.postForObject(firebaseUrl, request, FirebaseResponse.class);
logger.info("response is: " + firebaseResponse.toString());
return firebaseResponse.getMessage_id()+"";
}
public ArrayList<ClientHttpRequestInterceptor> getInterceptors(){
ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + FIREBASE_SERVER_KEY));
interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json"));
return interceptors;
}
}
This is such a great resource that you are providing and you give it away for free. I love seeing websites that understand the value of providing a quality resource for free. It is the old what goes around comes around routine.
ReplyDeleteHow to create an app
Thank you for the informative post . Now Internet service providers and network operators have started using the new Industry best In-browser messaging solution.
ReplyDeleteIn-browser Notification for ISPs and Telecom Providers
ReplyDeleteGreat Article
Cyber Security Projects
projects for cse
JavaScript Training in Chennai
JavaScript Training in Chennai