Skip to content

Commit

Permalink
Merge pull request #181 from Azure/feature/fcmv1
Browse files Browse the repository at this point in the history
Adds support for FCM V1
  • Loading branch information
aziztitu authored Feb 16, 2024
2 parents 12f481e + 3eca82d commit 3c37606
Show file tree
Hide file tree
Showing 22 changed files with 503 additions and 9 deletions.
2 changes: 1 addition & 1 deletion NotificationHubs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.windowsazure</groupId>
<artifactId>Notification-Hubs-java-sdk</artifactId>
<version>1.0.5</version>
<version>1.1.0-beta1</version>
<name>Windows Azure Notification Hubs Java SDK</name>
<url>https://github.com/Azure/azure-notificationhubs-java-backend</url>
<description>Azure Notification Hubs Java SDK for interacting with the data and management plane operations.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public BaseInstallation clone() {
private static void validateNotificationPlatform(NotificationPlatform notificationPlatform) {
if (notificationPlatform == NotificationPlatform.Fcm) {
throw new RuntimeException(
"FCM is currently not supported, use NotificationPlatform.Gcm which uses FCM Legacy Mode. See https://aka.ms/AA9dpaz");
"FCM is currently not supported, use NotificationPlatform.FcmV1 which uses FCM V1.");
}
}

