Skip to content

Commit 98a8c7f

Browse files
committed
Added localization support to channel syncing
1 parent aa1f2f8 commit 98a8c7f

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationChannelManager.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ static String createNotificationChannel(Context context, JSONObject jsonPayload)
8989
return DEFAULT_CHANNEL_ID;
9090
}
9191

92+
// Creates NotificationChannel and NotificationChannelGroup based on a json payload.
93+
// Returns channel id after it is created.
9294
// Language dependent fields will be passed localized
9395
@RequiresApi(api = Build.VERSION_CODES.O)
9496
private static String createChannel(Context context, NotificationManager notificationManager, JSONObject payload) throws JSONException {
@@ -99,14 +101,23 @@ private static String createChannel(Context context, NotificationManager notific
99101
if (channel_id.equals(NotificationChannel.DEFAULT_CHANNEL_ID))
100102
channel_id = DEFAULT_CHANNEL_ID;
101103

102-
String channel_name = channelPayload.optString("nm", "Miscellaneous");
104+
JSONObject payloadWithText = channelPayload;
105+
if (channelPayload.has("langs")) {
106+
JSONObject langList = channelPayload.getJSONObject("langs");
107+
String deviceLanguage = OSUtils.getCorrectedLanguage();
108+
if (langList.has(deviceLanguage))
109+
payloadWithText = langList.optJSONObject(deviceLanguage);
110+
}
111+
112+
String channel_name = payloadWithText.optString("nm", "Miscellaneous");
103113

104114
int importance = priorityToImportance(payload.optInt("pri", 6));
105115
NotificationChannel channel = new NotificationChannel(channel_id, channel_name, importance);
116+
channel.setDescription(payloadWithText.optString("dscr", null));
106117

107118
if (channelPayload.has("grp_id")) {
108119
String group_id = channelPayload.optString("grp_id");
109-
CharSequence group_name = channelPayload.optString("grp_nm");
120+
CharSequence group_name = payloadWithText.optString("grp_nm");
110121
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group_id, group_name));
111122
channel.setGroup(group_id);
112123
}
@@ -169,7 +180,7 @@ static void processChannelList(Context context, JSONObject payload) {
169180

170181
NotificationManager notificationManager =
171182
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
172-
183+
173184
Set<String> sycnedChannelSet = new HashSet<>();
174185
JSONArray chnlList = payload.optJSONArray("chnl_lst");
175186
int jsonArraySize = chnlList.length();

OneSignalSDK/unittest/src/test/java/com/test/onesignal/NotificationChannelManagerRunner.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.robolectric.android.controller.ActivityController;
2323
import org.robolectric.annotation.Config;
2424
import org.robolectric.shadows.ShadowLog;
25+
import org.robolectric.shadows.ShadowNotificationManager;
2526

2627
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationChannelManager_createNotificationChannel;
2728
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationChannelManager_processChannelList;
@@ -93,12 +94,13 @@ public void createNotificationChannelCreateBasicChannel() throws Exception {
9394
}
9495

9596
@Test
96-
public void createNotificationChannelWithALlOptionsl() throws Exception {
97+
public void createNotificationChannelWithALlOptions() throws Exception {
9798
JSONObject payload = new JSONObject();
9899
JSONObject chnl = new JSONObject();
99100

100101
chnl.put("id", "test_id");
101102
chnl.put("nm", "Test Name");
103+
chnl.put("dscr", "Some description");
102104
chnl.put("grp_id", "grp_id");
103105
chnl.put("grp_nm", "Group Name");
104106

@@ -120,6 +122,7 @@ public void createNotificationChannelWithALlOptionsl() throws Exception {
120122
assertEquals("test_id", ret);
121123
assertEquals("test_id", ShadowRoboNotificationManager.lastChannel.getId());
122124
assertEquals("Test Name", channel.getName());
125+
assertEquals("Some description", channel.getDescription());
123126
assertEquals("grp_id", channel.getGroup());
124127
NotificationChannelGroup group = ShadowRoboNotificationManager.lastChannelGroup;
125128
assertEquals("grp_id", group.getId());
@@ -194,6 +197,35 @@ public void processPayloadDeletingOldChannel() throws Exception {
194197
assertChannelsForBasicChannelList();
195198
}
196199

200+
// Test that specific "en" defined keys name and descriptions are used when
201+
// the device language is English.
202+
// Top level keys under no language key are considered the default language.
203+
@Test
204+
public void processChannelListWithMultiLanguage() throws Exception {
205+
JSONObject payload = createBasicChannelListPayload();
206+
207+
JSONObject channelItem = (JSONObject)payload.optJSONArray("chnl_lst").get(0);
208+
JSONObject channelProperties = channelItem.optJSONObject("chnl");
209+
210+
// Add "langs" key with a "en" sub key.
211+
JSONObject langs = new JSONObject();
212+
JSONObject en = new JSONObject();
213+
en.put("nm", "en_nm");
214+
en.put("dscr", "en_dscr");
215+
en.put("grp_nm", "en_grp_nm");
216+
langs.put("en", en);
217+
channelProperties.put("langs", langs);
218+
219+
channelProperties.put("grp_id", "grp_id1");
220+
221+
NotificationChannelManager_processChannelList(blankActivity, payload);
222+
223+
NotificationChannel channel = getChannel("OS_id1");
224+
assertEquals("en_nm", channel.getName());
225+
assertEquals("en_dscr", channel.getDescription());
226+
assertEquals("en_grp_nm", ShadowRoboNotificationManager.lastChannelGroup.getName());
227+
}
228+
197229
JSONObject createBasicChannelListPayload() throws JSONException {
198230
createChannel("local_existing_id");
199231
createChannel("OS_existing_id");

0 commit comments

Comments
 (0)