googlearchive / firebase-jobdispatcher-android

DEPRECATED please see the README.md below for details.
Apache License 2.0
1.79k stars 208 forks source link

dispatcher.cancel() is not working #203

Closed 3llomi closed 4 years ago

3llomi commented 6 years ago

i am trying to cancel a Job when the user clicks on a Button my scenario is like this: a message received from FCM contains a URL . inside onMessageReceived the job will run and start downloading that file

private void scheduleJob(final Message message) {
        FirebaseJobDispatcher dispatcher = FBJDSingleton.getInstance(this);
        Bundle myExtrasBundle = new Bundle();
        myExtrasBundle.putString("messageId", message.getMessageId());
        myExtrasBundle.putString("link", message.getContent());
        myExtrasBundle.putInt("type", message.getType());
        myExtrasBundle.putString("receiverId", message.getFromId());

        Job myJob = dispatcher.newJobBuilder()
                .setService(MyJobService.class)
                .setTag(message.getMessageId())
                .setRecurring(false)
                .setLifetime(Lifetime.UNTIL_NEXT_BOOT)
                .setTrigger(Trigger.NOW)
                .setReplaceCurrent(false)
                .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
                .setConstraints(Constraint.ON_ANY_NETWORK)
                .setExtras(myExtrasBundle)
                .build();

        dispatcher.mustSchedule(myJob);
    }
@Override
    public boolean onStartJob(JobParameters job) {
        Log.d("3llomi", "ON Start JOB");
        Toast.makeText(this, "ONSTART JOB", Toast.LENGTH_SHORT).show();
        String messageId = job.getExtras().getString("messageId");
        String link = job.getExtras().getString("link");
        int type = job.getExtras().getInt("type");
        String receiverId = job.getExtras().getString("receiverId");
        downloadFile(link, type, messageId, receiverId, -1);
        return false;
    }

when the user clicks the cancel Button it should stop the Download Job

            //onClick in Activity
            FBJDSingleton.getInstance(this).cancel(messageId);

and here is the Singleton Class

public class FBJDSingleton {
    private static FirebaseJobDispatcher firebaseJobDispatcher;

    private FBJDSingleton() {}

     public static FirebaseJobDispatcher getInstance(Context context) {
        if (firebaseJobDispatcher == null) {
            firebaseJobDispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
        }
        return firebaseJobDispatcher;
    }
}
adsonafonso commented 6 years ago

are you returning "false" onStopJob() @3llomi?

3llomi commented 6 years ago

@adsonafonso yes i am returning false because i don't want to re-run the Job again

adsonafonso commented 6 years ago

@3llomi https://github.com/firebase/firebase-jobdispatcher-android/issues/110 this seems related. Need to call jobFinished(job, false). Let me know if this works for you

samtstern commented 4 years ago

In April 2019 we announced that Firebase Job Dispatcher would be deprecated today, April 7th 2020. For this reason we are going to close all open issues and archive the repository. We will also stop supporting FJD for new app installations (such as those targeting sdk versions greater than Android Q). Existing apps that rely on FJD should continue to function as usual.

While we will no longer work on FJD, we encourage you to migrate to Work Manager which is a superior tool and fully supported by the Android team.

Thank you for taking the time to try the library and provide this feedback. We sincerely appreciate your effort and we apologize for not addressing it in time.