Expand Down Expand Up @@ -290,6 +290,7 @@ public static <T extends BaseInstallation> T fromJson(String jsonString) {

Gson gson = new GsonBuilder()
.registerTypeAdapter(BaseInstallation.class, new BaseInstallationDeserializer())
.registerTypeAdapter(NotificationPlatform.class, new LowerCaseEnumDeserializer())
.create();

return (T)gson.fromJson(jsonString, BaseInstallation.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public BaseInstallation deserialize(JsonElement jsonElement, Type type, JsonDese
}

private static Class<?> getClass(String platformString) {
switch (platformString) {
switch (platformString.toLowerCase()) {
case "adm":
return AdmInstallation.class;
case "apple":
Expand All @@ -36,6 +36,8 @@ private static Class<?> getClass(String platformString) {
return BaiduInstallation.class;
case "fcm":
return FcmInstallation.class;
case "fcmv1":
return FcmV1Installation.class;
case "mpns":
return MpnsInstallation.class;
case "wns":
Expand Down
26 changes: 26 additions & 0 deletions NotificationHubs/src/com/windowsazure/messaging/FcmCredential.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

/**
* This class represents Azure Notification Hubs credentials for Firebase Messaging.
* @deprecated use {@link com.windowsazure.messaging.FcmV1Credential#FcmV1Credential()} instead.
*/
public final class FcmCredential extends PnsCredential {

private String googleApiKey;
private String gcmEndpoint = "https://fcm.googleapis.com/fcm/send";

/**
* Creates a new instance of the FcmCredential class.
Expand All @@ -31,6 +33,17 @@ public FcmCredential(String googleApiKey) {
this.googleApiKey = googleApiKey;
}

/**
* Creates a new instance of the FcmCredential class.
* @param googleApiKey The Google API key from Firebase.
* @param gcmEndpoint The GCM Endpoint for firebase.
*/
public FcmCredential(String googleApiKey, String gcmEndpoint) {
super();
this.googleApiKey = googleApiKey;
this.gcmEndpoint = gcmEndpoint;
}

/**
* Gets the Google API key for Firebase Messaging.
* @return The Google API key for Firebase Messaging.
Expand All @@ -43,9 +56,22 @@ public FcmCredential(String googleApiKey) {
*/
public void setGoogleApiKey(String value) { googleApiKey = value; }

/**
* Gets the GCM Endpoint for Firebase Messaging.
* @return The GCM Endpoint for Firebase Messaging.
*/
public String getGcmEndpoint() { return gcmEndpoint; }

/**
* Sets the GCM Endpoint for Firebase Messaging.
* @param value The GCM Endpoint for Firebase Messaging to set.
*/
public void setGcmEndpoint(String value) { gcmEndpoint = value; }

@Override
public List<SimpleEntry<String, String>> getProperties() {
ArrayList<SimpleEntry<String, String>> result = new ArrayList<>();
result.add(new SimpleEntry<>("GcmEndpoint", getGcmEndpoint()));
result.add(new SimpleEntry<>("GoogleApiKey", getGoogleApiKey()));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/**
* This class represents a Firebase Cloud Messaging installation.
* @deprecated use {@link com.windowsazure.messaging.FcmV1Installation#FcmV1Installation(String)} instead.
*/
public class FcmInstallation extends Installation {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* This class represents a notification to the Firebase Cloud Messaging service.
* @deprecated use {@link com.windowsazure.messaging.FcmV1Notification#FcmV1Notification(String)} instead.
*/
public class FcmNotification extends Notification {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* Class representing a native registration for devices using FCM.
* @deprecated use {@link com.windowsazure.messaging.FcmV1Registration#FcmV1Registration()} instead.
*/
public class FcmRegistration extends Registration {
private static final String FCM_NATIVE_REGISTRATION1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><entry xmlns=\"http://www.w3.org/2005/Atom\"><content type=\"application/xml\"><GcmRegistrationDescription xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/netservices/2010/10/servicebus/connect\">";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* This class represents a Firebase Messaging device template registration.
* @deprecated use {@link com.windowsazure.messaging.FcmV1TemplateRegistration#FcmV1TemplateRegistration()} instead.
*/
public class FcmTemplateRegistration extends FcmRegistration implements TemplateRegistration {
private static final String FCM_TEMPLATE_REGISTRATION1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><entry xmlns=\"http://www.w3.org/2005/Atom\"><content type=\"application/xml\"><GcmTemplateRegistrationDescription xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/netservices/2010/10/servicebus/connect\">";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------

package com.windowsazure.messaging;

import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.List;

/**
* This class represents Azure Notification Hubs credentials for Firebase Messaging V1.
*/
public final class FcmV1Credential extends PnsCredential {

private String privateKey;
private String projectId;
private String clientEmail;

/**
* Creates a new instance of the FcmV1Credential class.
*/
public FcmV1Credential() {

}

/**
* Creates a new instance of the FcmV1Credential class.
* @param privateKey The Private key from Firebase.
* @param projectId The Project ID from Firebase.
* @param clientEmail The Client Email from Firebase.
*/
public FcmV1Credential(String privateKey, String projectId, String clientEmail) {
super();
this.privateKey = privateKey;
this.projectId = projectId;
this.clientEmail = clientEmail;
}

/**
* Gets the Private key for Firebase Messaging.
* @return The Private key for Firebase Messaging.
*/
public String getPrivateKey() { return privateKey; }

/**
* Sets the Private key for Firebase Messaging.
* @param value The Private key for Firebase Messaging to set.
*/
public void setPrivateKey(String value) { privateKey = value; }

/**
* Gets the Project ID for Firebase Messaging.
* @return The Project ID for Firebase Messaging.
*/
public String getProjectId() { return projectId; }

/**
* Sets the Project ID for Firebase Messaging.
* @param value The Project ID for Firebase Messaging to set.
*/
public void setProjectId(String value) { projectId = value; }

/**
* Gets the Client Email for Firebase Messaging.
* @return The Client Email for Firebase Messaging.
*/
public String getClientEmail() { return clientEmail; }

/**
* Sets the Client Email for Firebase Messaging.
* @param value The Client Email for Firebase Messaging to set.
*/
public void setClientEmail(String value) { clientEmail = value; }

@Override
public List<SimpleEntry<String, String>> getProperties() {
ArrayList<SimpleEntry<String, String>> result = new ArrayList<>();
result.add(new SimpleEntry<>("PrivateKey", getPrivateKey()));
result.add(new SimpleEntry<>("ProjectId", getProjectId()));
result.add(new SimpleEntry<>("ClientEmail", getClientEmail()));
return result;
}

@Override
public String getRootTagName() {
return "FcmV1Credential";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------

package com.windowsazure.messaging;

/**
* This class represents a Firebase Cloud Messaging V1 installation.
*/
public class FcmV1Installation extends Installation {

/**
* Creates a new instance of the FcmV1Installation class.
*
* @param installationId The ID for the installation.
*/
public FcmV1Installation(String installationId) {
super(installationId, NotificationPlatform.FcmV1, null, (String[]) null);
}

/**
* Creates a new instance of the FcmV1Installation class.
*
* @param installationId The ID for the installation.
* @param tags The tags for the installation.
*/
public FcmV1Installation(String installationId, String... tags) {
super(installationId, NotificationPlatform.FcmV1, null, tags);
}

/**
* Creates a new instance of the FcmV1Installation class.
*
* @param installationId The ID for the installation.
*/
public FcmV1Installation(String installationId, String pushChannel) {
super(installationId, NotificationPlatform.FcmV1, pushChannel, (String[]) null);
}

/**
* Creates a new instance of the FcmV1Installation class.
* @param installationId The ID for the installation.
* @param pushChannel The device specific push channel for the installation.
* @param tags The tags for the installation.
*/
public FcmV1Installation(String installationId, String pushChannel, String... tags) {
super(installationId, NotificationPlatform.FcmV1, pushChannel, tags);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------

package com.windowsazure.messaging;

import org.apache.hc.core5.http.ContentType;

/**
* This class represents a notification to the Firebase Cloud Messaging V1 service.
*/
public class FcmV1Notification extends Notification {

/**
* Creates a new instance of the FcmV1Notification class.
* @param body The JSON body for the Firebase Cloud Messaging V1 service.
*/
public FcmV1Notification(String body) {
this.body = body;
this.contentType = ContentType.APPLICATION_JSON;
this.headers.put("ServiceBusNotification-Format", "fcmv1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------

package com.windowsazure.messaging;

import java.util.Objects;

/**
* Class representing a native registration for devices using FCM V1.
*/
public class FcmV1Registration extends Registration {
private static final String FCM_V1_NATIVE_REGISTRATION1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><entry xmlns=\"http://www.w3.org/2005/Atom\"><content type=\"application/xml\"><FcmV1RegistrationDescription xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.microsoft.com/netservices/2010/10/servicebus/connect\">";
private static final String FCM_V1_NATIVE_REGISTRATION2 = "<FcmV1RegistrationId>";
private static final String FCM_V1_NATIVE_REGISTRATION3 = "</FcmV1RegistrationId></FcmV1RegistrationDescription></content></entry>";

protected String fcmRegistrationId;

/**
* Creates a new instance of the FcmV1Registration class.
*/
public FcmV1Registration() {
super();
}

/**
* Creates a new instance of the FcmV1Registration with a registration ID and FCM registration ID.
* @param registrationId The Azure Notification Hubs registration ID.
* @param fcmRegistrationId The Firebase Cloud Messaging registration ID.
*/
public FcmV1Registration(String registrationId, String fcmRegistrationId) {
super(registrationId);
this.fcmRegistrationId = fcmRegistrationId;
}

/**
* Creates a new instance of the FcmV1Registration with a Firebase Cloud Messaging V1 registration ID.
* @param fcmRegistrationId The Firebase Cloud Messaging V1 registration ID.
*/
public FcmV1Registration(String fcmRegistrationId) {
super();
this.fcmRegistrationId = fcmRegistrationId;
}

/**
* Gets the Firebase Messaging registration ID.
* @return The Firebase Messaging registration ID.
*/
public String getFcmRegistrationId() { return fcmRegistrationId; }

/**
* Sets the Firebase Messaging registration ID.
* @param value The Firebase Messaging registration ID to set.
*/
public void setFcmRegistrationId(String value) { fcmRegistrationId = value; }

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
FcmV1Registration that = (FcmV1Registration) o;
return Objects.equals(getFcmRegistrationId(), that.getFcmRegistrationId());
}

/**
* Gets the PNS handle for getting devices by channel.
* @return The PNS handle for getting devices by channel.
*/
@Override
public String getPnsHandle() { return fcmRegistrationId; }

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getFcmRegistrationId());
}

@Override
public String getXml() {
return FCM_V1_NATIVE_REGISTRATION1 +
getTagsXml() +
FCM_V1_NATIVE_REGISTRATION2 +
fcmRegistrationId +
FCM_V1_NATIVE_REGISTRATION3;
}

}
Loading

0 comments on commit 3c37606

Please sign in to comment.