diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml
index ffb529ef29..ea7aec37a0 100644
--- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml
+++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/pom.xml
@@ -111,6 +111,10 @@
org.wso2.carbon.analytics-common
org.wso2.carbon.event.output.adapter.core
+
+ com.google.auth
+ google-auth-library-oauth2-http
+
@@ -141,7 +145,9 @@
io.entgra.device.mgt.core.device.mgt.common.push.notification,
org.apache.commons.logging,
io.entgra.device.mgt.core.device.mgt.common.*,
- io.entgra.device.mgt.core.device.mgt.core.service
+ io.entgra.device.mgt.core.device.mgt.core.service,
+ io.entgra.device.mgt.core.device.mgt.extensions.logger.spi,
+ io.entgra.device.mgt.core.notification.logger.*
diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java
index c6100aa418..669edd314a 100644
--- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java
+++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/push/notification/provider/fcm/FCMNotificationStrategy.java
@@ -17,9 +17,14 @@
*/
package io.entgra.device.mgt.core.device.mgt.extensions.push.notification.provider.fcm;
+import com.google.auth.oauth2.GoogleCredentials;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
+import com.hazelcast.aws.utility.Environment;
+import io.entgra.device.mgt.core.device.mgt.core.operation.mgt.OperationManagerImpl;
+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.LogFactory;
import io.entgra.device.mgt.core.device.mgt.common.Device;
@@ -30,10 +35,14 @@ 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.extensions.push.notification.provider.fcm.internal.FCMDataHolder;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.io.*;
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.nio.file.StandardOpenOption;
+import java.util.Arrays;
import java.util.List;
public class FCMNotificationStrategy implements NotificationStrategy {
@@ -42,7 +51,7 @@ public class FCMNotificationStrategy implements NotificationStrategy {
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_ENDPOINT = "https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages: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 HTTP_STATUS_CODE_OK = 200;
@@ -59,12 +68,13 @@ public class FCMNotificationStrategy implements NotificationStrategy {
@Override
public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException {
+ String token = getFcmOauthToken();
try {
if (NOTIFIER_TYPE_FCM.equals(config.getType())) {
Device device = FCMDataHolder.getInstance().getDeviceManagementProviderService()
.getDeviceWithTypeProperties(ctx.getDeviceId());
if(device.getProperties() != null && getFCMToken(device.getProperties()) != null) {
- this.sendWakeUpCall(ctx.getOperation().getCode(), device);
+ this.sendWakeUpCall(ctx.getOperation().getCode(), device, token);
}
} else {
if (log.isDebugEnabled()) {
@@ -79,6 +89,25 @@ public class FCMNotificationStrategy implements NotificationStrategy {
}
}
+ private String getFcmOauthToken() {
+ GoogleCredentials googleCredentials = null;
+ try {
+ googleCredentials = GoogleCredentials
+ .fromStream(new FileInputStream("/etc/service-account.json"))
+ .createScoped(Arrays.asList("https://www.googleapis.com/auth/firebase.messaging"));
+ googleCredentials.refresh();
+ if (null != googleCredentials) {
+ writeLog("========= Google Credentials created " + googleCredentials.getAccessToken());
+ } else {
+ writeLog("========= Google Credentials is null");
+ }
+ return googleCredentials.getAccessToken().getTokenValue();
+ } catch (IOException e) {
+ log.error("Error occurred while getting the FCM OAuth token.", e);
+ throw new RuntimeException(e);
+ }
+ }
+
@Override
public NotificationContext buildContext() {
return null;
@@ -89,9 +118,10 @@ public class FCMNotificationStrategy implements NotificationStrategy {
}
- private void sendWakeUpCall(String message, Device device) throws IOException,
+ private void sendWakeUpCall(String message, Device device, String token) throws IOException,
PushNotificationExecutionFailedException {
if (device.getProperties() != null) {
+ writeLog("===== Calling senWakeupCall " + device);
OutputStream os = null;
byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes();
@@ -99,7 +129,7 @@ public class FCMNotificationStrategy implements NotificationStrategy {
try {
conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection();
conn.setRequestProperty("Content-Type", "application/json");
- conn.setRequestProperty("Authorization", "key=" + config.getProperty(FCM_API_KEY));
+ conn.setRequestProperty("Authorization", "Bearer " + token);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
os = conn.getOutputStream();
@@ -125,7 +155,16 @@ public class FCMNotificationStrategy implements NotificationStrategy {
private static String getFCMRequest(String message, String registrationId) {
JsonObject fcmRequest = new JsonObject();
- fcmRequest.addProperty("delay_while_idle", false);
+ JsonObject messageObject = new JsonObject();
+ 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);
+
+
+ /*fcmRequest.addProperty("delay_while_idle", false);
fcmRequest.addProperty("time_to_live", TIME_TO_LIVE);
fcmRequest.addProperty("priority", "high");
@@ -140,7 +179,9 @@ public class FCMNotificationStrategy implements NotificationStrategy {
JsonArray regIds = new JsonArray();
regIds.add(new JsonPrimitive(registrationId));
- fcmRequest.add("registration_ids", regIds);
+ fcmRequest.add("registration_ids", regIds);*/
+
+ writeLog("========= FCM Request " + fcmRequest);
return fcmRequest.toString();
}
@@ -155,9 +196,21 @@ public class FCMNotificationStrategy implements NotificationStrategy {
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
public PushNotificationConfig getConfig() {
return config;
}
+
}
diff --git a/pom.xml b/pom.xml
index 680a4ced22..516bd8e982 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1916,6 +1916,11 @@
mockito-inline
${mokito.version}
+
+ com.google.auth
+ google-auth-library-oauth2-http
+ 1.20.0
+