Skip to content

Commit 0cdf710

Browse files
committed
Supported setting channel's name.
1 parent 1a1a687 commit 0cdf710

File tree

8 files changed

+90
-8
lines changed

8 files changed

+90
-8
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ public final class ReactNativeConstants {
1010
public static final String EVENT_AZURE_NOTIFICATION_HUB_REGISTERED = "azureNotificationHubRegistered";
1111
public static final String EVENT_AZURE_NOTIFICATION_HUB_REGISTERED_ERROR = "azureNotificationHubRegisteredError";
1212

13+
// Registration's keys
14+
public static final String KEY_REGISTRATION_CHANNELNAME = "channelName";
15+
public static final String KEY_REGISTRATION_CHANNELIMPORTANCE = "channelImportance";
16+
public static final String KEY_REGISTRATION_CHANNELSHOWBADGE = "channelShowBadge";
17+
public static final String KEY_REGISTRATION_CHANNELENABLELIGHTS = "channelEnableLights";
18+
public static final String KEY_REGISTRATION_CHANNELENABLEVIBRATION = "channelEnableVibration";
19+
1320
// Shared prefs used in NotificationHubUtil
1421
public static final String SHARED_PREFS_NAME = "com.azure.reactnative.notificationhub.NotificationHubUtil";
1522
public static final String KEY_FOR_PREFS_REGISTRATIONID = "AzureNotificationHub_registrationID";
@@ -18,6 +25,7 @@ public final class ReactNativeConstants {
1825
public static final String KEY_FOR_PREFS_FCMTOKEN = "AzureNotificationHub_FCMToken";
1926
public static final String KEY_FOR_PREFS_TAGS = "AzureNotificationHub_Tags";
2027
public static final String KEY_FOR_PREFS_SENDERID = "AzureNotificationHub_senderID";
28+
public static final String KEY_FOR_PREFS_CHANNELNAME = "AzureNotificationHub_channelName";
2129
public static final String KEY_FOR_PREFS_CHANNELIMPORTANCE = "AzureNotificationHub_channelImportance";
2230
public static final String KEY_FOR_PREFS_CHANNELSHOWBADGE = "AzureNotificationHub_channelShowBadge";
2331
public static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights";

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public static void createNotificationChannel(Context context) {
2323
if (notificationChannelID == null) {
2424
ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil.getInstance();
2525
ReactNativeNotificationChannelBuilder builder = ReactNativeNotificationChannelBuilder.Factory.create();
26+
27+
if (notificationHubUtil.hasChannelName(context)) {
28+
builder.setName(notificationHubUtil.getChannelName(context));
29+
}
30+
2631
if (notificationHubUtil.hasChannelImportance(context)) {
2732
builder.setImportance(notificationHubUtil.getChannelImportance(context));
2833
}

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,28 @@ public void register(ReadableMap config, Promise promise) {
9797
notificationHubUtil.setSenderID(reactContext, senderID);
9898
notificationHubUtil.setTags(reactContext, tags);
9999

100-
if (config.hasKey("channelImportance")) {
101-
int channelImportance = config.getInt("channelImportance");
100+
if (config.hasKey(KEY_REGISTRATION_CHANNELNAME)) {
101+
String channelName = config.getString(KEY_REGISTRATION_CHANNELNAME);
102+
notificationHubUtil.setChannelName(reactContext, channelName);
103+
}
104+
105+
if (config.hasKey(KEY_REGISTRATION_CHANNELIMPORTANCE)) {
106+
int channelImportance = config.getInt(KEY_REGISTRATION_CHANNELIMPORTANCE);
102107
notificationHubUtil.setChannelImportance(reactContext, channelImportance);
103108
}
104109

105-
if (config.hasKey("channelShowBadge")) {
106-
boolean channelShowBadge = config.getBoolean("channelShowBadge");
110+
if (config.hasKey(KEY_REGISTRATION_CHANNELSHOWBADGE)) {
111+
boolean channelShowBadge = config.getBoolean(KEY_REGISTRATION_CHANNELSHOWBADGE);
107112
notificationHubUtil.setChannelShowBadge(reactContext, channelShowBadge);
108113
}
109114

110-
if (config.hasKey("channelEnableLights")) {
111-
boolean channelEnableLights = config.getBoolean("channelEnableLights");
115+
if (config.hasKey(KEY_REGISTRATION_CHANNELENABLELIGHTS)) {
116+
boolean channelEnableLights = config.getBoolean(KEY_REGISTRATION_CHANNELENABLELIGHTS);
112117
notificationHubUtil.setChannelEnableLights(reactContext, channelEnableLights);
113118
}
114119

115-
if (config.hasKey("channelEnableVibration")) {
116-
boolean channelEnableVibration = config.getBoolean("channelEnableVibration");
120+
if (config.hasKey(KEY_REGISTRATION_CHANNELENABLEVIBRATION)) {
121+
boolean channelEnableVibration = config.getBoolean(KEY_REGISTRATION_CHANNELENABLEVIBRATION);
117122
notificationHubUtil.setChannelEnableVibration(reactContext, channelEnableVibration);
118123
}
119124

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,18 @@ public void setSenderID(Context context, String senderID) {
105105
setPref(context, KEY_FOR_PREFS_SENDERID, senderID);
106106
}
107107

108+
public String getChannelName(Context context) {
109+
return getPref(context, KEY_FOR_PREFS_CHANNELNAME);
110+
}
111+
112+
public void setChannelName(Context context, String channelName) {
113+
setPref(context, KEY_FOR_PREFS_CHANNELNAME, channelName);
114+
}
115+
116+
public boolean hasChannelName(Context context) {
117+
return hasKey(context, KEY_FOR_PREFS_CHANNELNAME);
118+
}
119+
108120
public int getChannelImportance(Context context) {
109121
return getPrefInt(context, KEY_FOR_PREFS_CHANNELIMPORTANCE);
110122
}

docs/android-installation.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ const connectionString = '...'; // The Notification Hub connection string
189189
const hubName = '...'; // The Notification Hub name
190190
const senderID = '...'; // The Sender ID from the Cloud Messaging tab of the Firebase console
191191
const tags = [ ... ]; // The set of tags to subscribe to
192+
const channelName = '...'; // The channel's name
192193
const channelImportance = 3; // The channel's importance (NotificationManager.IMPORTANCE_DEFAULT = 3)
193194
// Notes:
194195
// 1. Setting this value to 4 enables heads-up notification on Android 8
@@ -213,6 +214,7 @@ export default class App extends Component {
213214
hubName,
214215
senderID,
215216
tags,
217+
channelName,
216218
channelImportance,
217219
channelShowBadge,
218220
channelEnableLights,

sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeFirebaseMessagingServiceTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.mockito.ArgumentMatchers.any;
3131
import static org.mockito.ArgumentMatchers.anyBoolean;
3232
import static org.mockito.ArgumentMatchers.anyInt;
33+
import static org.mockito.ArgumentMatchers.anyString;
3334
import static org.mockito.ArgumentMatchers.eq;
3435
import static org.mockito.Mockito.reset;
3536
import static org.mockito.Mockito.times;
@@ -99,6 +100,7 @@ public void testCreateNotificationChannelBelowVersionO() {
99100

100101
ReactNativeNotificationChannelBuilder builder = PowerMockito.mock(ReactNativeNotificationChannelBuilder.class);
101102
when(ReactNativeNotificationChannelBuilder.Factory.create()).thenReturn(builder);
103+
when(mHubUtil.hasChannelName(mReactApplicationContext)).thenReturn(false);
102104
when(mHubUtil.hasChannelImportance(mReactApplicationContext)).thenReturn(false);
103105
when(mHubUtil.hasChannelShowBadge(mReactApplicationContext)).thenReturn(false);
104106
when(mHubUtil.hasChannelEnableLights(mReactApplicationContext)).thenReturn(false);
@@ -107,6 +109,7 @@ public void testCreateNotificationChannelBelowVersionO() {
107109

108110
ReactNativeFirebaseMessagingService.createNotificationChannel(mReactApplicationContext);
109111

112+
verify(builder, times(0)).setName(anyString());
110113
verify(builder, times(0)).setImportance(anyInt());
111114
verify(builder, times(0)).setShowBadge(anyBoolean());
112115
verify(builder, times(0)).enableLights(anyBoolean());
@@ -118,6 +121,7 @@ public void testCreateNotificationChannelBelowVersionO() {
118121

119122
@Test
120123
public void testCreateNotificationChannel() {
124+
final String channelName = "Channel Name";
121125
final int channelImportance = 1;
122126
final boolean channelShowBadge = true;
123127
final boolean channelEnableLights = true;
@@ -129,6 +133,8 @@ public void testCreateNotificationChannel() {
129133
NotificationChannel channel = PowerMockito.mock(NotificationChannel.class);
130134
when(channel.getId()).thenReturn(NOTIFICATION_CHANNEL_ID);
131135
when(builder.build()).thenReturn(channel);
136+
when(mHubUtil.hasChannelName(mReactApplicationContext)).thenReturn(true);
137+
when(mHubUtil.getChannelName(mReactApplicationContext)).thenReturn(channelName);
132138
when(mHubUtil.hasChannelImportance(mReactApplicationContext)).thenReturn(true);
133139
when(mHubUtil.getChannelImportance(mReactApplicationContext)).thenReturn(channelImportance);
134140
when(mHubUtil.hasChannelShowBadge(mReactApplicationContext)).thenReturn(true);
@@ -141,6 +147,8 @@ public void testCreateNotificationChannel() {
141147

142148
ReactNativeFirebaseMessagingService.createNotificationChannel(mReactApplicationContext);
143149

150+
verify(mHubUtil, times(1)).hasChannelName(mReactApplicationContext);
151+
verify(builder, times(1)).setName(channelName);
144152
verify(mHubUtil, times(1)).hasChannelImportance(mReactApplicationContext);
145153
verify(builder, times(1)).setImportance(channelImportance);
146154
verify(mHubUtil, times(1)).hasChannelShowBadge(mReactApplicationContext);

sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeNotificationHubModuleTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ public void testRegisterMissingSenderID() {
172172
ERROR_INVALID_SENDER_ID);
173173
}
174174

175+
@Test
176+
public void testRegisterHasChannelName() {
177+
final String channelName = "Channel Name";
178+
179+
when(mConfig.getString("connectionString")).thenReturn("connectionString");
180+
when(mConfig.getString("hubName")).thenReturn("hubName");
181+
when(mConfig.getString("senderID")).thenReturn("senderID");
182+
when(mConfig.hasKey(KEY_REGISTRATION_CHANNELNAME)).thenReturn(true);
183+
when(mConfig.getString(KEY_REGISTRATION_CHANNELNAME)).thenReturn(channelName);
184+
185+
mHubModule.register(mConfig, mPromise);
186+
187+
verify(mNotificationHubUtil, times(1)).setChannelName(
188+
any(ReactContext.class), eq(channelName));
189+
}
190+
175191
@Test
176192
public void testRegisterHasChannelImportance() {
177193
final int channelImportance = 1;

sample/android/app/src/test/java/com/reactnativeazurenotificationhubsample/ReactNativeNotificationHubUtilTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,32 @@ public void testSetSenderID() {
194194
verify(mEditor, times(1)).apply();
195195
}
196196

197+
@Test
198+
public void testGetChannelName() {
199+
mHubUtil.getChannelName(mReactApplicationContext);
200+
201+
verify(mSharedPreferences, times(1)).getString(
202+
KEY_FOR_PREFS_CHANNELNAME, null);
203+
}
204+
205+
@Test
206+
public void testSetChannelName() {
207+
final String channelName = "Channel Name";
208+
209+
mHubUtil.setChannelName(mReactApplicationContext, channelName);
210+
211+
verify(mEditor, times(1)).putString(
212+
KEY_FOR_PREFS_CHANNELNAME, channelName);
213+
verify(mEditor, times(1)).apply();
214+
}
215+
216+
@Test
217+
public void testHasChannelName() {
218+
mHubUtil.hasChannelName(mReactApplicationContext);
219+
220+
verify(mSharedPreferences, times(1)).contains(KEY_FOR_PREFS_CHANNELNAME);
221+
}
222+
197223
@Test
198224
public void testGetChannelImportance() {
199225
mHubUtil.getChannelImportance(mReactApplicationContext);

0 commit comments

Comments
 (0)