|
| 1 | +/* |
| 2 | + * Copyright (c) 2015-present, Parse, LLC. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | +package com.parse.fcm; |
| 10 | + |
| 11 | +import android.support.annotation.CallSuper; |
| 12 | + |
| 13 | +import com.firebase.jobdispatcher.Constraint; |
| 14 | +import com.firebase.jobdispatcher.FirebaseJobDispatcher; |
| 15 | +import com.firebase.jobdispatcher.GooglePlayDriver; |
| 16 | +import com.firebase.jobdispatcher.Job; |
| 17 | +import com.firebase.jobdispatcher.RetryStrategy; |
| 18 | +import com.google.firebase.iid.FirebaseInstanceIdService; |
| 19 | +import com.parse.ParseInstallation; |
| 20 | + |
| 21 | +/** |
| 22 | + * Assures the {@link ParseInstallation#getDeviceToken()} stays up to date. If you need to do custom things with the token, make sure you extend this |
| 23 | + * class and call super. |
| 24 | + */ |
| 25 | +public class ParseFirebaseInstanceIDService extends FirebaseInstanceIdService { |
| 26 | + |
| 27 | + @CallSuper |
| 28 | + @Override |
| 29 | + public void onTokenRefresh() { |
| 30 | + super.onTokenRefresh(); |
| 31 | + FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(getApplicationContext())); |
| 32 | + Job job = dispatcher.newJobBuilder() |
| 33 | + .setRecurring(false) |
| 34 | + .setReplaceCurrent(true) |
| 35 | + // retry with exponential backoff |
| 36 | + .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL) |
| 37 | + .setConstraints( |
| 38 | + // only run on a network |
| 39 | + Constraint.ON_ANY_NETWORK |
| 40 | + ) |
| 41 | + .setService(ParseFirebaseJobService.class) // the JobService that will be called |
| 42 | + .setTag("upload-token") // uniquely identifies the job |
| 43 | + .build(); |
| 44 | + |
| 45 | + dispatcher.mustSchedule(job); |
| 46 | + } |
| 47 | +} |
0 commit comments