Skip to content

Commit 8025425

Browse files
committed
Handle JobIntentService IllegalArgumentException
* This handles the IllegalArgumentException which can throw on Oreo+ when a high priority notification is sent and when a NotificationExtenderService is setup. - Only thrown in the case where FCM could not add the app to the whitelist.
1 parent ccbd4e3 commit 8025425

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
// This allows using ether a IntentService even for Android 8.0 (API 26) when needed.
2424
// - This allows starting NotificationExtenderService on a high priority FCM message
2525
// - Even when the device is in doze mode.
26+
// Has fallback handling to job service if IllegalStateException is thrown when starting service.
27+
// ==== Testing ====
2628
// - The following can be used to confirm;
2729
// - adb shell dumpsys deviceidle force-idle
2830
// - And the following to undo
@@ -314,7 +316,7 @@ public boolean onStartJob(JobParameters params) {
314316

315317
@Override
316318
public boolean onStopJob(JobParameters params) {
317-
if (DEBUG) Log.d(TAG, "onStartJob: " + params);
319+
if (DEBUG) Log.d(TAG, "onStopJob: " + params);
318320
boolean result = mService.doStopCurrentWork();
319321
synchronized (mLock) {
320322
// Once we return, the job is stopped, so its JobParameters are no
@@ -531,7 +533,21 @@ public static void enqueueWork(@NonNull Context context, @NonNull ComponentName
531533
synchronized (sLock) {
532534
WorkEnqueuer we = getWorkEnqueuer(context, component, true, jobId, useWakefulService);
533535
we.ensureJobId(jobId);
534-
we.enqueueWork(work);
536+
537+
// Can throw on API 26+ if useWakefulService=true and app is NOT whitelisted.
538+
// One example is when an FCM high priority message is received the system will
539+
// temporarily whitelist the app. However it is possible that it does not end up getting
540+
// whitelisted so we need to catch this and fall back to a job service.
541+
try {
542+
we.enqueueWork(work);
543+
} catch (IllegalStateException e) {
544+
if (useWakefulService) {
545+
we = getWorkEnqueuer(context, component, true, jobId, false);
546+
we.enqueueWork(work);
547+
}
548+
else
549+
throw e;
550+
}
535551
}
536552
}
537553

0 commit comments

Comments
 (0)