Skip to content

Commit 4a5fa0d

Browse files
authored
Merge pull request #1290 from OneSignal/improvement/thread-access-db
Improve thread DB access handling
2 parents fc1ebe0 + b5b1792 commit 4a5fa0d

21 files changed

+735
-439
lines changed

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import android.content.Intent;
3232
import android.os.Bundle;
3333

34+
import androidx.annotation.Nullable;
35+
3436
import com.amazon.device.messaging.ADMMessageHandlerJobBase;
3537
import com.amazon.device.messaging.ADMMessageHandlerBase;
3638
import com.amazon.device.messaging.ADMMessageReceiver;
@@ -68,24 +70,30 @@ public ADMMessageHandler() {
6870

6971
@Override
7072
protected void onMessage(Intent intent) {
71-
Context context = getApplicationContext();
72-
Bundle bundle = intent.getExtras();
73-
74-
NotificationBundleProcessor.ProcessedBundleResult processedResult = NotificationBundleProcessor.processBundleFromReceiver(context, bundle);
75-
// TODO: Figure out the correct replacement or usage of completeWakefulIntent method
76-
// FCMBroadcastReceiver.completeWakefulIntent(intent);
73+
final Context context = getApplicationContext();
74+
final Bundle bundle = intent.getExtras();
75+
76+
NotificationBundleProcessor.ProcessBundleReceiverCallback bundleReceiverCallback = new NotificationBundleProcessor.ProcessBundleReceiverCallback() {
77+
@Override
78+
public void onBundleProcessed(@Nullable NotificationBundleProcessor.ProcessedBundleResult processedResult) {
79+
// TODO: Figure out the correct replacement or usage of completeWakefulIntent method
80+
// FCMBroadcastReceiver.completeWakefulIntent(intent);
7781

78-
if (processedResult.processed())
79-
return;
82+
if (processedResult.processed())
83+
return;
8084

81-
JSONObject payload = NotificationBundleProcessor.bundleAsJSONObject(bundle);
82-
OSNotification notification = new OSNotification(payload);
85+
JSONObject payload = NotificationBundleProcessor.bundleAsJSONObject(bundle);
86+
OSNotification notification = new OSNotification(payload);
87+
88+
OSNotificationGenerationJob notificationJob = new OSNotificationGenerationJob(context);
89+
notificationJob.setJsonPayload(payload);
90+
notificationJob.setContext(context);
91+
notificationJob.setNotification(notification);
92+
NotificationBundleProcessor.processJobForDisplay(notificationJob, true);
93+
}
94+
};
95+
NotificationBundleProcessor.processBundleFromReceiver(context, bundle, bundleReceiverCallback);
8396

84-
OSNotificationGenerationJob notificationJob = new OSNotificationGenerationJob(context);
85-
notificationJob.setJsonPayload(payload);
86-
notificationJob.setContext(context);
87-
notificationJob.setNotification(notification);
88-
NotificationBundleProcessor.processJobForDisplay(notificationJob, true);
8997
}
9098

9199
@Override

OneSignalSDK/onesignal/src/main/java/com/onesignal/ADMMessageHandlerJob.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,29 @@ class ADMMessageHandlerJob : ADMMessageHandlerJobBase() {
99
override fun onMessage(context: Context?, intent: Intent?) {
1010
val bundle = intent?.extras
1111

12-
val processedResult = NotificationBundleProcessor.processBundleFromReceiver(context, bundle)
13-
// TODO: Figure out the correct replacement or usage of completeWakefulIntent method
14-
// FCMBroadcastReceiver.completeWakefulIntent(intent);
15-
16-
if (processedResult.processed()) return
17-
18-
val payload = NotificationBundleProcessor.bundleAsJSONObject(bundle)
19-
val notification = OSNotification(payload)
20-
21-
val notificationJob = OSNotificationGenerationJob(context).apply {
22-
this.jsonPayload = payload
23-
this.context = context
24-
this.notification = notification
12+
val bundleReceiverCallback = object : NotificationBundleProcessor.ProcessBundleReceiverCallback {
13+
override fun onBundleProcessed(processedResult: NotificationBundleProcessor.ProcessedBundleResult?) {
14+
// TODO: Figure out the correct replacement or usage of completeWakefulIntent method
15+
// FCMBroadcastReceiver.completeWakefulIntent(intent);
16+
17+
processedResult?.let {
18+
if (it.processed()) return
19+
}
20+
21+
val payload = NotificationBundleProcessor.bundleAsJSONObject(bundle)
22+
val notification = OSNotification(payload)
23+
24+
val notificationJob = OSNotificationGenerationJob(context).apply {
25+
this.jsonPayload = payload
26+
this.context = context
27+
this.notification = notification
28+
}
29+
30+
NotificationBundleProcessor.processJobForDisplay(notificationJob, true)
31+
}
2532
}
2633

27-
NotificationBundleProcessor.processJobForDisplay(notificationJob, true)
34+
NotificationBundleProcessor.processBundleFromReceiver(context, bundle, bundleReceiverCallback)
2835
}
2936

3037
override fun onRegistered(context: Context?, newRegistrationId: String?) {

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

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,30 @@ public void onReceive(Context context, Intent intent) {
6666
return;
6767

6868
OneSignal.initWithContext(context);
69-
ProcessedBundleResult processedResult = processOrderBroadcast(context, intent, bundle);
7069

71-
// Null means this isn't a FCM message
72-
if (processedResult == null) {
73-
setSuccessfulResultCode();
74-
return;
75-
}
76-
77-
// Prevent other FCM receivers from firing if:
78-
// 1. This is a duplicated FCM message
79-
// 2. OR work manager is processing the notification
80-
if (processedResult.isDup || processedResult.isWorkManagerProcessing) {
81-
// Abort to prevent other FCM receivers from process this Intent.
82-
setAbort();
83-
return;
84-
}
85-
86-
setSuccessfulResultCode();
70+
NotificationBundleProcessor.ProcessBundleReceiverCallback bundleReceiverCallback = new NotificationBundleProcessor.ProcessBundleReceiverCallback() {
71+
72+
@Override
73+
public void onBundleProcessed(@Nullable ProcessedBundleResult processedResult) {
74+
// Null means this isn't a FCM message
75+
if (processedResult == null) {
76+
setSuccessfulResultCode();
77+
return;
78+
}
79+
80+
// Prevent other FCM receivers from firing if:
81+
// 1. This is a duplicated FCM message
82+
// 2. OR work manager is processing the notification
83+
if (processedResult.isDup || processedResult.isWorkManagerProcessing) {
84+
// Abort to prevent other FCM receivers from process this Intent.
85+
setAbort();
86+
return;
87+
}
88+
89+
setSuccessfulResultCode();
90+
}
91+
};
92+
processOrderBroadcast(context, intent, bundle, bundleReceiverCallback);
8793
}
8894

8995
private void setSuccessfulResultCode() {
@@ -109,19 +115,26 @@ private void setAbort() {
109115
}
110116
}
111117

112-
private static @Nullable ProcessedBundleResult processOrderBroadcast(Context context, Intent intent, Bundle bundle) {
118+
private static void processOrderBroadcast(final Context context, Intent intent, final Bundle bundle,
119+
final NotificationBundleProcessor.ProcessBundleReceiverCallback fcmBundleReceiver) {
113120
if (!isFCMMessage(intent))
114-
return null;
121+
fcmBundleReceiver.onBundleProcessed(null);
115122

116-
ProcessedBundleResult processedResult = NotificationBundleProcessor.processBundleFromReceiver(context, bundle);
123+
NotificationBundleProcessor.ProcessBundleReceiverCallback bundleReceiverCallback = new NotificationBundleProcessor.ProcessBundleReceiverCallback() {
124+
@Override
125+
public void onBundleProcessed(@Nullable ProcessedBundleResult processedResult) {
126+
// Return if the notification will NOT be handled by normal FCMIntentService display flow.
127+
if (processedResult!= null && processedResult.processed()){
128+
fcmBundleReceiver.onBundleProcessed(processedResult);
129+
return;
130+
}
117131

118-
// Return if the notification will NOT be handled by normal FCMIntentService display flow.
119-
if (processedResult.processed())
120-
return processedResult;
132+
startFCMService(context, bundle);
121133

122-
startFCMService(context, bundle);
123-
124-
return processedResult;
134+
fcmBundleReceiver.onBundleProcessed(processedResult);
135+
}
136+
};
137+
NotificationBundleProcessor.processBundleFromReceiver(context, bundle, bundleReceiverCallback);
125138
}
126139

127140
static void startFCMService(Context context, Bundle bundle) {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.os.Bundle;
77

88
import androidx.annotation.NonNull;
9+
import androidx.annotation.Nullable;
910
import androidx.annotation.RequiresApi;
1011

1112
import static com.onesignal.NotificationBundleProcessor.processBundleFromReceiver;
@@ -25,7 +26,12 @@ protected void onHandleWork(@NonNull Intent intent) {
2526
if (bundle == null)
2627
return;
2728

28-
processBundleFromReceiver(this, bundle);
29+
processBundleFromReceiver(this, bundle, new NotificationBundleProcessor.ProcessBundleReceiverCallback() {
30+
@Override
31+
public void onBundleProcessed(@Nullable NotificationBundleProcessor.ProcessedBundleResult processedResult) {
32+
33+
}
34+
});
2935
}
3036

3137
public static void enqueueWork(Context context, Intent intent) {

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import android.content.Intent;
3535
import android.os.Bundle;
3636

37+
import androidx.annotation.Nullable;
38+
3739
import static com.onesignal.NotificationBundleProcessor.processBundleFromReceiver;
3840

3941
/**
@@ -56,14 +58,18 @@ public FCMIntentService() {
5658
* Expect if a NotificationExtenderService is setup
5759
*/
5860
@Override
59-
protected void onHandleIntent(Intent intent) {
61+
protected void onHandleIntent(final Intent intent) {
6062
Bundle bundle = intent.getExtras();
6163
if (bundle == null)
6264
return;
6365

64-
processBundleFromReceiver(this, bundle);
65-
66-
// Release the wake lock provided by the WakefulBroadcastReceiver.
67-
FCMBroadcastReceiver.completeWakefulIntent(intent);
66+
NotificationBundleProcessor.ProcessBundleReceiverCallback bundleReceiverCallback = new NotificationBundleProcessor.ProcessBundleReceiverCallback() {
67+
@Override
68+
public void onBundleProcessed(@Nullable NotificationBundleProcessor.ProcessedBundleResult processedResult) {
69+
// Release the wake lock provided by the WakefulBroadcastReceiver.
70+
FCMBroadcastReceiver.completeWakefulIntent(intent);
71+
}
72+
};
73+
processBundleFromReceiver(this, bundle, bundleReceiverCallback);
6874
}
6975
}

0 commit comments

Comments
 (0)