mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Add FCM changes
This commit is contained in:
parent
aca7aa3ccc
commit
5c73277c32
@ -111,6 +111,10 @@
|
|||||||
<groupId>org.wso2.carbon.analytics-common</groupId>
|
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||||
<artifactId>org.wso2.carbon.event.output.adapter.core</artifactId>
|
<artifactId>org.wso2.carbon.event.output.adapter.core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.auth</groupId>
|
||||||
|
<artifactId>google-auth-library-oauth2-http</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@ -141,7 +145,9 @@
|
|||||||
io.entgra.device.mgt.core.device.mgt.common.push.notification,
|
io.entgra.device.mgt.core.device.mgt.common.push.notification,
|
||||||
org.apache.commons.logging,
|
org.apache.commons.logging,
|
||||||
io.entgra.device.mgt.core.device.mgt.common.*,
|
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.*
|
||||||
</Import-Package>
|
</Import-Package>
|
||||||
</instructions>
|
</instructions>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|||||||
@ -17,9 +17,14 @@
|
|||||||
*/
|
*/
|
||||||
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.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonPrimitive;
|
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.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;
|
||||||
@ -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.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 java.io.IOException;
|
import java.io.*;
|
||||||
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.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class FCMNotificationStrategy implements NotificationStrategy {
|
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 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/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 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 that 28 days
|
||||||
private static final int HTTP_STATUS_CODE_OK = 200;
|
private static final int HTTP_STATUS_CODE_OK = 200;
|
||||||
@ -59,12 +68,13 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|||||||
|
|
||||||
@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);
|
this.sendWakeUpCall(ctx.getOperation().getCode(), device, token);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (log.isDebugEnabled()) {
|
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
|
@Override
|
||||||
public NotificationContext buildContext() {
|
public NotificationContext buildContext() {
|
||||||
return null;
|
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 {
|
PushNotificationExecutionFailedException {
|
||||||
if (device.getProperties() != null) {
|
if (device.getProperties() != null) {
|
||||||
|
writeLog("===== Calling senWakeupCall " + device);
|
||||||
OutputStream os = null;
|
OutputStream os = null;
|
||||||
byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes();
|
byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes();
|
||||||
|
|
||||||
@ -99,7 +129,7 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|||||||
try {
|
try {
|
||||||
conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection();
|
conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection();
|
||||||
conn.setRequestProperty("Content-Type", "application/json");
|
conn.setRequestProperty("Content-Type", "application/json");
|
||||||
conn.setRequestProperty("Authorization", "key=" + config.getProperty(FCM_API_KEY));
|
conn.setRequestProperty("Authorization", "Bearer " + token);
|
||||||
conn.setRequestMethod("POST");
|
conn.setRequestMethod("POST");
|
||||||
conn.setDoOutput(true);
|
conn.setDoOutput(true);
|
||||||
os = conn.getOutputStream();
|
os = conn.getOutputStream();
|
||||||
@ -125,7 +155,16 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|||||||
|
|
||||||
private static String getFCMRequest(String message, String registrationId) {
|
private static String getFCMRequest(String message, String registrationId) {
|
||||||
JsonObject fcmRequest = new JsonObject();
|
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("time_to_live", TIME_TO_LIVE);
|
||||||
fcmRequest.addProperty("priority", "high");
|
fcmRequest.addProperty("priority", "high");
|
||||||
|
|
||||||
@ -140,7 +179,9 @@ public class FCMNotificationStrategy implements NotificationStrategy {
|
|||||||
JsonArray regIds = new JsonArray();
|
JsonArray regIds = new JsonArray();
|
||||||
regIds.add(new JsonPrimitive(registrationId));
|
regIds.add(new JsonPrimitive(registrationId));
|
||||||
|
|
||||||
fcmRequest.add("registration_ids", regIds);
|
fcmRequest.add("registration_ids", regIds);*/
|
||||||
|
|
||||||
|
writeLog("========= FCM Request " + fcmRequest);
|
||||||
return fcmRequest.toString();
|
return fcmRequest.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,9 +196,21 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
5
pom.xml
5
pom.xml
@ -1916,6 +1916,11 @@
|
|||||||
<artifactId>mockito-inline</artifactId>
|
<artifactId>mockito-inline</artifactId>
|
||||||
<version>${mokito.version}</version>
|
<version>${mokito.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.auth</groupId>
|
||||||
|
<artifactId>google-auth-library-oauth2-http</artifactId>
|
||||||
|
<version>1.20.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user