mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Add FCM request improvements
This commit is contained in:
commit
34a9aa7431
@ -151,6 +151,10 @@
|
|||||||
<groupId>org.wso2.carbon</groupId>
|
<groupId>org.wso2.carbon</groupId>
|
||||||
<artifactId>org.wso2.carbon.utils</artifactId>
|
<artifactId>org.wso2.carbon.utils</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -19,6 +19,9 @@ package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provid
|
|||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
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 io.entgra.device.mgt.core.device.mgt.common.Device;
|
import io.entgra.device.mgt.core.device.mgt.common.Device;
|
||||||
@ -89,8 +92,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 +100,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);
|
|
||||||
}
|
}
|
||||||
|
if(!response.isSuccessful()) {
|
||||||
int status = conn.getResponseCode();
|
String msg = "Response Status: " + response.code() + ", Response Message: " + response.message();
|
||||||
if (status != 200) {
|
log.error(msg);
|
||||||
log.error("Response Status: " + status + ", Response Message: " + conn.getResponseMessage());
|
throw new IOException(msg);
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
if (conn != null) {
|
|
||||||
conn.disconnect();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,14 +124,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
|
||||||
|
|||||||
@ -22,6 +22,8 @@ import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManag
|
|||||||
import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.ContextMetadata;
|
import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.ContextMetadata;
|
||||||
import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.PushNotificationConfiguration;
|
import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.PushNotificationConfiguration;
|
||||||
import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.FCMNotificationStrategy;
|
import io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm.FCMNotificationStrategy;
|
||||||
|
import okhttp3.ConnectionPool;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
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 org.wso2.carbon.utils.CarbonUtils;
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
@ -33,6 +35,7 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class FCMUtil {
|
public class FCMUtil {
|
||||||
|
|
||||||
@ -43,10 +46,29 @@ public class FCMUtil {
|
|||||||
"repository" + File.separator + "resources" + File.separator + "service-account.json";
|
"repository" + File.separator + "resources" + File.separator + "service-account.json";
|
||||||
private static final String[] FCM_SCOPES = { "https://www.googleapis.com/auth/firebase.messaging" };
|
private static final String[] FCM_SCOPES = { "https://www.googleapis.com/auth/firebase.messaging" };
|
||||||
private Properties contextMetadataProperties;
|
private Properties contextMetadataProperties;
|
||||||
|
private static ConnectionPool connectionPool;
|
||||||
|
private static OkHttpClient client;
|
||||||
|
|
||||||
private FCMUtil() {
|
private FCMUtil() {
|
||||||
initContextConfigs();
|
initContextConfigs();
|
||||||
initDefaultOAuthApplication();
|
initDefaultOAuthApplication();
|
||||||
|
initPooledConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the connection pool for the OkHttpClient instance.
|
||||||
|
*/
|
||||||
|
private void initPooledConnection() {
|
||||||
|
connectionPool = new ConnectionPool(25, 1, TimeUnit.MINUTES);
|
||||||
|
client = new OkHttpClient.Builder().connectionPool(connectionPool).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Pooled OkHttpClient instance
|
||||||
|
* @return OkHttpClient instance
|
||||||
|
*/
|
||||||
|
public OkHttpClient getHttpClient() {
|
||||||
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initDefaultOAuthApplication() {
|
private void initDefaultOAuthApplication() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user