mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
refactor code for firebase cloud messaging
This commit is contained in:
parent
4e0afb7d39
commit
33132b1f8e
@ -27,10 +27,10 @@
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm</artifactId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm</artifactId>
|
||||
<packaging>bundle</packaging>
|
||||
<name>WSO2 Carbon - GCM Based Push Notification Provider Implementation</name>
|
||||
<description>WSO2 Carbon - GCM Based Push Notification Provider Implementation</description>
|
||||
<name>WSO2 Carbon - FCM Based Push Notification Provider Implementation</name>
|
||||
<description>WSO2 Carbon - FCM Based Push Notification Provider Implementation</description>
|
||||
<url>http://wso2.org</url>
|
||||
|
||||
<dependencies>
|
||||
@ -128,10 +128,10 @@
|
||||
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
||||
<Bundle-Description>GCM Based Push Notification Provider Bundle</Bundle-Description>
|
||||
<Bundle-Description>FCM Based Push Notification Provider Bundle</Bundle-Description>
|
||||
<Export-Package>
|
||||
!org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.internal,
|
||||
org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.*
|
||||
!org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.internal,
|
||||
org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.*
|
||||
</Export-Package>
|
||||
<Import-Package>
|
||||
com.google.gson,
|
||||
@ -16,24 +16,24 @@
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm;
|
||||
package org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationProvider;
|
||||
|
||||
public class GCMBasedPushNotificationProvider implements PushNotificationProvider {
|
||||
public class FCMBasedPushNotificationProvider implements PushNotificationProvider {
|
||||
|
||||
private static final String PS_PROVIDER_GCM = "GCM";
|
||||
private static final String PS_PROVIDER_FCM = "FCM";
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return PS_PROVIDER_GCM;
|
||||
return PS_PROVIDER_FCM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NotificationStrategy getNotificationStrategy(PushNotificationConfig config) {
|
||||
return new GCMNotificationStrategy(config);
|
||||
return new FCMNotificationStrategy(config);
|
||||
}
|
||||
|
||||
}
|
||||
@ -16,7 +16,7 @@
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm;
|
||||
package org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
@ -27,7 +27,7 @@ import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig;
|
||||
import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException;
|
||||
import org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.internal.GCMDataHolder;
|
||||
import org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.internal.FCMDataHolder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
@ -35,16 +35,16 @@ import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
public class GCMNotificationStrategy implements NotificationStrategy {
|
||||
public class FCMNotificationStrategy implements NotificationStrategy {
|
||||
|
||||
private static final String GCM_TOKEN = "GCM_TOKEN";
|
||||
private static final String GCM_ENDPOINT = "https://fcm.googleapis.com/fcm/send";
|
||||
private static final String GCM_API_KEY = "gcmAPIKey";
|
||||
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 = 60;
|
||||
private static final int HTTP_STATUS_CODE_OK = 200;
|
||||
private PushNotificationConfig config;
|
||||
|
||||
public GCMNotificationStrategy(PushNotificationConfig config) {
|
||||
public FCMNotificationStrategy(PushNotificationConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ public class GCMNotificationStrategy implements NotificationStrategy {
|
||||
public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException {
|
||||
try {
|
||||
Device device =
|
||||
GCMDataHolder.getInstance().getDeviceManagementProviderService().getDevice(ctx.getDeviceId());
|
||||
FCMDataHolder.getInstance().getDeviceManagementProviderService().getDevice(ctx.getDeviceId());
|
||||
this.sendWakeUpCall(ctx.getOperation().getCode(), device);
|
||||
} catch (DeviceManagementException e) {
|
||||
throw new PushNotificationExecutionFailedException("Error occurred while retrieving device information", e);
|
||||
@ -79,13 +79,13 @@ public class GCMNotificationStrategy implements NotificationStrategy {
|
||||
private void sendWakeUpCall(String message,
|
||||
Device device) throws IOException, PushNotificationExecutionFailedException {
|
||||
OutputStream os = null;
|
||||
byte[] bytes = getGCMRequest(message, getGCMToken(device.getProperties())).getBytes();
|
||||
byte[] bytes = getFCMRequest(message, getFCMToken(device.getProperties())).getBytes();
|
||||
|
||||
HttpURLConnection conn = null;
|
||||
try {
|
||||
conn = (HttpURLConnection) new URL(GCM_ENDPOINT).openConnection();
|
||||
conn = (HttpURLConnection) new URL(FCM_ENDPOINT).openConnection();
|
||||
conn.setRequestProperty("Content-Type", "application/json");
|
||||
conn.setRequestProperty("Authorization", "key=" + config.getProperty(GCM_API_KEY));
|
||||
conn.setRequestProperty("Authorization", "key=" + config.getProperty(FCM_API_KEY));
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setDoOutput(true);
|
||||
os = conn.getOutputStream();
|
||||
@ -102,35 +102,35 @@ public class GCMNotificationStrategy implements NotificationStrategy {
|
||||
}
|
||||
}
|
||||
|
||||
private static String getGCMRequest(String message, String registrationId) {
|
||||
JsonObject gcmRequest = new JsonObject();
|
||||
gcmRequest.addProperty("delay_while_idle", false);
|
||||
gcmRequest.addProperty("time_to_live", TIME_TO_LIVE);
|
||||
private static String getFCMRequest(String message, String registrationId) {
|
||||
JsonObject fcmRequest = new JsonObject();
|
||||
fcmRequest.addProperty("delay_while_idle", false);
|
||||
fcmRequest.addProperty("time_to_live", TIME_TO_LIVE);
|
||||
|
||||
//Add message to GCM request
|
||||
//Add message to FCM request
|
||||
JsonObject data = new JsonObject();
|
||||
if (message != null && !message.isEmpty()) {
|
||||
data.addProperty("data", message);
|
||||
gcmRequest.add("data", data);
|
||||
fcmRequest.add("data", data);
|
||||
}
|
||||
|
||||
//Set device reg-id
|
||||
JsonArray regIds = new JsonArray();
|
||||
regIds.add(new JsonPrimitive(registrationId));
|
||||
|
||||
gcmRequest.add("registration_ids", regIds);
|
||||
return gcmRequest.toString();
|
||||
fcmRequest.add("registration_ids", regIds);
|
||||
return fcmRequest.toString();
|
||||
}
|
||||
|
||||
private static String getGCMToken(List<Device.Property> properties) {
|
||||
String gcmToken = null;
|
||||
private static String getFCMToken(List<Device.Property> properties) {
|
||||
String fcmToken = null;
|
||||
for (Device.Property property : properties) {
|
||||
if (GCM_TOKEN.equals(property.getName())) {
|
||||
gcmToken = property.getValue();
|
||||
if (FCM_TOKEN.equals(property.getName())) {
|
||||
fcmToken = property.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return gcmToken;
|
||||
return fcmToken;
|
||||
}
|
||||
|
||||
}
|
||||
@ -16,16 +16,16 @@
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.internal;
|
||||
package org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.internal;
|
||||
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
|
||||
public class GCMDataHolder {
|
||||
public class FCMDataHolder {
|
||||
|
||||
private DeviceManagementProviderService deviceManagementProviderService;
|
||||
private static GCMDataHolder thisInstance = new GCMDataHolder();
|
||||
private static FCMDataHolder thisInstance = new FCMDataHolder();
|
||||
|
||||
public static GCMDataHolder getInstance() {
|
||||
public static FCMDataHolder getInstance() {
|
||||
return thisInstance;
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@
|
||||
* under the License.
|
||||
*
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.internal;
|
||||
package org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.internal;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@ -24,7 +24,7 @@ import org.osgi.service.component.ComponentContext;
|
||||
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
|
||||
/**
|
||||
* @scr.component name="org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.internal.GCMPushNotificationServiceComponent" immediate="true"
|
||||
* @scr.component name="org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.internal.FCMPushNotificationServiceComponent" immediate="true"
|
||||
* @scr.reference name="carbon.device.mgt.provider"
|
||||
* interface="org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService"
|
||||
* cardinality="1..1"
|
||||
@ -32,23 +32,23 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
* bind="setDeviceManagementProviderService"
|
||||
* unbind="unsetDeviceManagementProviderService"
|
||||
*/
|
||||
public class GCMPushNotificationServiceComponent {
|
||||
public class FCMPushNotificationServiceComponent {
|
||||
|
||||
private static final Log log = LogFactory.getLog(GCMPushNotificationServiceComponent.class);
|
||||
private static final Log log = LogFactory.getLog(FCMPushNotificationServiceComponent.class);
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
protected void activate(ComponentContext componentContext) {
|
||||
try {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Initializing GCM based push notification provider implementation bundle");
|
||||
log.debug("Initializing FCM based push notification provider implementation bundle");
|
||||
}
|
||||
//Do nothing
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("GCM based push notification provider implementation bundle has been successfully " +
|
||||
log.debug("FCM based push notification provider implementation bundle has been successfully " +
|
||||
"initialized");
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
log.error("Error occurred while initializing GCM based push notification provider " +
|
||||
log.error("Error occurred while initializing FCM based push notification provider " +
|
||||
"implementation bundle", e);
|
||||
}
|
||||
}
|
||||
@ -59,12 +59,12 @@ public class GCMPushNotificationServiceComponent {
|
||||
|
||||
protected void setDeviceManagementProviderService(
|
||||
DeviceManagementProviderService deviceManagementProviderService) {
|
||||
GCMDataHolder.getInstance().setDeviceManagementProviderService(deviceManagementProviderService);
|
||||
FCMDataHolder.getInstance().setDeviceManagementProviderService(deviceManagementProviderService);
|
||||
}
|
||||
|
||||
protected void unsetDeviceManagementProviderService(
|
||||
DeviceManagementProviderService deviceManagementProviderService) {
|
||||
GCMDataHolder.getInstance().setDeviceManagementProviderService(deviceManagementProviderService);
|
||||
FCMDataHolder.getInstance().setDeviceManagementProviderService(deviceManagementProviderService);
|
||||
}
|
||||
|
||||
}
|
||||
@ -25,7 +25,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
||||
import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService;
|
||||
|
||||
/**
|
||||
* @scr.component name="org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.internal.XMPPPushNotificationServiceComponent" immediate="true"
|
||||
* @scr.component name="org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.internal.XMPPPushNotificationServiceComponent" immediate="true"
|
||||
* @scr.reference name="carbon.device.mgt.provider"
|
||||
* interface="org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService"
|
||||
* cardinality="1..1"
|
||||
|
||||
@ -34,7 +34,7 @@
|
||||
<url>http://wso2.org</url>
|
||||
|
||||
<modules>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm</module>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm</module>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt</module>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp</module>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.device.type.deployer</module>
|
||||
|
||||
@ -27,17 +27,17 @@
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.feature</artifactId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.feature</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<version>2.0.24-SNAPSHOT</version>
|
||||
<name>WSO2 Carbon - GCM Based Push Notification Provider Feature</name>
|
||||
<name>WSO2 Carbon - FCM Based Push Notification Provider Feature</name>
|
||||
<url>http://wso2.org</url>
|
||||
<description>WSO2 Carbon - MQTT Based Push Notification Provider Feature</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm</artifactId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@ -80,7 +80,7 @@
|
||||
<goal>p2-feature-gen</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<id>org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm</id>
|
||||
<id>org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm</id>
|
||||
<propertiesFile>../../../features/etc/feature.properties</propertiesFile>
|
||||
<adviceFile>
|
||||
<properties>
|
||||
@ -90,7 +90,7 @@
|
||||
</adviceFile>
|
||||
<bundles>
|
||||
<bundleDef>
|
||||
org.wso2.carbon.devicemgt:org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm:${carbon.device.mgt.version}
|
||||
org.wso2.carbon.devicemgt:org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm:${carbon.device.mgt.version}
|
||||
</bundleDef>
|
||||
</bundles>
|
||||
<importFeatures>
|
||||
@ -34,7 +34,7 @@
|
||||
|
||||
<modules>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.device.type.deployer.feature</module>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.feature</module>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.feature</module>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.feature</module>
|
||||
<module>org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp.feature</module>
|
||||
</modules>
|
||||
|
||||
@ -26,7 +26,7 @@
|
||||
</DataSourceConfiguration>
|
||||
</ManagementRepository>
|
||||
<PushNotificationProviders>
|
||||
<Provider>org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.GCMBasedPushNotificationProvider</Provider>
|
||||
<Provider>org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm.FCMBasedPushNotificationProvider</Provider>
|
||||
<!--<Provider>org.wso2.carbon.device.mgt.mobile.impl.ios.apns.APNSBasedPushNotificationProvider</Provider>-->
|
||||
<Provider>org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.MQTTBasedPushNotificationProvider</Provider>
|
||||
<Provider>org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp.XMPPBasedPushNotificationProvider</Provider>
|
||||
|
||||
2
pom.xml
2
pom.xml
@ -1403,7 +1403,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm</artifactId>
|
||||
<artifactId>org.wso2.carbon.device.mgt.extensions.push.notification.provider.fcm</artifactId>
|
||||
<version>${carbon.device.mgt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user