3
3
import android .content .Context ;
4
4
5
5
import androidx .annotation .NonNull ;
6
- import androidx .annotation .Nullable ;
7
- import androidx .concurrent .futures .CallbackToFutureAdapter ;
8
6
import androidx .work .Data ;
9
7
import androidx .work .ExistingWorkPolicy ;
10
- import androidx .work .ListenableWorker ;
11
8
import androidx .work .OneTimeWorkRequest ;
12
9
import androidx .work .WorkManager ;
10
+ import androidx .work .Worker ;
13
11
import androidx .work .WorkerParameters ;
14
12
15
- import com .google .common .util .concurrent .ListenableFuture ;
16
-
17
13
import org .json .JSONException ;
18
14
import org .json .JSONObject ;
19
15
23
19
24
20
class OSNotificationWorkManager {
25
21
26
- private static final String OS_NOTIFICATION_ID = "os_bnotification_id" ;
27
22
private static final String ANDROID_NOTIF_ID_WORKER_DATA_PARAM = "android_notif_id" ;
28
23
private static final String JSON_PAYLOAD_WORKER_DATA_PARAM = "json_payload" ;
29
24
private static final String TIMESTAMP_WORKER_DATA_PARAM = "timestamp" ;
@@ -54,20 +49,9 @@ static void removeNotificationIdProcessed(String osNotificationId) {
54
49
}
55
50
56
51
static void beginEnqueueingWork (Context context , String osNotificationId , int androidNotificationId , String jsonPayload , long timestamp ,
57
- boolean isRestoring , boolean isHighPriority , boolean needsWorkerThread ) {
58
- if (!needsWorkerThread ) {
59
- try {
60
- JSONObject jsonPayloadObject = new JSONObject (jsonPayload );
61
- processNotificationData (context , androidNotificationId , jsonPayloadObject , isRestoring , timestamp );
62
- } catch (JSONException e ) {
63
- OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , "Error occurred parsing jsonPayload to JSONObject in beginEnqueueingWork e: " + e .getMessage ());
64
- e .printStackTrace ();
65
- }
66
- return ;
67
- }
52
+ boolean isRestoring , boolean isHighPriority ) {
68
53
// TODO: Need to figure out how to implement the isHighPriority param
69
54
Data inputData = new Data .Builder ()
70
- .putString (OS_NOTIFICATION_ID , osNotificationId )
71
55
.putInt (ANDROID_NOTIF_ID_WORKER_DATA_PARAM , androidNotificationId )
72
56
.putString (JSON_PAYLOAD_WORKER_DATA_PARAM , jsonPayload )
73
57
.putLong (TIMESTAMP_WORKER_DATA_PARAM , timestamp )
@@ -83,29 +67,15 @@ static void beginEnqueueingWork(Context context, String osNotificationId, int an
83
67
.enqueueUniqueWork (osNotificationId , ExistingWorkPolicy .KEEP , workRequest );
84
68
}
85
69
86
- public static class NotificationWorker extends ListenableWorker {
70
+ public static class NotificationWorker extends Worker {
87
71
88
72
public NotificationWorker (@ NonNull Context context , @ NonNull WorkerParameters workerParams ) {
89
73
super (context , workerParams );
90
74
}
91
75
92
76
@ NonNull
93
77
@ Override
94
- public ListenableFuture <Result > startWork () {
95
- return CallbackToFutureAdapter .getFuture (new CallbackToFutureAdapter .Resolver <Result >() {
96
- @ Nullable
97
- @ Override
98
- public Object attachCompleter (@ NonNull CallbackToFutureAdapter .Completer <Result > completer ) throws Exception {
99
- String notificationId = NotificationWorker .this .doWork (completer );
100
-
101
- // This value is used only for debug purposes: it will be used
102
- // in toString() of returned future or error cases.
103
- return "NotificationWorkerFutureCallback_" + notificationId ;
104
- }
105
- });
106
- }
107
-
108
- private String doWork (@ NonNull CallbackToFutureAdapter .Completer <Result > completer ) {
78
+ public Result doWork () {
109
79
Data inputData = getInputData ();
110
80
try {
111
81
OneSignal .onesignalLog (OneSignal .LOG_LEVEL .DEBUG , "NotificationWorker running doWork with data: " + inputData );
@@ -116,7 +86,6 @@ private String doWork(@NonNull CallbackToFutureAdapter.Completer<Result> complet
116
86
boolean isRestoring = inputData .getBoolean (IS_RESTORING_WORKER_DATA_PARAM , false );
117
87
118
88
processNotificationData (
119
- completer ,
120
89
getApplicationContext (),
121
90
androidNotificationId ,
122
91
jsonPayload ,
@@ -125,36 +94,30 @@ private String doWork(@NonNull CallbackToFutureAdapter.Completer<Result> complet
125
94
} catch (JSONException e ) {
126
95
OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , "Error occurred doing work for job with id: " + getId ().toString ());
127
96
e .printStackTrace ();
128
- completer . setException ( e );
97
+ return Result . failure ( );
129
98
}
130
- return inputData . getString ( OS_NOTIFICATION_ID );
99
+ return Result . success ( );
131
100
}
132
- }
133
101
134
- static void processNotificationData (Context context , int androidNotificationId , JSONObject jsonPayload ,
135
- boolean isRestoring , Long timestamp ) {
136
- processNotificationData (null , context , androidNotificationId , jsonPayload , isRestoring , timestamp );
137
- }
102
+ private void processNotificationData (Context context , int androidNotificationId , JSONObject jsonPayload ,
103
+ boolean isRestoring , Long timestamp ) {
104
+ OSNotification notification = new OSNotification (null , jsonPayload , androidNotificationId );
105
+ OSNotificationController controller = new OSNotificationController (context , notification , jsonPayload , isRestoring , true , timestamp );
106
+ OSNotificationReceivedEvent notificationReceived = new OSNotificationReceivedEvent (controller , notification );
138
107
139
- static void processNotificationData ( CallbackToFutureAdapter . Completer < ListenableWorker . Result > completer ,
140
- Context context , int androidNotificationId , JSONObject jsonPayload ,
141
- boolean isRestoring , Long timestamp ) {
142
- OSNotification notification = new OSNotification ( null , jsonPayload , androidNotificationId );
143
- OSNotificationController controller = new OSNotificationController ( completer , context , notification , jsonPayload , isRestoring , true , timestamp );
144
- OSNotificationReceivedEvent notificationReceived = new OSNotificationReceivedEvent ( controller , notification );
108
+ if ( OneSignal . remoteNotificationReceivedHandler != null )
109
+ try {
110
+ OneSignal . remoteNotificationReceivedHandler . remoteNotificationReceived ( context , notificationReceived );
111
+ } catch ( Throwable t ) {
112
+ OneSignal . Log ( OneSignal . LOG_LEVEL . ERROR , "remoteNotificationReceived throw an exception. Displaying normal OneSignal notification." , t );
113
+ notificationReceived . complete ( notification );
145
114
146
- if (OneSignal .remoteNotificationReceivedHandler != null )
147
- try {
148
- OneSignal .remoteNotificationReceivedHandler .remoteNotificationReceived (context , notificationReceived );
149
- } catch (Throwable t ) {
150
- OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "remoteNotificationReceived throw an exception. Displaying normal OneSignal notification." , t );
115
+ throw t ;
116
+ }
117
+ else {
118
+ OneSignal .Log (OneSignal .LOG_LEVEL .WARN , "remoteNotificationReceivedHandler not setup, displaying normal OneSignal notification" );
151
119
notificationReceived .complete (notification );
152
-
153
- throw t ;
154
120
}
155
- else {
156
- OneSignal .Log (OneSignal .LOG_LEVEL .WARN , "remoteNotificationReceivedHandler not setup, displaying normal OneSignal notification" );
157
- notificationReceived .complete (notification );
158
121
}
159
122
}
160
123
}
0 commit comments