I am stuck and can't get my OneTimeWorkRequest to work properly.
I have got this Retrofit API call I want to resend to the backend incase there's no network connectivity or similar on the phone. What I can't wrap my head around is how to pass the call to the worker.
I've got this following code:
Here's the API Service:
public void sendMessage(Context context, Message message){
ListService listService = ServiceBuilder.getInstance(context).buildService(ListService.class);
Call<Void> createRequest = listService.sendMessage(message);
createRequest.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if(response.isSuccessful()) {
Toast toast = new Toast(context);
toast.setText("Thank you for your message!");
toast.show();
}
else{
Toast toast = new Toast(context);
toast.setText("Could not send message!");
toast.show();
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Toast toast = new Toast(context);
toast.setText("Could not send message!");
toast.show();
}
});
}
The worker in the main class:
Constraints constraints4 = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
OneTimeWorkRequest sendFeedbackWorker = new OneTimeWorkRequest.Builder(SendMessageWorker.class)
.setInitialDelay(15, TimeUnit.MINUTES)
.setConstraints(constraints4)
.build();
WorkManager.getInstance().enqueue(sendMessageWorker);
The worker class:
public class SendMessageWorker extends Worker {
private static final String TAG = "SendMessageWorker";
public SendMessageWorker(
@NonNull
Context context,
@NonNull
WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@NotNull
@Override
public Result doWork() {
//What do I do here?
return Result.success();
}
}
What am I missing, is it just the Worker class? And is it the application context I need in there? If so, how do I pass it?
I put all info in as putStrings, converted the date.
Data myFeedbackMessage = new Data.Builder()
.putString("myFeedbackMessageId", message.getId())
.putString("myFeedbackMessage", message.getFeedbackMessage())
.putString("myFeedbackPerson", message.getFeedbackPersonName())
.putString("myFeedbackCreated",message.getCreated().toString())
.build();