|
|
@ -26,13 +26,13 @@ import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotific
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationExecutionFailedException;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationExecutionFailedException;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.internal.FCMDataHolder;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.internal.FCMDataHolder;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.util.FCMUtil;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.util.FCMUtil;
|
|
|
|
|
|
|
|
import okhttp3.Request;
|
|
|
|
|
|
|
|
import okhttp3.RequestBody;
|
|
|
|
|
|
|
|
import okhttp3.Response;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
@ -89,8 +89,6 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private void sendWakeUpCall(String accessToken, String registrationId) throws IOException,
|
|
|
|
private void sendWakeUpCall(String accessToken, String registrationId) throws IOException,
|
|
|
|
PushNotificationExecutionFailedException {
|
|
|
|
PushNotificationExecutionFailedException {
|
|
|
|
HttpURLConnection conn = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String fcmServerEndpoint = FCMUtil.getInstance().getContextMetadataProperties()
|
|
|
|
String fcmServerEndpoint = FCMUtil.getInstance().getContextMetadataProperties()
|
|
|
|
.getProperty(FCM_ENDPOINT_KEY);
|
|
|
|
.getProperty(FCM_ENDPOINT_KEY);
|
|
|
|
if(fcmServerEndpoint == null) {
|
|
|
|
if(fcmServerEndpoint == null) {
|
|
|
@ -99,26 +97,21 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
throw new PushNotificationExecutionFailedException(msg);
|
|
|
|
throw new PushNotificationExecutionFailedException(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
RequestBody fcmRequest = getFCMRequest(registrationId);
|
|
|
|
byte[] bytes = getFCMRequest(registrationId).getBytes();
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
URL url = new URL(fcmServerEndpoint);
|
|
|
|
.url(fcmServerEndpoint)
|
|
|
|
conn = (HttpURLConnection) url.openConnection();
|
|
|
|
.post(fcmRequest)
|
|
|
|
conn.setRequestProperty("Content-Type", "application/json");
|
|
|
|
.addHeader("Authorization", "Bearer " + accessToken)
|
|
|
|
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
|
|
|
|
.build();
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
try (Response response = FCMUtil.getInstance().getHttpClient().newCall(request).execute()) {
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("FCM message sent to the FCM server. Response code: " + response.code()
|
|
|
|
try (OutputStream os = conn.getOutputStream()) {
|
|
|
|
+ " Response message : " + response.message());
|
|
|
|
os.write(bytes);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int status = conn.getResponseCode();
|
|
|
|
|
|
|
|
if (status != 200) {
|
|
|
|
|
|
|
|
log.error("Response Status: " + status + ", Response Message: " + conn.getResponseMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if(!response.isSuccessful()) {
|
|
|
|
if (conn != null) {
|
|
|
|
String msg = "Response Status: " + response.code() + ", Response Message: " + response.message();
|
|
|
|
conn.disconnect();
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new IOException(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -128,14 +121,14 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
* @param registrationId Registration ID of the device
|
|
|
|
* @param registrationId Registration ID of the device
|
|
|
|
* @return FCM request as a JSON string
|
|
|
|
* @return FCM request as a JSON string
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static String getFCMRequest(String registrationId) {
|
|
|
|
private static RequestBody getFCMRequest(String registrationId) {
|
|
|
|
JsonObject messageObject = new JsonObject();
|
|
|
|
JsonObject messageObject = new JsonObject();
|
|
|
|
messageObject.addProperty("token", registrationId);
|
|
|
|
messageObject.addProperty("token", registrationId);
|
|
|
|
|
|
|
|
|
|
|
|
JsonObject fcmRequest = new JsonObject();
|
|
|
|
JsonObject fcmRequest = new JsonObject();
|
|
|
|
fcmRequest.add("message", messageObject);
|
|
|
|
fcmRequest.add("message", messageObject);
|
|
|
|
|
|
|
|
|
|
|
|
return fcmRequest.toString();
|
|
|
|
return RequestBody.create(fcmRequest.toString(), okhttp3.MediaType.parse("application/json"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|