|
27 | 27 |
|
28 | 28 | package com.onesignal;
|
29 | 29 |
|
| 30 | +import android.content.Context; |
| 31 | + |
30 | 32 | 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; |
31 | 43 |
|
32 | 44 | class OSReceiveReceiptController {
|
33 | 45 |
|
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 | + |
36 | 50 | private final OSRemoteParamController remoteParamController;
|
37 |
| - |
38 | 51 | private static OSReceiveReceiptController sInstance;
|
39 | 52 |
|
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(); |
44 | 55 | }
|
45 | 56 |
|
46 | 57 | synchronized public static OSReceiveReceiptController getInstance() {
|
47 | 58 | if (sInstance == null)
|
48 |
| - sInstance = new OSReceiveReceiptController(OneSignal.getRemoteParamController(), OneSignal.getDelayTaskController()); |
| 59 | + sInstance = new OSReceiveReceiptController(); |
49 | 60 | return sInstance;
|
50 | 61 | }
|
51 | 62 |
|
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) { |
57 | 64 | if (!remoteParamController.isReceiveReceiptEnabled()) {
|
58 |
| - OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "sendReceiveReceipt disable"); |
| 65 | + OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "sendReceiveReceipt disabled"); |
59 | 66 | return;
|
60 | 67 | }
|
61 | 68 |
|
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(); |
66 | 107 | }
|
67 | 108 |
|
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(); |
85 | 120 | }
|
86 |
| - }; |
87 | 121 |
|
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 | + } |
89 | 137 | }
|
90 | 138 | }
|
0 commit comments