Skip to content

Commit ddca019

Browse files
authored
feat(Tags): Add tags support to Notification Hub module (#9)
Fixes #2
1 parent 1c7db45 commit ddca019

File tree

5 files changed

+87
-27
lines changed

5 files changed

+87
-27
lines changed

android/src/main/java/com/azure/reactnative/notificationhub/NotificationHubUtil.java

+35-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import android.content.Context;
44
import android.content.SharedPreferences;
5-
import android.content.SharedPreferences.Editor;
5+
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
import java.util.Set;
69

710
public class NotificationHubUtil {
811
private static NotificationHubUtil sharedNotificationHubUtilInstance = null;
@@ -17,7 +20,8 @@ public class NotificationHubUtil {
1720
"AzureNotificationHub_hubName";
1821
private static final String KEY_FOR_PREFS_FCMTOKEN =
1922
"AzureNotificationHub_FCMToken";
20-
23+
private static final String KEY_FOR_PREFS_TAGS =
24+
"AzureNotificationHub_Tags";
2125

2226
public static NotificationHubUtil getInstance() {
2327
if(sharedNotificationHubUtilInstance == null) {
@@ -27,41 +31,57 @@ public static NotificationHubUtil getInstance() {
2731
}
2832

2933
public String getConnectionString(Context context) {
30-
return getPref(context, KEY_FOR_PREFS_CONNECTIONSTRING, null);
34+
return getPref(context, KEY_FOR_PREFS_CONNECTIONSTRING);
3135
}
3236

3337
public void setConnectionString(Context context, String connectionString) {
3438
setPref(context, KEY_FOR_PREFS_CONNECTIONSTRING, connectionString);
3539
}
3640

3741
public String getHubName(Context context) {
38-
return getPref(context, KEY_FOR_PREFS_HUBNAME, null);
42+
return getPref(context, KEY_FOR_PREFS_HUBNAME);
3943
}
4044

4145
public void setHubName(Context context, String hubName) {
4246
setPref(context, KEY_FOR_PREFS_HUBNAME, hubName);
4347
}
4448

4549
public String getRegistrationID(Context context) {
46-
return getPref(context, KEY_FOR_PREFS_REGISTRATIONID, null);
50+
return getPref(context, KEY_FOR_PREFS_REGISTRATIONID);
4751
}
4852

4953
public void setRegistrationID(Context context, String registrationID) {
5054
setPref(context, KEY_FOR_PREFS_REGISTRATIONID, registrationID);
5155
}
5256

5357
public String getFCMToken(Context context) {
54-
return getPref(context, KEY_FOR_PREFS_FCMTOKEN, null);
58+
return getPref(context, KEY_FOR_PREFS_FCMTOKEN);
5559
}
5660

5761
public void setFCMToken(Context context, String token) {
5862
setPref(context, KEY_FOR_PREFS_FCMTOKEN, token);
5963
}
6064

61-
private String getPref(Context context, String key, String defaultValue) {
65+
public String[] getTags(Context context) {
66+
Set<String> set = getPrefSet(context, KEY_FOR_PREFS_TAGS);
67+
return set != null ? set.toArray(new String[set.size()]) : null;
68+
}
69+
70+
public void setTags(Context context, String[] tags) {
71+
Set<String> set = tags != null ? new HashSet<>(Arrays.asList(tags)) : null;
72+
setPrefSet(context, KEY_FOR_PREFS_TAGS, set);
73+
}
74+
75+
private String getPref(Context context, String key) {
6276
SharedPreferences prefs =
6377
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
64-
return prefs.getString(key, defaultValue);
78+
return prefs.getString(key, null);
79+
}
80+
81+
private Set<String> getPrefSet(Context context, String key) {
82+
SharedPreferences prefs =
83+
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
84+
return prefs.getStringSet(key, null);
6585
}
6686

6787
private void setPref(Context context, String key, String value) {
@@ -70,4 +90,11 @@ private void setPref(Context context, String key, String value) {
7090
editor.putString(key, value);
7191
editor.apply();
7292
}
93+
94+
private void setPrefSet(Context context, String key, Set<String> value) {
95+
SharedPreferences.Editor editor =
96+
context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE).edit();
97+
editor.putStringSet(key, value);
98+
editor.apply();
99+
}
73100
}

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.facebook.react.bridge.ReactContext;
1515
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1616
import com.facebook.react.bridge.ReactMethod;
17+
import com.facebook.react.bridge.ReadableArray;
1718
import com.facebook.react.bridge.ReadableMap;
1819
import com.facebook.react.bridge.UiThreadUtil;
1920

@@ -52,9 +53,19 @@ public void register(ReadableMap config, Promise promise) {
5253
promise.reject(ERROR_INVALID_ARGUMENTS, "Sender ID cannot be null.");
5354
}
5455

56+
String[] tags = null;
57+
if (config.hasKey("tags") && !config.isNull("tags")) {
58+
ReadableArray tagsJson = config.getArray("tags");
59+
tags = new String[tagsJson.size()];
60+
for (int i = 0; i < tagsJson.size(); ++i) {
61+
tags[i] = tagsJson.getString(i);
62+
}
63+
}
64+
5565
ReactContext reactContext = getReactApplicationContext();
5666
notificationHubUtil.setConnectionString(reactContext, connectionString);
5767
notificationHubUtil.setHubName(reactContext, hubName);
68+
notificationHubUtil.setTags(reactContext, tags);
5869

5970
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
6071
int resultCode = apiAvailability.isGooglePlayServicesAvailable(reactContext);
@@ -93,11 +104,11 @@ public void unregister(Promise promise) {
93104
NotificationHub hub = new NotificationHub(hubName, connectionString, reactContext);
94105
try {
95106
hub.unregister();
107+
notificationHubUtil.setRegistrationID(reactContext, null);
108+
NotificationsManager.stopHandlingNotifications(reactContext);
96109
} catch (Exception e) {
97110
promise.reject(ERROR_NOTIFICATION_HUB, e);
98111
}
99-
100-
NotificationsManager.stopHandlingNotifications(reactContext);
101112
}
102113

103114
private static class GoogleApiAvailabilityRunnable implements Runnable {

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeRegistrationIntentService.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ protected void onHandleIntent(Intent intent) {
2525
String hubName = notificationHubUtil.getHubName(this);
2626
String regID = notificationHubUtil.getRegistrationID(this);
2727
String storedToken = notificationHubUtil.getFCMToken(this);
28+
String[] tags = notificationHubUtil.getTags(this);
29+
2830
if (connectionString == null || hubName == null) {
2931
// The intent was triggered when no connection string has been set.
3032
// This is likely due to an InstanceID refresh occurring while no user
@@ -41,7 +43,7 @@ protected void onHandleIntent(Intent intent) {
4143
if (regID == null || storedToken != token) {
4244
NotificationHub hub = new NotificationHub(hubName, connectionString, this);
4345
Log.d(TAG, "NH Registration refreshing with token : " + token);
44-
regID = hub.register(token).getRegistrationId();
46+
regID = hub.register(token, tags).getRegistrationId();
4547

4648
Log.d(TAG, "New NH Registration Successfully - RegId : " + regID);
4749

windows/ReactWindowsAzureNotificationHub/ReactAzureNotificationHubModule.cs

+35-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ namespace ReactWindowsAzureNotificationHub
99
class ReactAzureNotificationHubModule : ReactContextNativeModuleBase
1010
{
1111
private const string RegistrationFailure = "E_REGISTRATION_FAILED";
12+
private const string ErrorInvalidArguments = "E_INVALID_ARGUMENTS";
13+
14+
private string _connectionString;
15+
private string _hubName;
1216

1317
public ReactAzureNotificationHubModule(ReactContext reactContext)
1418
: base(reactContext)
@@ -24,45 +28,61 @@ public override string Name
2428
}
2529

2630
[ReactMethod]
27-
public async void register(JObject config, IPromise callback)
31+
public async void register(JObject config, IPromise promise)
2832
{
33+
_connectionString = config.Value<string>("connectionString");
34+
_hubName = config.Value<string>("hubName");
35+
var tags = config["tags"]?.ToObject<string[]>();
36+
37+
AssertArguments(promise);
38+
2939
try
3040
{
31-
var hubName = config.Value<string>("hubName");
32-
var connectionString = config.Value<string>("connectionString");
3341
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync().AsTask().ConfigureAwait(false);
34-
var hub = new NotificationHub(hubName, connectionString);
35-
var result = await hub.RegisterNativeAsync(channel.Uri).ConfigureAwait(false);
36-
42+
var hub = new NotificationHub(_hubName, _connectionString);
43+
var result = await hub.RegisterNativeAsync(channel.Uri, tags).ConfigureAwait(false);
3744
if (result.RegistrationId != null)
3845
{
39-
callback.Resolve(result.RegistrationId);
46+
promise.Resolve(result.RegistrationId);
4047
}
4148
else
4249
{
43-
callback.Reject(RegistrationFailure, "Registration was not successful.");
50+
promise.Reject(RegistrationFailure, "Registration was not successful.");
4451
}
4552
}
4653
catch (Exception ex)
4754
{
48-
callback.Reject(ex);
55+
promise.Reject(ex);
4956
}
5057
}
5158

5259
[ReactMethod]
53-
public async void unregister(JObject config, IPromise callback)
60+
public async void unregister(IPromise promise)
5461
{
62+
AssertArguments(promise);
63+
5564
try
5665
{
57-
var hubName = config.Value<string>("hubName");
58-
var connectionString = config.Value<string>("connectionString");
59-
var hub = new NotificationHub(hubName, connectionString);
66+
var hub = new NotificationHub(_hubName, _connectionString);
6067
await hub.UnregisterNativeAsync();
61-
callback.Resolve(true);
68+
promise.Resolve(true);
6269
}
6370
catch (Exception ex)
6471
{
65-
callback.Reject(ex);
72+
promise.Reject(ex);
73+
}
74+
}
75+
76+
private void AssertArguments(IPromise promise)
77+
{
78+
if (_connectionString == null)
79+
{
80+
promise.Reject(ErrorInvalidArguments, "Connection string cannot be null.");
81+
}
82+
83+
if (_hubName == null)
84+
{
85+
promise.Reject(ErrorInvalidArguments, "Hub name cannot be null.");
6686
}
6787
}
6888
}

windows/ReactWindowsAzureNotificationHub/ReactWindowsAzureNotificationHub.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
<EmbeddedResource Include="Properties\ReactWindowsAzureNotificationHub.rd.xml" />
114114
</ItemGroup>
115115
<ItemGroup>
116-
<ProjectReference Include="..\..\..\..\react-native-windows\ReactWindows\ReactNative\ReactNative.csproj">
116+
<ProjectReference Include="..\..\..\react-native-windows\ReactWindows\ReactNative\ReactNative.csproj">
117117
<Project>{c7673ad5-e3aa-468c-a5fd-fa38154e205c}</Project>
118118
<Name>ReactNative</Name>
119119
</ProjectReference>

0 commit comments

Comments
 (0)