|
|
|
@ -17,9 +17,12 @@
|
|
|
|
|
*/
|
|
|
|
|
package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
|
import com.google.auth.oauth2.GoogleCredentials;
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
|
import com.google.gson.JsonPrimitive;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager;
|
|
|
|
|
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.extensions.push.notification.provider.fcm.util.FCMUtil;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.Device;
|
|
|
|
@ -29,24 +32,29 @@ import io.entgra.device.mgt.core.device.mgt.common.push.notification.Notificatio
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.common.push.notification.PushNotificationConfig;
|
|
|
|
|
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 org.wso2.carbon.utils.CarbonUtils;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(FCMNotificationStrategy.class);
|
|
|
|
|
|
|
|
|
|
private static final String NOTIFIER_TYPE_FCM = "FCM";
|
|
|
|
|
private static final String FCM_TOKEN = "FCM_TOKEN";
|
|
|
|
|
private static final String FCM_ENDPOINT = "https://fcm.googleapis.com/fcm/send";
|
|
|
|
|
private static final String FCM_API_KEY = "fcmAPIKey";
|
|
|
|
|
private static final int TIME_TO_LIVE = 2419199; // 1 second less that 28 days
|
|
|
|
|
private static final int TIME_TO_LIVE = 2419199; // 1 second less than 28 days
|
|
|
|
|
private static final int HTTP_STATUS_CODE_OK = 200;
|
|
|
|
|
private final PushNotificationConfig config;
|
|
|
|
|
private static final String FCM_ENDPOINT_KEY = "FCM_SERVER_ENDPOINT";
|
|
|
|
|
|
|
|
|
|
public FCMNotificationStrategy(PushNotificationConfig config) {
|
|
|
|
|
this.config = config;
|
|
|
|
@ -64,7 +72,9 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
|
Device device = FCMDataHolder.getInstance().getDeviceManagementProviderService()
|
|
|
|
|
.getDeviceWithTypeProperties(ctx.getDeviceId());
|
|
|
|
|
if(device.getProperties() != null && getFCMToken(device.getProperties()) != null) {
|
|
|
|
|
this.sendWakeUpCall(ctx.getOperation().getCode(), device);
|
|
|
|
|
FCMUtil.getInstance().getDefaultApplication().refresh();
|
|
|
|
|
sendWakeUpCall(FCMUtil.getInstance().getDefaultApplication().getAccessToken().getTokenValue(),
|
|
|
|
|
getFCMToken(device.getProperties()));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
@ -79,31 +89,36 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public NotificationContext buildContext() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void undeploy() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendWakeUpCall(String message, Device device) throws IOException,
|
|
|
|
|
private void sendWakeUpCall(String accessToken, String registrationId) throws IOException,
|
|
|
|
|
PushNotificationExecutionFailedException {
|
|
|
|
|
if (device.getProperties() != null) {
|
|
|
|
|
OutputStream os = null;
|
|
|
|
|
byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes();
|
|
|
|
|
|
|
|
|
|
HttpURLConnection conn = null;
|
|
|
|
|
|
|
|
|
|
String fcmServerEndpoint = FCMUtil.getInstance().getContextMetadataProperties().getProperty(FCM_ENDPOINT_KEY);
|
|
|
|
|
if(fcmServerEndpoint == null) {
|
|
|
|
|
String msg = "Encountered configuration issue. " + FCM_ENDPOINT_KEY + " is not defined";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new PushNotificationExecutionFailedException(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection();
|
|
|
|
|
byte[] bytes = getFCMRequest(registrationId).getBytes();
|
|
|
|
|
URL url = new URL(fcmServerEndpoint);
|
|
|
|
|
conn = (HttpURLConnection) url.openConnection();
|
|
|
|
|
conn.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
conn.setRequestProperty("Authorization", "key=" + config.getProperty(FCM_API_KEY));
|
|
|
|
|
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
|
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
os = conn.getOutputStream();
|
|
|
|
|
os.write(bytes);
|
|
|
|
|
|
|
|
|
|
int status = conn.getResponseCode();
|
|
|
|
|
if (status != 200) {
|
|
|
|
|
log.error("Response Status: " + status + ", Response Message: " + conn.getResponseMessage());
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (os != null) {
|
|
|
|
|
os.close();
|
|
|
|
@ -112,36 +127,26 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
|
conn.disconnect();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int status = conn.getResponseCode();
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Result code: " + status + ", Message: " + conn.getResponseMessage());
|
|
|
|
|
}
|
|
|
|
|
if (status != HTTP_STATUS_CODE_OK) {
|
|
|
|
|
throw new PushNotificationExecutionFailedException("Push notification sending failed with the HTTP " +
|
|
|
|
|
"error code '" + status + "'");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getFCMRequest(String message, String registrationId) {
|
|
|
|
|
private static String getFCMRequest(String registrationId) {
|
|
|
|
|
JsonObject messageObject = new JsonObject();
|
|
|
|
|
messageObject.addProperty("token", registrationId);
|
|
|
|
|
|
|
|
|
|
JsonObject fcmRequest = new JsonObject();
|
|
|
|
|
fcmRequest.addProperty("delay_while_idle", false);
|
|
|
|
|
fcmRequest.addProperty("time_to_live", TIME_TO_LIVE);
|
|
|
|
|
fcmRequest.addProperty("priority", "high");
|
|
|
|
|
fcmRequest.add("message", messageObject);
|
|
|
|
|
|
|
|
|
|
return fcmRequest.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Add message to FCM request
|
|
|
|
|
JsonObject data = new JsonObject();
|
|
|
|
|
if (message != null && !message.isEmpty()) {
|
|
|
|
|
data.addProperty("data", message);
|
|
|
|
|
fcmRequest.add("data", data);
|
|
|
|
|
@Override
|
|
|
|
|
public NotificationContext buildContext() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Set device reg-id
|
|
|
|
|
JsonArray regIds = new JsonArray();
|
|
|
|
|
regIds.add(new JsonPrimitive(registrationId));
|
|
|
|
|
@Override
|
|
|
|
|
public void undeploy() {
|
|
|
|
|
|
|
|
|
|
fcmRequest.add("registration_ids", regIds);
|
|
|
|
|
return fcmRequest.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getFCMToken(List<Device.Property> properties) {
|
|
|
|
@ -159,5 +164,4 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
|
public PushNotificationConfig getConfig() {
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|