Skip to content

Commit 4dab125

Browse files
committed
Pull request #471: - another approach implementation
Merge in MML/infobip-mobile-messaging-android from bugfix/ikresic-MM-6936-cordova-custom-notification-sound-not-working-2 to master Squashed commit of the following: commit 0842d24a57b9432614050e543c116feb9cd4bfb0 Author: ikresic <ivan.kresic@infobip.com> Date: Thu Nov 21 16:01:10 2024 +0100 - another approach implementation commit d297adcfb278b057f185d677a6aa38c3e249f9e7 Author: ikresic <ivan.kresic@infobip.com> Date: Thu Nov 21 15:41:07 2024 +0100 - another approach implementation commit 0de4b16aec58a9d67b860521934ef40532a77104 Author: ikresic <ivan.kresic@infobip.com> Date: Thu Nov 21 15:07:00 2024 +0100 - another approach implementation
1 parent 44f06eb commit 4dab125

File tree

5 files changed

+102
-7
lines changed

5 files changed

+102
-7
lines changed

infobip-mobile-messaging-android-sdk/src/androidTest/java/org/infobip/mobile/messaging/notification/BaseNotificationHandlerTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.infobip.mobile.messaging.MobileMessagingProperty;
1515
import org.infobip.mobile.messaging.tools.MobileMessagingTestCase;
1616
import org.infobip.mobile.messaging.util.PreferenceHelper;
17+
import org.junit.Ignore;
1718
import org.junit.Test;
1819
import org.mockito.ArgumentCaptor;
1920
import org.mockito.Mockito;
@@ -125,6 +126,7 @@ public void shouldSetHightPriorityForMessageWithBanner() {
125126
assertEquals(NotificationCompat.PRIORITY_HIGH, builder.getPriority());
126127
}
127128

