Skip to content

Commit a5b31f4

Browse files
committed
Moved Channel code into it's own class
1 parent 764f98b commit a5b31f4

File tree

7 files changed

+255
-176
lines changed

7 files changed

+255
-176
lines changed

OneSignalSDK/app/build.gradle

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ android {
2828
}
2929
}
3030

31+
repositories {
32+
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
33+
}
34+
3135
dependencies {
3236
compile fileTree(dir: 'libs', include: ['*.jar'])
3337
compile 'com.android.support:appcompat-v7:26.0.0'
@@ -41,25 +45,28 @@ dependencies {
4145
exclude group: 'com.google.android.gms', module: 'play-services-location'
4246
}
4347

48+
// Use snapshot
49+
// compile 'com.onesignal:OneSignal:4.0.0-SNAPSHOT'
50+
4451
// Use to run local .aar file
4552
// compile(name: 'OneSignal-release', ext: 'aar')
4653

4754
// New Instructions - Use for released SDK
48-
// compile 'com.onesignal:OneSignal:[3.5.3,4.0.0)'
55+
//compile 'com.onesignal:OneSignal:[3.5.8, 3.99.99]'
4956

5057
// Old Instructions - Use for released SDK
5158
// compile 'com.onesignal:OneSignal:3.+@aar'
5259

5360

5461
// Test with 7.0.0 to make sure there are no breaking changes in Google's libraries.
5562
// This insures that the SDK will work if an app developer is using an older version of GMS.
56-
compile "com.google.android.gms:play-services-gcm:11.0.2"
63+
compile "com.google.android.gms:play-services-gcm:11.0.4"
5764

5865
// play-services-analytics is required for AdvertisingIdClient when using GMS 8.1.0 or lower.
5966
// In 8.3.0 it is included in 'basement' which is required by 'base'.
6067
// compile 'com.google.android.gms:play-services-analytics:7.0.0'
6168

62-
compile "com.google.android.gms:play-services-location:11.0.2"
69+
compile "com.google.android.gms:play-services-location:11.0.4"
6370

6471

6572
// NOTES: Android Support Library v4 breakout. VERSION - 24.2.0

OneSignalSDK/onesignal/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ dependencies {
3131
compile 'com.android.support:support-v4:26.0.0'
3232

3333
// NOTE: Starting with play-services 10.2.0, minSdkVersion is now 14
34-
compile 'com.google.android.gms:play-services-gcm:11.0.2'
35-
compile 'com.google.android.gms:play-services-location:11.0.2'
34+
compile 'com.google.android.gms:play-services-gcm:11.0.4'
35+
compile 'com.google.android.gms:play-services-location:11.0.4'
3636

3737
// NOTE: customtabs sets minSdkVersion to 15.
3838
compile 'com.android.support:customtabs:26.0.0'

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

Lines changed: 3 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@
4444
import android.app.Activity;
4545
import android.app.AlertDialog;
4646
import android.app.Notification;
47-
import android.app.NotificationChannel;
48-
import android.app.NotificationChannelGroup;
49-
import android.app.NotificationManager;
5047
import android.app.PendingIntent;
5148
import android.content.ContentResolver;
5249
import android.content.ContentValues;
@@ -61,7 +58,6 @@
6158
import android.graphics.BitmapFactory;
6259
import android.net.Uri;
6360
import android.os.Build;
64-
import android.support.annotation.RequiresApi;
6561
import android.support.v4.app.NotificationCompat;
6662
import android.support.v4.app.NotificationManagerCompat;
6763
import android.text.SpannableString;
@@ -78,12 +74,6 @@ class GenerateNotification {
7874
private static Resources contextResources = null;
7975
private static Class<?> notificationOpenedClass;
8076
private static boolean openerIsBroadcast;
81-
82-
// Can't create a channel with the id 'miscellaneous' as an exception is thrown.
83-
// Using it results in the notification not being displayed.
84-
// private static final String DEFAULT_CHANNEL_ID = "miscellaneous"; // NotificationChannel.DEFAULT_CHANNEL_ID;
85-
86-
private static final String DEFAULT_CHANNEL_ID = "fcm_fallback_notification_channel";
8777

8878
private static class OneSignalNotificationBuilder {
8979
NotificationCompat.Builder compatBuilder;
@@ -182,105 +172,6 @@ else if (i == 2)
182172
});
183173
}
184174

185-
// TODO: Need to handle delete, do so on cold start up.
186-
private static String createNotificationChannel(NotificationGenerationJob notifJob) {
187-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
188-
return DEFAULT_CHANNEL_ID;
189-
190-
// TODO: Can new api be used if not targeting O but device is and have a new enough library?
191-
// If not then check for this and return.
192-
193-
JSONObject payload = notifJob.jsonPayload;
194-
195-
// Testing with additional data
196-
// JSONObject customJson = null;
197-
// try {
198-
// customJson = new JSONObject(notifJob.jsonPayload.optString("custom"));
199-
// } catch (JSONException e) {
200-
// e.printStackTrace();
201-
// }
202-
// JSONObject payload = customJson.optJSONObject("a");
203-
204-
if (!payload.has("chnl"))
205-
return createDefaultChannel();
206-
207-
try {
208-
JSONObject channelPayload = payload.getJSONObject("chnl");
209-
210-
NotificationManager notificationManager =
211-
(NotificationManager) currentContext.getSystemService(Context.NOTIFICATION_SERVICE);
212-
213-
String channel_id = channelPayload.optString("id", DEFAULT_CHANNEL_ID);
214-
// Ensure we don't try to use the system reserved id
215-
if (channel_id.equals(NotificationChannel.DEFAULT_CHANNEL_ID))
216-
channel_id = DEFAULT_CHANNEL_ID;
217-
218-
int importance = channelPayload.optInt("imp", NotificationManager.IMPORTANCE_DEFAULT);
219-
String channel_name = channelPayload.optString("nm", "Miscellaneous");
220-
221-
NotificationChannel channel = new NotificationChannel(channel_id, channel_name, importance);
222-
223-
if (channelPayload.has("grp")) {
224-
String group_id = channelPayload.optString("grp");
225-
CharSequence group_name = channelPayload.optString("grp_nm");
226-
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(group_id, group_name));
227-
channel.setGroup(group_id);
228-
}
229-
230-
channel.enableLights(channelPayload.optBoolean("lght", true));
231-
if (channelPayload.has("ledc")) {
232-
BigInteger ledColor = new BigInteger(channelPayload.optString("ledc"), 16);
233-
channel.setLightColor(ledColor.intValue());
234-
}
235-
236-
channel.enableVibration(channelPayload.optBoolean("vib", true));
237-
if (channelPayload.has("vib_pt")) {
238-
JSONArray json_vib_array = channelPayload.optJSONArray("vib_pt");
239-
long[] long_array = new long[json_vib_array.length()];
240-
for (int i = 0; i < json_vib_array.length(); i++)
241-
long_array[i] = json_vib_array.optLong(i);
242-
channel.setVibrationPattern(long_array);
243-
}
244-
245-
if (channelPayload.has("snd_nm")) {
246-
// Sound will only play if Importance is set to High or Urgent
247-
Uri uri = getCustomSound(channelPayload.optString("snd_nm", null));
248-
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "##################### channel.seSound():" + uri);
249-
channel.setSound(uri, null);
250-
}
251-
else if (!channelPayload.optBoolean("snd", true))
252-
channel.setSound(null, null);
253-
// Setting sound to null makes it 'None' in the Settings.
254-
// Otherwise not calling setSound makes it the default notification sound.
255-
256-
channel.setLockscreenVisibility(channelPayload.optInt("lck", Notification.VISIBILITY_PUBLIC));
257-
channel.enableVibration(channelPayload.optBoolean("lght", true));
258-
channel.setShowBadge(channelPayload.optBoolean("bdg", true));
259-
channel.setBypassDnd(channelPayload.optBoolean("bdnd", false));
260-
261-
notificationManager.createNotificationChannel(channel);
262-
return channel_id;
263-
} catch (JSONException e) {
264-
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Count not create notification channel due to JSON payload error!", e);
265-
}
266-
267-
return DEFAULT_CHANNEL_ID;
268-
}
269-
270-
@RequiresApi(api = Build.VERSION_CODES.O)
271-
private static String createDefaultChannel() {
272-
NotificationManager notificationManager =
273-
(NotificationManager) currentContext.getSystemService(Context.NOTIFICATION_SERVICE);
274-
275-
NotificationChannel channel = new NotificationChannel(DEFAULT_CHANNEL_ID,
276-
"Miscellaneous",
277-
NotificationManager.IMPORTANCE_DEFAULT);
278-
channel.enableLights(true);
279-
channel.enableVibration(true);
280-
notificationManager.createNotificationChannel(channel);
281-
return DEFAULT_CHANNEL_ID;
282-
}
283-
284175
private static CharSequence getTitle(JSONObject gcmBundle) {
285176
CharSequence title = gcmBundle.optString("title", null);
286177

@@ -321,7 +212,7 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
321212

322213
NotificationCompat.Builder notifBuilder;
323214
try {
324-
String channelId = createNotificationChannel(notifJob);
215+
String channelId = NotificationChannelManager.createNotificationChannel(currentContext, notifJob.jsonPayload);
325216
// Will throw if app is using 26.0.0-beta1 or older of the support library.
326217
notifBuilder = new NotificationCompat.Builder(currentContext, channelId);
327218
} catch(Throwable t) {
@@ -382,7 +273,7 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
382273
notifBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bigPictureIcon).setSummaryText(message));
383274

