Skip to content

Commit 6a53b9c

Browse files
committed
Use a Worker with a delay to send Receive Receipts
- Add a `ReceiveReceiptWorker` to call `sendReceiveReceipt(notificationId)`
1 parent 23657fd commit 6a53b9c

File tree

3 files changed

+91
-38
lines changed

3 files changed

+91
-38
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ static void processNotification(OSNotificationGenerationJob notificationJob, boo
185185

186186
// Logic for when the notification is displayed
187187
String notificationId = notificationJob.getApiNotificationId();
188-
OSReceiveReceiptController.getInstance().sendReceiveReceipt(notificationId);
188+
Context context = notificationJob.getContext();
189+
OSReceiveReceiptController.getInstance().beginEnqueueingWork(context, notificationId);
189190
OneSignal.getSessionManager().onNotificationReceived(notificationId);
190191
}
191192

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

Lines changed: 85 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,64 +27,112 @@
2727

2828
package com.onesignal;
2929

30+
import android.content.Context;
31+
3032
import androidx.annotation.NonNull;
33+
import androidx.work.Constraints;
34+
import androidx.work.Data;
35+
import androidx.work.ExistingWorkPolicy;
36+
import androidx.work.NetworkType;
37+
import androidx.work.OneTimeWorkRequest;
38+
import androidx.work.WorkManager;
39+
import androidx.work.Worker;
40+
import androidx.work.WorkerParameters;
41+
42+
import java.util.concurrent.TimeUnit;
3143

3244
class OSReceiveReceiptController {
3345

34-
private final OSDelayTaskController taskController;
35-
private final OSReceiveReceiptRepository repository;
46+
private static final String OS_NOTIFICATION_ID = "os_notification_id";
47+
private int minDelay = 0;
48+
private int maxDelay = 25;
49+
3650
private final OSRemoteParamController remoteParamController;
37-
3851
private static OSReceiveReceiptController sInstance;
3952

40-
private OSReceiveReceiptController(OSRemoteParamController remoteParamController, OSDelayTaskController taskController) {
41-
this.remoteParamController = remoteParamController;
42-
this.taskController = taskController;
43-
this.repository = new OSReceiveReceiptRepository();
53+
private OSReceiveReceiptController() {
54+
this.remoteParamController = OneSignal.getRemoteParamController();
4455
}
4556

4657
synchronized public static OSReceiveReceiptController getInstance() {
4758
if (sInstance == null)
48-
sInstance = new OSReceiveReceiptController(OneSignal.getRemoteParamController(), OneSignal.getDelayTaskController());
59+
sInstance = new OSReceiveReceiptController();
4960
return sInstance;
5061
}
5162

52-
void sendReceiveReceipt(@NonNull final String notificationId) {
53-
final String appId = OneSignal.appId == null || OneSignal.appId.isEmpty() ? OneSignal.getSavedAppId() : OneSignal.appId;
54-
final String playerId = OneSignal.getUserId();
55-
Integer deviceType = null;
56-
63+
void beginEnqueueingWork(Context context, String osNotificationId) {
5764
if (!remoteParamController.isReceiveReceiptEnabled()) {
58-
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "sendReceiveReceipt disable");
65+
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "sendReceiveReceipt disabled");
5966
return;
6067
}
6168

62-
try {
63-
deviceType = new OSUtils().getDeviceType();
64-
} catch (NullPointerException e) {
65-
e.printStackTrace();
69+
int delay = OSUtils.getRandomDelay(minDelay, maxDelay);
70+
71+
Data inputData = new Data.Builder()
72+
.putString(OS_NOTIFICATION_ID, osNotificationId)
73+
.build();
74+
75+
Constraints constraints = new Constraints.Builder()
76+
.setRequiredNetworkType(NetworkType.CONNECTED)
77+
.build();
78+
79+
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(ReceiveReceiptWorker.class)
80+
.setConstraints(constraints)
81+
.setInitialDelay(delay, TimeUnit.SECONDS)
82+
.setInputData(inputData)
83+
.build();
84+
85+
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "OSReceiveReceiptController enqueueing send receive receipt work with notificationId: " + osNotificationId + " and delay: " + delay + " seconds");
86+
87+
WorkManager.getInstance(context)
88+
.enqueueUniqueWork(osNotificationId + "_receive_receipt", ExistingWorkPolicy.KEEP, workRequest);
89+
90+
}
91+
92+
public static class ReceiveReceiptWorker extends Worker {
93+
94+
public ReceiveReceiptWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
95+
super(context, workerParams);
96+
}
97+
98+
@NonNull
99+
@Override
100+
public Result doWork() {
101+
Data inputData = getInputData();
102+
String notificationId = inputData.getString(OS_NOTIFICATION_ID);
103+
104+
sendReceiveReceipt(notificationId);
105+
106+
return Result.success();
66107
}
67108

68-
final Integer finalDeviceType = deviceType;
69-
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "OSReceiveReceiptController: Device Type is: " + finalDeviceType);
70-
71-
Runnable receiveReceiptRunnable = new Runnable() {
72-
@Override
73-
public void run() {
74-
repository.sendReceiveReceipt(appId, playerId, finalDeviceType, notificationId, new OneSignalRestClient.ResponseHandler() {
75-
@Override
76-
void onSuccess(String response) {
77-
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "Receive receipt sent for notificationID: " + notificationId);
78-
}
79-
80-
@Override
81-
void onFailure(int statusCode, String response, Throwable throwable) {
82-
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Receive receipt failed with statusCode: " + statusCode + " response: " + response);
83-
}
84-
});
109+
void sendReceiveReceipt(@NonNull final String notificationId) {
110+
final String appId = OneSignal.appId == null || OneSignal.appId.isEmpty() ? OneSignal.getSavedAppId() : OneSignal.appId;
111+
final String playerId = OneSignal.getUserId();
112+
Integer deviceType = null;
113+
114+
OSReceiveReceiptRepository repository = new OSReceiveReceiptRepository();
115+
116+
try {
117+
deviceType = new OSUtils().getDeviceType();
118+
} catch (NullPointerException e) {
119+
e.printStackTrace();
85120
}
86-
};
87121

88-
taskController.delayTaskByRandom(receiveReceiptRunnable);
122+
final Integer finalDeviceType = deviceType;
123+
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "ReceiveReceiptWorker: Device Type is: " + finalDeviceType);
124+
125+
repository.sendReceiveReceipt(appId, playerId, finalDeviceType, notificationId, new OneSignalRestClient.ResponseHandler() {
126+
@Override
127+
void onSuccess(String response) {
128+
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "Receive receipt sent for notificationID: " + notificationId);
129+
}
130+
131+
@Override
132+
void onFailure(int statusCode, String response, Throwable throwable) {
133+
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Receive receipt failed with statusCode: " + statusCode + " response: " + response);
134+
}
135+
});
136+
}
89137
}
90138
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import java.util.Collections;
6363
import java.util.HashSet;
6464
import java.util.Iterator;
65+
import java.util.Random;
6566
import java.util.Set;
6667
import java.util.UUID;
6768
import java.util.concurrent.ConcurrentHashMap;
@@ -649,4 +650,7 @@ static boolean shouldLogMissingAppIdError(@Nullable String appId) {
649650
return true;
650651
}
651652

653+
static int getRandomDelay(int minDelay, int maxDelay) {
654+
return new Random().nextInt(maxDelay + 1 - minDelay) + minDelay;
655+
}
652656
}

0 commit comments

Comments
 (0)