Skip to content

Commit 970eb89

Browse files
committed
Moved more services into ScheduledJobs
* WIP NotificationExtenderService needs to be reworked to decouple it from it's own IntentService classContext - Reworked implementation should not extend from a Context class. One will be passed in. * Created a wrapper for PersistableBundle and Bundle classes to clean up code. * Fixed sound and vibration for each notification when restored on cold start of app. - Still issue when rebooting and updating the app
1 parent 7783699 commit 970eb89

18 files changed

+337
-107
lines changed

OneSignalSDK/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
maven { url 'https://maven.google.com' }
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.0.0-alpha6'
9+
classpath 'com.android.tools.build:gradle:3.0.0-alpha7'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files

OneSignalSDK/onesignal/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ android {
55
buildToolsVersion '26.0.0'
66

77
defaultConfig {
8-
minSdkVersion 14
8+
minSdkVersion 15
99
consumerProguardFiles 'consumer-proguard-rules.pro'
1010
}
1111

@@ -29,11 +29,11 @@ dependencies {
2929
provided fileTree(dir: 'libs', include: ['*.jar'])
3030

3131
// NOTE: Starting with play-services 10.2.0, minSdkVersion is now 14
32-
compile 'com.google.android.gms:play-services-gcm:10.0.1'
33-
compile 'com.google.android.gms:play-services-location:10.0.1'
32+
compile 'com.google.android.gms:play-services-gcm:11.0.2'
33+
compile 'com.google.android.gms:play-services-location:11.0.2'
3434

3535
// NOTE: customtabs sets minSdkVersion to 15.
36-
compile 'com.android.support:customtabs:26.+'
36+
compile 'com.android.support:customtabs:26.0.0-beta2'
3737
}
3838

3939
// Remove OnActivityPausedListener class.

OneSignalSDK/onesignal/src/main/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@
7070
android:permission="android.permission.BIND_JOB_SERVICE" />
7171

7272
<service android:name="com.onesignal.SyncService" />
73-
<activity android:name="com.onesignal.PermissionsActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
73+
<activity android:name="com.onesignal.PermissionsActivity"
74+
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
7475

7576
<service android:name="com.onesignal.NotificationRestoreService" />
77+
<!-- For Android O -->
78+
<service android:name="com.onesignal.NotificationRestoreJobService"
79+
android:permission="android.permission.BIND_JOB_SERVICE" />
7680
<receiver android:name="com.onesignal.BootUpReceiver">
7781
<intent-filter>
7882
<action android:name="android.intent.action.BOOT_COMPLETED" />

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
package com.onesignal;
2929

30-
import android.content.ComponentName;
3130
import android.content.Context;
3231
import android.content.Intent;
3332
import android.support.v4.content.WakefulBroadcastReceiver;
@@ -36,9 +35,6 @@ public class BootUpReceiver extends WakefulBroadcastReceiver {
3635

3736
@Override
3837
public void onReceive(Context context, Intent intent) {
39-
Intent intentForService = new Intent();
40-
intentForService.setComponent(new ComponentName(context.getPackageName(),
41-
NotificationRestoreService.class.getName()));
42-
startWakefulService(context, intentForService);
38+
NotificationRestorer.startRestoreTaskFromReceiver(this, context);
4339
}
4440
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package com.onesignal;
2+
3+
import android.content.Intent;
4+
import android.os.Build;
5+
import android.os.Bundle;
6+
import android.os.PersistableBundle;
7+
import android.support.annotation.RequiresApi;
8+
9+
interface BundleCompat<T> {
10+
void putString(String key, String value);
11+
void putLong(String key, Long value);
12+
void putBoolean(String key, Boolean value);
13+
14+
String getString(String key);
15+
Integer getInt(String key);
16+
Long getLong(String key);
17+
boolean getBoolean(String key);
18+
boolean getBoolean(String key, boolean value);
19+
20+
boolean containsKey(String key);
21+
22+
T getBundle();
23+
}
24+
25+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
26+
class BundleCompatPersistableBundle implements BundleCompat<PersistableBundle> {
27+
private PersistableBundle mBundle;
28+
29+
BundleCompatPersistableBundle() {
30+
mBundle = new PersistableBundle();
31+
}
32+
33+
BundleCompatPersistableBundle(PersistableBundle bundle) {
34+
mBundle = bundle;
35+
}
36+
37+
@Override
38+
public void putString(String key, String value) {
39+
mBundle.putString(key, value);
40+
}
41+
42+
@Override
43+
public void putLong(String key, Long value) {
44+
mBundle.putLong(key, value);
45+
}
46+
47+
@Override
48+
public void putBoolean(String key, Boolean value) {
49+
mBundle.putBoolean(key, value);
50+
}
51+
52+
@Override
53+
public String getString(String key) {
54+
return mBundle.getString(key);
55+
}
56+
57+
@Override
58+
public Integer getInt(String key) {
59+
return mBundle.getInt(key);
60+
}
61+
62+
@Override
63+
public Long getLong(String key) {
64+
return mBundle.getLong(key);
65+
}
66+
67+
@Override
68+
public boolean getBoolean(String key) {
69+
return mBundle.getBoolean(key);
70+
}
71+
72+
@Override
73+
public boolean getBoolean(String key, boolean value) {
74+
return mBundle.getBoolean(key, value);
75+
}
76+
77+
@Override
78+
public boolean containsKey(String key) {
79+
return mBundle.containsKey(key);
80+
}
81+
82+
@Override
83+
public PersistableBundle getBundle() {
84+
return mBundle;
85+
}
86+
}
87+
88+
class BundleCompatBundle implements BundleCompat<Bundle> {
89+
private Bundle mBundle;
90+
91+
BundleCompatBundle() {
92+
mBundle = new Bundle();
93+
}
94+
95+
BundleCompatBundle(Bundle bundle) {
96+
mBundle = bundle;
97+
}
98+
99+
BundleCompatBundle(Intent intent) {
100+
mBundle = intent.getExtras();
101+
}
102+
103+
@Override
104+
public void putString(String key, String value) {
105+
mBundle.putString(key, value);
106+
}
107+
108+
@Override
109+
public void putLong(String key, Long value) {
110+
mBundle.putLong(key, value);
111+
}
112+
113+
@Override
114+
public void putBoolean(String key, Boolean value) {
115+
mBundle.putBoolean(key, value);
116+
}
117+
118+
@Override
119+
public String getString(String key) {
120+
return mBundle.getString(key);
121+
}
122+
123+
@Override
124+
public Integer getInt(String key) {
125+
return mBundle.getInt(key);
126+
}
127+
128+
@Override
129+
public Long getLong(String key) {
130+
return mBundle.getLong(key);
131+
}
132+
133+
@Override
134+
public boolean getBoolean(String key) {
135+
return mBundle.getBoolean(key);
136+
}
137+
138+
@Override
139+
public boolean containsKey(String key) {
140+
return mBundle.containsKey(key);
141+
}
142+
143+
@Override
144+
public Bundle getBundle() {
145+
return mBundle;
146+
}
147+
148+
@Override
149+
public boolean getBoolean(String key, boolean value) {
150+
return mBundle.getBoolean(key, value);
151+
}
152+
}
153+
154+
class BundleCompatFactory {
155+
static BundleCompat getInstance() {
156+
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
157+
return new BundleCompatPersistableBundle();
158+
return new BundleCompatBundle();
159+
}
160+
}

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static boolean isGcmMessage(Intent intent) {
5858

5959
@Override
6060
public void onReceive(Context context, Intent intent) {
61-
// Google Play services started sending an extra non-ordered broadcast with the bundle:
61+
// Google Play services started sending an extra non-ordered broadcast with the mBundle:
6262
// { "COM": "RST_FULL", "from": "google.com/iid" }
6363
// Result codes are not valid with non-ordered broadcasts so omit it to prevent errors to the logcat.
6464
Bundle bundle = intent.getExtras();
@@ -120,31 +120,32 @@ private static void startGCMService(Context context, Bundle bundle) {
120120
// 1. No remote resource needs to be loaded. Starts with "http"
121121
// - Check large icon, big picture, and background image
122122

123-
// TODO: Only JobScheduler implementation to only IF
123+
BundleCompat taskExtras = BundleCompatFactory.getInstance();
124+
taskExtras.putString("json_payload", NotificationBundleProcessor.bundleAsJSONObject(bundle).toString());
125+
taskExtras.putLong("timestamp", System.currentTimeMillis() / 1000L);
126+
127+
// TODO: Only use JobScheduler implementation if:
124128
// 1. If GCM payload priority is not high.
125129
// - startWakefulService will work in this case on O.
126130
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
127-
ComponentName componentName = new ComponentName(context.getPackageName(), GcmIntentJobService.class.getName());
128-
129-
PersistableBundle extras = new PersistableBundle();
130-
extras.putString("json_payload", NotificationBundleProcessor.bundleAsJSONObject(bundle).toString());
131-
extras.putLong("timestamp", System.currentTimeMillis() / 1000L);
132-
131+
ComponentName componentName = new ComponentName(context.getPackageName(),
132+
GcmIntentJobService.class.getName());
133+
133134
Random random = new Random();
134135
JobInfo jobInfo = new JobInfo.Builder(random.nextInt(), componentName)
135136
.setOverrideDeadline(100)
136-
.setExtras(extras)
137+
.setExtras((PersistableBundle)taskExtras.getBundle())
137138
.build();
138139
JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
139140
jobScheduler.schedule(jobInfo);
140141
}
141142
else {
142-
ComponentName componentName = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
143+
ComponentName componentName = new ComponentName(context.getPackageName(),
144+
GcmIntentService.class.getName());
143145

144-
Intent intentForService = new Intent();
145-
intentForService.putExtra("json_payload", NotificationBundleProcessor.bundleAsJSONObject(bundle).toString());
146-
intentForService.putExtra("timestamp", System.currentTimeMillis() / 1000L);
147-
intentForService.setComponent(componentName);
146+
Intent intentForService = new Intent()
147+
.replaceExtras((Bundle)taskExtras.getBundle())
148+
.setComponent(componentName);
148149
startWakefulService(context, intentForService);
149150
}
150151
}

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

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,15 @@
22

33
import android.app.job.JobParameters;
44
import android.app.job.JobService;
5-
import android.content.Context;
65
import android.os.Build;
7-
import android.os.Bundle;
8-
import android.os.PersistableBundle;
96
import android.support.annotation.RequiresApi;
10-
import android.util.Log;
117

12-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
13-
public class GcmIntentJobService extends JobService {
14-
15-
// - Runs on the main thread.
16-
// - Wakelock is held until jobFinished is called.
8+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
9+
public class GcmIntentJobService extends OneSignalJobServiceBase {
1710
@Override
18-
public boolean onStartJob(JobParameters jobParameters) {
19-
final PersistableBundle extras = jobParameters.getExtras();
20-
if (extras == null)
21-
return false;
22-
23-
final JobService jobService = this;
24-
final JobParameters finalJobParameters = jobParameters;
25-
new Thread(new Runnable() {
26-
public void run() {
27-
startProcessing(jobService, finalJobParameters, extras);
28-
}
29-
}).start();
30-
31-
// true as we created a thread and need it to hold a wakelock.
32-
return true;
33-
}
34-
35-
private static void startProcessing(JobService jobService, JobParameters jobParameters, PersistableBundle extras) {
36-
Bundle bundle = new Bundle();
37-
for(String key : extras.keySet()) {
38-
Object obj = extras.get(key);
39-
if (obj instanceof String)
40-
bundle.putString(key, (String)obj);
41-
else if (obj instanceof Long)
42-
bundle.putLong(key, (Long)obj);
43-
}
44-
45-
46-
//
47-
// bundle.putString("json_payload", extras.getString("json_payload"));
48-
// bundle.putLong("timestamp", extras.getLong("timestamp"));
49-
50-
NotificationBundleProcessor.ProcessFromGCMIntentService(jobService, bundle, null);
51-
52-
// Must be called to release the wake lock.
53-
jobService.jobFinished(jobParameters, false);
54-
}
55-
56-
@Override
57-
public boolean onStopJob(JobParameters jobParameters) {
58-
// TODO: Check if this normally fires. When jobFinished is called maybe?
59-
Log.e("OneSignal", "GcmIntentJobService.onStopJob!!!!!!!!!!!!!!!!!!!!!!!");
60-
return true;
11+
void startProcessing(JobService jobService, JobParameters jobParameters) {
12+
NotificationBundleProcessor.ProcessFromGCMIntentService(jobService,
13+
new BundleCompatPersistableBundle(jobParameters.getExtras()),
14+
null);
6115
}
6216
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ protected void onHandleIntent(Intent intent) {
5757
if (extras == null)
5858
return;
5959

60-
NotificationBundleProcessor.ProcessFromGCMIntentService(this, extras, null);
60+
NotificationBundleProcessor.ProcessFromGCMIntentService(this,
61+
new BundleCompatBundle(extras),
62+
null);
6163

6264
// Release the wake lock provided by the WakefulBroadcastReceiver.
6365
GcmBroadcastReceiver.completeWakefulIntent(intent);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ private static OneSignalNotificationBuilder getBaseOneSignalNotificationBuilder(
314314
}
315315

316316
private static void removeNotifyOptions(NotificationCompat.Builder builder) {
317-
builder.setDefaults(0)
317+
builder.setOnlyAlertOnce(true)
318+
.setDefaults(0)
318319
.setSound(null)
319320
.setVibrate(null)
320321
.setTicker(null);
@@ -381,7 +382,7 @@ private static void showNotification(NotificationGenerationJob notifJob) {
381382
// stacked notifications on Android 4.2 and older
382383
// The benefits of calling notify for individual notifications in-addition to the summary above it is shows
383384
// each notification in a stack on Android Wear and each one is actionable just like the Gmail app does per email.
384-
// Note that on Android 7.0 this is the opposite. Only individual notifications will show and bundle / group is
385+
// Note that on Android 7.0 this is the opposite. Only individual notifications will show and mBundle / group is
385386
// created by Android itself.
386387
if (group == null || Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
387388
addXiaomiSettings(oneSignalNotificationBuilder, notification);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ class NotificationBundleProcessor {
4949

5050
static final String DEFAULT_ACTION = "__DEFAULT__";
5151

52-
static void ProcessFromGCMIntentService(Context context, Bundle bundle, NotificationExtenderService.OverrideSettings overrideSettings) {
52+
static void ProcessFromGCMIntentService(Context context, BundleCompat bundle, NotificationExtenderService.OverrideSettings overrideSettings) {
5353
try {
5454
String jsonStrPayload = bundle.getString("json_payload");
5555
if (jsonStrPayload == null) {
56-
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "json_payload key is nonexistent from bundle passed to ProcessFromGCMIntentService: " + bundle);
56+
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "json_payload key is nonexistent from mBundle passed to ProcessFromGCMIntentService: " + bundle);
5757
return;
5858
}
5959

0 commit comments

Comments
 (0)