|
1 | 1 | package org.infobip.mobile.messaging;
|
2 | 2 |
|
| 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 | + |
3 | 7 | import android.app.Application;
|
4 | 8 | import android.app.NotificationChannel;
|
5 | 9 | import android.app.NotificationManager;
|
| 10 | +import android.content.ContentResolver; |
6 | 11 | import android.content.Context;
|
| 12 | +import android.media.AudioAttributes; |
| 13 | +import android.net.Uri; |
7 | 14 | import android.os.Build;
|
8 | 15 | import android.text.TextUtils;
|
9 | 16 | import android.util.Pair;
|
10 | 17 |
|
| 18 | +import androidx.annotation.NonNull; |
| 19 | +import androidx.annotation.Nullable; |
| 20 | +import androidx.core.app.NotificationChannelCompat; |
| 21 | +import androidx.core.app.NotificationManagerCompat; |
| 22 | + |
11 | 23 | import com.google.firebase.FirebaseApp;
|
12 | 24 | import com.google.firebase.FirebaseOptions;
|
13 | 25 |
|
|
75 | 87 | import org.infobip.mobile.messaging.util.StringUtils;
|
76 | 88 | import org.infobip.mobile.messaging.util.SystemInformation;
|
77 | 89 |
|
| 90 | +import java.io.IOException; |
78 | 91 | import java.lang.reflect.Method;
|
79 | 92 | import java.security.MessageDigest;
|
80 | 93 | import java.util.ArrayList;
|
|
93 | 106 | import java.util.concurrent.TimeUnit;
|
94 | 107 | import java.util.regex.Pattern;
|
95 | 108 |
|
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 |
| - |
103 | 109 | /**
|
104 | 110 | * @author sslavin
|
105 | 111 | * @since 28.04.2016.
|
@@ -186,6 +192,11 @@ protected MobileMessagingCore(Context context, Broadcaster broadcaster, Executor
|
186 | 192 | ComponentUtil.setConnectivityComponentsStateEnabled(context, true);
|
187 | 193 |
|
188 | 194 | initDefaultChannels();
|
| 195 | + |
| 196 | + if (PreferenceHelper.findString(context, MobileMessagingProperty.NOTIFICATION_CHANNEL_AUDIO) != null) { |
| 197 | + initCustomChannels(); |
| 198 | + } |
| 199 | + |
189 | 200 | migratePrefsIfNecessary(context);
|
190 | 201 |
|
191 | 202 | this.installationId = getUniversalInstallationId();
|
@@ -264,6 +275,52 @@ private void initDefaultChannels() {
|
264 | 275 | }
|
265 | 276 | }
|
266 | 277 |
|
| 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 | + |
267 | 324 | /**
|
268 | 325 | * Gets an instance of MobileMessagingCore after it is initialized via {@link MobileMessagingCore.Builder}.
|
269 | 326 | * <br>
|
@@ -1300,6 +1357,12 @@ static void setFullFeatureInAppsEnabled(Context context, boolean fullFeaturedInA
|
1300 | 1357 | PreferenceHelper.saveBoolean(context, MobileMessagingProperty.FULL_FEATURE_IN_APPS_ENABLED, fullFeaturedInApps);
|
1301 | 1358 | }
|
1302 | 1359 |
|
| 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 | + |
1303 | 1366 | public static void setShouldSaveUserData(Context context, boolean shouldSaveUserData) {
|
1304 | 1367 | PreferenceHelper.saveBoolean(context, MobileMessagingProperty.SAVE_USER_DATA_ON_DISK, shouldSaveUserData);
|
1305 | 1368 | }
|
@@ -1374,6 +1437,8 @@ private static void cleanup(Context context) {
|
1374 | 1437 | PreferenceHelper.remove(context, MobileMessagingProperty.BASEURL_CHECK_INTERVAL_HOURS);
|
1375 | 1438 | PreferenceHelper.remove(context, MobileMessagingProperty.POST_NOTIFICATIONS_REQUEST_ENABLED);
|
1376 | 1439 | 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); |
1377 | 1442 |
|
1378 | 1443 | MobileMessagingCore mmCore = Platform.mobileMessagingCore.get(context);
|
1379 | 1444 | mmCore.messagesSynchronizer = null;
|
|
0 commit comments