Skip to content

Commit 5ade1b8

Browse files
committed
Notification is generated from receiver if no remote files
* This to prevent creating a JobService or Service when not needed. * Also removed some unneeded pre-4.0 Android version checks.
1 parent b918ff3 commit 5ade1b8

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2017 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
129
package com.onesignal;
230

331
import android.content.Intent;

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void onReceive(Context context, Intent intent) {
6464
Bundle bundle = intent.getExtras();
6565
if (bundle == null || "google.com/iid".equals(bundle.getString("from")))
6666
return;
67-
67+
6868
ProcessedBundleResult processedResult = processOrderBroadcast(context, intent, bundle);
6969

7070
// Null means this isn't a GCM / FCM message.
@@ -120,18 +120,18 @@ private static ProcessedBundleResult processOrderBroadcast(Context context, Inte
120120
}
121121

122122
private static void startGCMService(Context context, Bundle bundle) {
123-
// TODO: Display notification directly if from BroadcastReceiver IF
124-
// 1. No remote resource needs to be loaded. Starts with "http"
125-
// - Check large icon, big picture, and background image
126-
127123
BundleCompat taskExtras = BundleCompatFactory.getInstance();
128124
taskExtras.putString("json_payload", NotificationBundleProcessor.bundleAsJSONObject(bundle).toString());
129125
taskExtras.putLong("timestamp", System.currentTimeMillis() / 1000L);
130126

131-
// TODO: Only use JobScheduler implementation if:
132-
// 1. If GCM payload priority is not high.
133-
// - startWakefulService will work in this case on O.
134-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
127+
// If no remote resources have to be downloaded don't create a job which could add some delay.
128+
if (!NotificationBundleProcessor.hasRemoteResource(bundle)) {
129+
NotificationBundleProcessor.ProcessFromGCMIntentService(context, taskExtras, null);
130+
return;
131+
}
132+
133+
boolean isHighPriority = Integer.parseInt(bundle.getString("pri", "0")) > 9;
134+
if (!isHighPriority && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
135135
ComponentName componentName = new ComponentName(context.getPackageName(),
136136
GcmIntentJobService.class.getName());
137137
Random random = new Random();
@@ -150,7 +150,6 @@ private static void startGCMService(Context context, Bundle bundle) {
150150
else {
151151
ComponentName componentName = new ComponentName(context.getPackageName(),
152152
GcmIntentService.class.getName());
153-
154153
Intent intentForService = new Intent()
155154
.replaceExtras((Bundle)taskExtras.getBundle())
156155
.setComponent(componentName);

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,6 @@ private static Notification createSingleNotificationBeforeSummaryBuilder(Notific
391391
// Without this MIUI 8 will only show the app icon on the left.
392392
// When a large icon is set the small icon will no longer show.
393393
private static void addXiaomiSettings(OneSignalNotificationBuilder oneSignalNotificationBuilder, Notification notification) {
394-
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
395-
return;
396-
397394
// Don't use unless a large icon is set.
398395
// The small white notification icon is hard to see with MIUI default light theme.
399396
if (!oneSignalNotificationBuilder.hasLargeIcon)
@@ -711,9 +708,6 @@ private static Integer safeGetColorFromHex(JSONObject gcmBundle, String colorKey
711708
}
712709

713710
private static Bitmap getLargeIcon(JSONObject gcmBundle) {
714-
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
715-
return null;
716-
717711
Bitmap bitmap = getBitmap(gcmBundle.optString("licon"));
718712
if (bitmap == null)
719713
bitmap = getBitmapFromAssetsOrResourceName("ic_onesignal_large_icon_default");
@@ -725,9 +719,6 @@ private static Bitmap getLargeIcon(JSONObject gcmBundle) {
725719
}
726720

727721
private static Bitmap getDefaultLargeIcon() {
728-
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
729-
return null;
730-
731722
Bitmap bitmap = getBitmapFromAssetsOrResourceName("ic_onesignal_large_icon_default");
732723
return resizeBitmapForLargeIconArea(bitmap);
733724
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,16 @@ static JSONArray newJsonArray(JSONObject jsonObject) {
438438
return jsonArray;
439439
}
440440

441+
static boolean hasRemoteResource(Bundle bundle) {
442+
return isBuildKeyRemote(bundle, "licon")
443+
|| isBuildKeyRemote(bundle, "bicon")
444+
|| bundle.getString("bg_img", null) != null;
445+
}
446+
447+
private static boolean isBuildKeyRemote(Bundle bundle, String key) {
448+
String value = bundle.getString(key, "").trim();
449+
return value.startsWith("http://") || value.startsWith("https://");
450+
}
441451

442452
static class ProcessedBundleResult {
443453
boolean isOneSignalPayload;

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import android.content.ComponentName;
3131
import android.content.Context;
3232
import android.net.Uri;
33-
import android.os.Build;
3433
import android.os.Bundle;
3534
import android.support.customtabs.CustomTabsCallback;
3635
import android.support.customtabs.CustomTabsClient;
@@ -44,11 +43,6 @@ class OneSignalChromeTab {
4443
private static boolean opened;
4544

4645
static void setup(Context context, String appId, String userId, String adId) {
47-
// Min Android API Level is set to 9 in the aar's AndroidManifest.xml for capability.
48-
// We need to check for compatibility at runtime for Chrome tabs then.
49-
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH)
50-
return;
51-
5246
if (opened)
5347
return;
5448

0 commit comments

Comments
 (0)