|
|
@ -18,13 +18,10 @@
|
|
|
|
package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm;
|
|
|
|
package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.auth.oauth2.GoogleCredentials;
|
|
|
|
import com.google.auth.oauth2.GoogleCredentials;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonPrimitive;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.DeviceConfigurationManager;
|
|
|
|
import com.hazelcast.aws.utility.Environment;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.ContextMetadata;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.operation.mgt.OperationManagerImpl;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.config.push.notification.PushNotificationConfiguration;
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger;
|
|
|
|
|
|
|
|
import io.entgra.device.mgt.core.notification.logger.impl.EntgraDeviceConnectivityLoggerImpl;
|
|
|
|
|
|
|
|
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;
|
|
|
@ -34,28 +31,34 @@ 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.PushNotificationConfig;
|
|
|
|
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 org.wso2.carbon.utils.CarbonUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.io.File;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.nio.file.StandardOpenOption;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
|
|
public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(FCMNotificationStrategy.class);
|
|
|
|
private static final Log log = LogFactory.getLog(FCMNotificationStrategy.class);
|
|
|
|
|
|
|
|
|
|
|
|
private static final String NOTIFIER_TYPE_FCM = "FCM";
|
|
|
|
private static final String NOTIFIER_TYPE_FCM = "FCM";
|
|
|
|
private static final String FCM_TOKEN = "FCM_TOKEN";
|
|
|
|
private static final String FCM_TOKEN = "FCM_TOKEN";
|
|
|
|
private static final String FCM_ENDPOINT = "https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send";
|
|
|
|
|
|
|
|
private static final String FCM_API_KEY = "fcmAPIKey";
|
|
|
|
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 static final int HTTP_STATUS_CODE_OK = 200;
|
|
|
|
private final PushNotificationConfig config;
|
|
|
|
private final PushNotificationConfig config;
|
|
|
|
|
|
|
|
private static final String FCM_SERVICE_ACCOUNT_PATH = CarbonUtils.getCarbonHome() + File.separator +
|
|
|
|
|
|
|
|
"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_ENDPOINT_KEY = "FCM_SERVER_ENDPOINT";
|
|
|
|
|
|
|
|
private Properties contextMetadataProperties;
|
|
|
|
|
|
|
|
private volatile GoogleCredentials defaultApplication;
|
|
|
|
|
|
|
|
|
|
|
|
public FCMNotificationStrategy(PushNotificationConfig config) {
|
|
|
|
public FCMNotificationStrategy(PushNotificationConfig config) {
|
|
|
|
this.config = config;
|
|
|
|
this.config = config;
|
|
|
@ -63,18 +66,20 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void init() {
|
|
|
|
public void init() {
|
|
|
|
|
|
|
|
initContextConfigs();
|
|
|
|
|
|
|
|
initDefaultOAuthApplication();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException {
|
|
|
|
public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException {
|
|
|
|
String token = getFcmOauthToken();
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (NOTIFIER_TYPE_FCM.equals(config.getType())) {
|
|
|
|
if (NOTIFIER_TYPE_FCM.equals(config.getType())) {
|
|
|
|
Device device = FCMDataHolder.getInstance().getDeviceManagementProviderService()
|
|
|
|
Device device = FCMDataHolder.getInstance().getDeviceManagementProviderService()
|
|
|
|
.getDeviceWithTypeProperties(ctx.getDeviceId());
|
|
|
|
.getDeviceWithTypeProperties(ctx.getDeviceId());
|
|
|
|
if(device.getProperties() != null && getFCMToken(device.getProperties()) != null) {
|
|
|
|
if(device.getProperties() != null && getFCMToken(device.getProperties()) != null) {
|
|
|
|
this.sendWakeUpCall(ctx.getOperation().getCode(), device, token);
|
|
|
|
defaultApplication.refresh();
|
|
|
|
|
|
|
|
sendWakeUpCall(defaultApplication.getAccessToken().getTokenValue(),
|
|
|
|
|
|
|
|
getFCMToken(device.getProperties()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
@ -89,51 +94,65 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getFcmOauthToken() {
|
|
|
|
private void initDefaultOAuthApplication() {
|
|
|
|
GoogleCredentials googleCredentials = null;
|
|
|
|
if (defaultApplication == null) {
|
|
|
|
|
|
|
|
synchronized (FCMNotificationStrategy.class) {
|
|
|
|
|
|
|
|
if (defaultApplication == null) {
|
|
|
|
|
|
|
|
Path serviceAccountPath = Paths.get(FCM_SERVICE_ACCOUNT_PATH);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
googleCredentials = GoogleCredentials
|
|
|
|
this.defaultApplication = GoogleCredentials.
|
|
|
|
.fromStream(new FileInputStream("/etc/service-account.json"))
|
|
|
|
fromStream(Files.newInputStream(serviceAccountPath)).
|
|
|
|
.createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging"));
|
|
|
|
createScoped(FCM_SCOPES);
|
|
|
|
googleCredentials.refresh();
|
|
|
|
|
|
|
|
if (null != googleCredentials) {
|
|
|
|
|
|
|
|
writeLog("========= Google Credentials created " + googleCredentials.getAccessToken());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
writeLog("========= Google Credentials is null");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return googleCredentials.getAccessToken().getTokenValue();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.error("Error occurred while getting the FCM OAuth token.", e);
|
|
|
|
log.error("Fail to initialize default OAuth application for FCM communication");
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public NotificationContext buildContext() {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
private void initContextConfigs() {
|
|
|
|
public void undeploy() {
|
|
|
|
PushNotificationConfiguration pushNotificationConfiguration = DeviceConfigurationManager.getInstance().
|
|
|
|
|
|
|
|
getDeviceManagementConfig().getPushNotificationConfiguration();
|
|
|
|
|
|
|
|
List<ContextMetadata> contextMetadata = pushNotificationConfiguration.getContextMetadata();
|
|
|
|
|
|
|
|
Properties properties = new Properties();
|
|
|
|
|
|
|
|
if (contextMetadata != null) {
|
|
|
|
|
|
|
|
for (ContextMetadata metadata : contextMetadata) {
|
|
|
|
|
|
|
|
properties.setProperty(metadata.getKey(), metadata.getValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
contextMetadataProperties = properties;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void sendWakeUpCall(String message, Device device, String token) throws IOException,
|
|
|
|
private void sendWakeUpCall(String accessToken, String registrationId) throws IOException,
|
|
|
|
PushNotificationExecutionFailedException {
|
|
|
|
PushNotificationExecutionFailedException {
|
|
|
|
if (device.getProperties() != null) {
|
|
|
|
|
|
|
|
writeLog("===== Calling senWakeupCall " + device);
|
|
|
|
|
|
|
|
OutputStream os = null;
|
|
|
|
OutputStream os = null;
|
|
|
|
byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpURLConnection conn = null;
|
|
|
|
HttpURLConnection conn = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String fcmServerEndpoint = contextMetadataProperties.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 {
|
|
|
|
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("Content-Type", "application/json");
|
|
|
|
conn.setRequestProperty("Authorization", "Bearer " + token);
|
|
|
|
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
conn.setRequestMethod("POST");
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
conn.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
|
|
os = conn.getOutputStream();
|
|
|
|
os = conn.getOutputStream();
|
|
|
|
os.write(bytes);
|
|
|
|
os.write(bytes);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int status = conn.getResponseCode();
|
|
|
|
|
|
|
|
if (status != 200) {
|
|
|
|
|
|
|
|
log.error("Response Status: " + status + ", Response Message: " + conn.getResponseMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (os != null) {
|
|
|
|
if (os != null) {
|
|
|
|
os.close();
|
|
|
|
os.close();
|
|
|
@ -142,47 +161,26 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
conn.disconnect();
|
|
|
|
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 fcmRequest = new JsonObject();
|
|
|
|
|
|
|
|
JsonObject messageObject = new JsonObject();
|
|
|
|
JsonObject messageObject = new JsonObject();
|
|
|
|
messageObject.addProperty("token", registrationId);
|
|
|
|
messageObject.addProperty("token", registrationId);
|
|
|
|
JsonObject notification = new JsonObject();
|
|
|
|
|
|
|
|
notification.addProperty("title", "FCM Message");
|
|
|
|
|
|
|
|
notification.addProperty("body", message);
|
|
|
|
|
|
|
|
messageObject.add("notification", notification);
|
|
|
|
|
|
|
|
fcmRequest.add("message", messageObject);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JsonObject fcmRequest = new JsonObject();
|
|
|
|
|
|
|
|
fcmRequest.add("message", messageObject);
|
|
|
|
|
|
|
|
|
|
|
|
/*fcmRequest.addProperty("delay_while_idle", false);
|
|
|
|
return fcmRequest.toString();
|
|
|
|
fcmRequest.addProperty("time_to_live", TIME_TO_LIVE);
|
|
|
|
|
|
|
|
fcmRequest.addProperty("priority", "high");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Add message to FCM request
|
|
|
|
|
|
|
|
JsonObject data = new JsonObject();
|
|
|
|
|
|
|
|
if (message != null && !message.isEmpty()) {
|
|
|
|
|
|
|
|
data.addProperty("data", message);
|
|
|
|
|
|
|
|
fcmRequest.add("data", data);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Set device reg-id
|
|
|
|
@Override
|
|
|
|
JsonArray regIds = new JsonArray();
|
|
|
|
public NotificationContext buildContext() {
|
|
|
|
regIds.add(new JsonPrimitive(registrationId));
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fcmRequest.add("registration_ids", regIds);*/
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void undeploy() {
|
|
|
|
|
|
|
|
|
|
|
|
writeLog("========= FCM Request " + fcmRequest);
|
|
|
|
|
|
|
|
return fcmRequest.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static String getFCMToken(List<Device.Property> properties) {
|
|
|
|
private static String getFCMToken(List<Device.Property> properties) {
|
|
|
@ -196,21 +194,8 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|
|
|
return fcmToken;
|
|
|
|
return fcmToken;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void writeLog(String message) {
|
|
|
|
|
|
|
|
try (FileWriter fw = new FileWriter("/opt/entgra/migration/entgra-uem-ultimate-6.0.3.0/log.txt", true);
|
|
|
|
|
|
|
|
BufferedWriter bw = new BufferedWriter(fw)) {
|
|
|
|
|
|
|
|
bw.write(message);
|
|
|
|
|
|
|
|
bw.newLine();
|
|
|
|
|
|
|
|
bw.flush();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public PushNotificationConfig getConfig() {
|
|
|
|
public PushNotificationConfig getConfig() {
|
|
|
|
return config;
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|