384275
if (isSoundEnabled(gcmBundle)) {
385-
Uri soundUri = getCustomSound(gcmBundle.optString("sound", null));
276+
Uri soundUri = OSUtils.getSoundUri(currentContext, gcmBundle.optString("sound", null));
386277
if (soundUri != null)
387278
notifBuilder.setSound(soundUri);
388279
else
@@ -819,10 +710,6 @@ private static Integer safeGetColorFromHex(JSONObject gcmBundle, String colorKey
819710
return null;
820711
}
821712

822-
private static boolean isValidResourceName(String name) {
823-
return (name != null && !name.matches("^[0-9]"));
824-
}
825-
826713
private static Bitmap getLargeIcon(JSONObject gcmBundle) {
827714
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
828715
return null;
@@ -927,7 +814,7 @@ private static int getResourceIcon(String iconName) {
927814
return 0;
928815

929816
String trimmedIconName = iconName.trim();
930-
if (!isValidResourceName(trimmedIconName))
817+
if (!OSUtils.isValidResourceName(trimmedIconName))
931818
return 0;
932819

933820
int notificationIcon = getDrawableId(trimmedIconName);
@@ -977,22 +864,6 @@ private static boolean isSoundEnabled(JSONObject gcmBundle) {
977864
return OneSignal.getSoundEnabled(currentContext);
978865
}
979866

980-
private static Uri getCustomSound(String sound) {
981-
int soundId;
982-
983-
if (isValidResourceName(sound)) {
984-
soundId = contextResources.getIdentifier(sound, "raw", packageName);
985-
if (soundId != 0)
986-
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + packageName + "/" + soundId);
987-
}
988-
989-
soundId = contextResources.getIdentifier("onesignal_default_sound", "raw", packageName);
990-
if (soundId != 0)
991-
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + packageName + "/" + soundId);
992-
993-
return null;
994-
}
995-
996867
// Android 5.0 accent color to use, only works when AndroidManifest.xml is targetSdkVersion >= 21
997868
private static BigInteger getAccentColor(JSONObject gcmBundle) {
998869
try {

0 commit comments

Comments
 (0)