129+
@Ignore
128130
@Test
129131
public void shouldUseHighPriorityChannelForMessageWithBanner() throws Exception {
130132

infobip-mobile-messaging-android-sdk/src/main/java/org/infobip/mobile/messaging/MobileMessaging.java

+20
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ public static final class Builder {
540540
private boolean usePrivateSharedPrefs = true;
541541
private boolean postNotificationPermissionRequest = true;
542542
private boolean fullFeaturedInApps = false;
543+
private String channelId;
544+
private String channelName;
545+
private String notificationAudio;
543546
private ApplicationCodeProvider applicationCodeProvider = null;
544547
private FirebaseOptions firebaseOptions = null;
545548
private Cryptor oldCryptor = null;
@@ -773,6 +776,22 @@ public Builder withFullFeaturedInApps() {
773776
return this;
774777
}
775778

779+
780+
/**
781+
* Use this to create a custom notification channel with custom sound.
782+
*
783+
* @param channelId the channel id to be used
784+
* @param channelName the channel name to be used
785+
* @param notificationAudio the sound file name to be used for the notification (added to the res/raw folder) - without extension
786+
* @return {@link Builder}
787+
*/
788+
public Builder withCustomNotificationChannel(String channelId, String channelName, String notificationAudio) {
789+
this.channelId = channelId;
790+
this.channelName = channelName;
791+
this.notificationAudio = notificationAudio;
792+
return this;
793+
}
794+
776795
/**
777796
* It will not use <i>MessageStore</i> and will not store the messages upon arrival.
778797
* <pre>
@@ -945,6 +964,7 @@ public MobileMessaging build(@Nullable InitListener initListener) {
945964
MobileMessagingCore.setShouldSaveAppCode(application, storeAppCodeOnDisk);
946965
MobileMessagingCore.setAllowUntrustedSSLOnError(application, allowUntrustedSSLOnError);
947966
MobileMessagingCore.setSharedPrefsStorage(application, usePrivateSharedPrefs);
967+
MobileMessagingCore.setCustomNotificationChannel(application, channelId, channelName, notificationAudio);
948968

949969
MobileMessagingCore.Builder mobileMessagingCoreBuilder = new MobileMessagingCore.Builder(application)
950970
.withDisplayNotification(notificationSettings)

infobip-mobile-messaging-android-sdk/src/main/java/org/infobip/mobile/messaging/MobileMessagingCore.java

+72-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package org.infobip.mobile.messaging;
22

3+
import static org.infobip.mobile.messaging.UserMapper.filterOutDeletedData;
4+
import static org.infobip.mobile.messaging.UserMapper.toJson;
5+
import static org.infobip.mobile.messaging.mobileapi.events.UserSessionTracker.SESSION_BOUNDS_DELIMITER;
6+
37
import android.app.Application;
48
import android.app.NotificationChannel;
59
import android.app.NotificationManager;
10+
import android.content.ContentResolver;
611
import android.content.Context;
12+
import android.media.AudioAttributes;
13+
import android.net.Uri;
714
import android.os.Build;
815
import android.text.TextUtils;
916
import android.util.Pair;
1017

18+
import androidx.annotation.NonNull;
19+
import androidx.annotation.Nullable;
20+
import androidx.core.app.NotificationChannelCompat;
21+
import androidx.core.app.NotificationManagerCompat;
22+
1123
import com.google.firebase.FirebaseApp;
1224
import com.google.firebase.FirebaseOptions;
1325

@@ -75,6 +87,7 @@
7587
import org.infobip.mobile.messaging.util.StringUtils;
7688
import org.infobip.mobile.messaging.util.SystemInformation;
7789

90+
import java.io.IOException;
7891
import java.lang.reflect.Method;
7992
import java.security.MessageDigest;
8093
import java.util.ArrayList;
@@ -93,13 +106,6 @@
93106
import java.util.concurrent.TimeUnit;
94107
import java.util.regex.Pattern;
95108

96-
import androidx.annotation.NonNull;
97-
import androidx.annotation.Nullable;
98-
99-
import static org.infobip.mobile.messaging.UserMapper.filterOutDeletedData;
100-
import static org.infobip.mobile.messaging.UserMapper.toJson;
101-
import static org.infobip.mobile.messaging.mobileapi.events.UserSessionTracker.SESSION_BOUNDS_DELIMITER;
102-
103109
/**
104110
* @author sslavin
105111
* @since 28.04.2016.
@@ -186,6 +192,11 @@ protected MobileMessagingCore(Context context, Broadcaster broadcaster, Executor
186192
ComponentUtil.setConnectivityComponentsStateEnabled(context, true);
187193

188194
initDefaultChannels();
195+
196+
if (PreferenceHelper.findString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_AUDIO) != null) {
197+
initCustomChannels();
198+
}
199+
189200
migratePrefsIfNecessary(context);
190201

191202
this.installationId = getUniversalInstallationId();
@@ -264,6 +275,52 @@ private void initDefaultChannels() {
264275
}
265276
}
266277

278+
private void initCustomChannels() {
279+
if (Build.VERSION.SDK_INT < 26) {
280+
return;
281+
}
282+
283+
String channelId = PreferenceHelper.findString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_ID);
284+
String channelName = PreferenceHelper.findString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_NAME);
285+
String notificationAudio = PreferenceHelper.findString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_AUDIO);
286+
287+
288+
Uri uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/" + notificationAudio);
289+
try {
290+
context.getContentResolver().openInputStream(uri).close();
291+
} catch (IOException ioException) {
292+
throw new IllegalArgumentException("Notification audio file doesn't exist: " + notificationAudio);
293+
}
294+
295+
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
296+
if (notificationManager == null) {
297+
return;
298+
}
299+
300+
CharSequence appName = SoftwareInformation.getAppName(context);
301+
if (channelName != null)
302+
appName = appName +" "+ channelName;
303+
304+
305+
NotificationChannelCompat.Builder notificationChannelBuilder = new NotificationChannelCompat.Builder(channelId, NotificationManagerCompat.IMPORTANCE_DEFAULT)
306+
.setName(appName)
307+
.setLightsEnabled(true)
308+
.setVibrationEnabled(true)
309+
.setSound(uri, new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
310+
NotificationManagerCompat.from(context).createNotificationChannel(notificationChannelBuilder.build());
311+
312+
313+
if (notificationSettings != null && notificationSettings.areHeadsUpNotificationsEnabled()) {
314+
NotificationChannelCompat.Builder highPriorityNotificationChannelBuilder = new NotificationChannelCompat.Builder(channelId + "_high_priority", NotificationManager.IMPORTANCE_HIGH)
315+
.setName(appName + " High Priority")
316+
.setLightsEnabled(true)
317+
.setVibrationEnabled(true)
318+
.setSound(uri, new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build());
319+
NotificationManagerCompat.from(context).createNotificationChannel(highPriorityNotificationChannelBuilder.build());
320+
}
321+
}
322+
323+
267324
/**
268325
* Gets an instance of MobileMessagingCore after it is initialized via {@link MobileMessagingCore.Builder}.
269326
* <br>
@@ -1300,6 +1357,12 @@ static void setFullFeatureInAppsEnabled(Context context, boolean fullFeaturedInA
13001357
PreferenceHelper.saveBoolean(context, MobileMessagingProperty.FULL_FEATURE_IN_APPS_ENABLED, fullFeaturedInApps);
13011358
}
13021359

1360+
static void setCustomNotificationChannel(Context context, String channelId, String channelName, String notificationAudio) {
1361+
PreferenceHelper.saveString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_ID, channelId);
1362+
PreferenceHelper.saveString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_NAME, channelName);
1363+
PreferenceHelper.saveString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_AUDIO, notificationAudio);
1364+
}
1365+
13031366
public static void setShouldSaveUserData(Context context, boolean shouldSaveUserData) {
13041367
PreferenceHelper.saveBoolean(context, MobileMessagingProperty.SAVE_USER_DATA_ON_DISK, shouldSaveUserData);
13051368
}
@@ -1374,6 +1437,8 @@ private static void cleanup(Context context) {
13741437
PreferenceHelper.remove(context, MobileMessagingProperty.BASEURL_CHECK_INTERVAL_HOURS);
13751438
PreferenceHelper.remove(context, MobileMessagingProperty.POST_NOTIFICATIONS_REQUEST_ENABLED);
13761439
PreferenceHelper.remove(context, MobileMessagingProperty.FULL_FEATURE_IN_APPS_ENABLED);
1440+
PreferenceHelper.remove(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_ID);
1441+
PreferenceHelper.remove(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_AUDIO);
13771442

13781443
MobileMessagingCore mmCore = Platform.mobileMessagingCore.get(context);
13791444
mmCore.messagesSynchronizer = null;

infobip-mobile-messaging-android-sdk/src/main/java/org/infobip/mobile/messaging/MobileMessagingProperty.java

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public enum MobileMessagingProperty {
6969
MODAL_IN_APP_NOTIFICATIONS_ENABLED("org.infobip.mobile.messaging.infobip.MODAL_IN_APP_NOTIFICATIONS_ENABLED", true),
7070
POST_NOTIFICATIONS_REQUEST_ENABLED("org.infobip.mobile.messaging.infobip.POST_NOTIFICATIONS_REQUEST_ENABLED", true),
7171
FULL_FEATURE_IN_APPS_ENABLED("org.infobip.mobile.messaging.infobip.FULL_FEATURE_IN_APPS_ENABLED", false),
72+
NOTIFICATION_CHANNEL_ID("org.infobip.mobile.messaging.infobip.NOTIFICATION_CHANNEL_ID", null),
73+
NOTIFICATION_CHANNEL_NAME("org.infobip.mobile.messaging.infobip.NOTIFICATION_CHANNEL_NAME", null),
74+
NOTIFICATION_CHANNEL_AUDIO("org.infobip.mobile.messaging.infobip.NOTIFICATION_CHANNEL_AUDIO", null),
7275
// END
7376

7477
// START: privacy settings prefs

infobip-mobile-messaging-android-sdk/src/main/java/org/infobip/mobile/messaging/notification/BaseNotificationHandler.java

+5
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ private void setNotificationPriority(NotificationCompat.Builder notificationBuil
346346
*/
347347
@NonNull
348348
private String getChannelIdForNotification(@NonNull NotificationSettings notificationSettings, Message message) {
349+
if(message.getSound() != null && !message.getSound().equals("default")) {
350+
return shouldDisplayHeadsUpNotification(notificationSettings, message)
351+
? PreferenceHelper.findString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_ID)+"_high_priority"
352+
: PreferenceHelper.findString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_ID);
353+
}
349354
return shouldDisplayHeadsUpNotification(notificationSettings, message)
350355
? MobileMessagingCore.MM_DEFAULT_HIGH_PRIORITY_CHANNEL_ID
351356
: MobileMessagingCore.MM_DEFAULT_CHANNEL_ID;

0 commit comments

Comments
 (0)