Friday, 11 August 2017
Send Google FCM Push Notification Using Spring Boot
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.esense.touch.dbconstants.NOTIFICATION;
import com.esense.touch.dbconstants.STORY;
import com.esense.touch.dbconstants.USER_NOTIFICATION;
import com.esense.touch.dbconstants.USER_ROLE;
import com.esense.touch.dbconstants.USER_ROLE_DETAIL;
import com.esense.touch.service.NotificationService;
@Service
public class PushNotificationService {
private String AUTH_KEY_FCM="your server auth key";
@Autowired
NotificationService notificationService;
public String sendPushNotification(Rows row)
throws IOException {
String result = "";
URL url = new URL(Constants.API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key=" + AUTH_KEY_FCM);
conn.setRequestProperty("Content-Type", "application/json");
try {
JSONObject json = new JSONObject();
json.put("to", "device Id");
JSONObject info = new JSONObject();
String notificationTitle = "Notification";
info.put("title", notificationTitle);
info.put("body", "Content");
info.put("image","abc/abc.jpg");
json.put("data", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}
JSONObject outputobj = new JSONObject(sb.toString());
if(outputobj.get("success").equals(1)){
// success you can write your success code here
}
result = "success";
} catch (Exception e) {
e.printStackTrace();
result = "failure";
}
return result;
}
}
Subscribe to:
Post Comments (Atom)
Create Thumbnail in Video in Spring and ffmpeg
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.jcodec.api.Fr...
-
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.jcodec.api.Fr...
-
To continue my series on the power of Zone, I examine yet another powerful and useful way you can use zones to improve your application. I...
-
Recall that the equation of a straight line is given by the equation y = mx +n (1) where m is called the slope of the line. (This m...
No comments:
Post a Comment
Thanks to comment our blog. i will contact you as soon as possible