Skip to content

Commit

Permalink
Use Firebase Job Dispatcher for long running tasks.
Browse files Browse the repository at this point in the history
- Provides compatiability with Android-O
- Show good practice on pre Android-O

Change-Id: I75edd3e911b6a599f9bc12435fe17c1e3a83b35a
  • Loading branch information
kroikie committed Mar 20, 2017
1 parent 35f2c1e commit b756fd3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
7 changes: 4 additions & 3 deletions messaging/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
buildToolsVersion '25.0.2'

defaultConfig {
applicationId "com.google.firebase.quickstart.fcm"
Expand All @@ -28,14 28,15 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:appcompat-v7:25.3.0'

compile 'com.google.firebase:firebase-messaging:10.2.0'
compile 'com.firebase:firebase-jobdispatcher:0.5.2'

// Testing dependencies
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support:support-annotations:25.0.1'
androidTestCompile 'com.android.support:support-annotations:25.3.0'
}

apply plugin: 'com.google.gms.google-services'
6 changes: 6 additions & 0 deletions messaging/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 44,12 @@
</intent-filter>
</service>
<!-- [END firebase_iid_service] -->
<service android:name=".MyJobService"
android:exported="false">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 25,10 @@
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.firebase.jobdispatcher.Constraint;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

Expand Down Expand Up @@ -57,6 61,15 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " remoteMessage.getData());

if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}

}

// Check if message contains a notification payload.
Expand All @@ -69,6 82,27 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
}
// [END receive_message]

/**
* Schedule a job using FirebaseJobDispatcher.
*/
private void scheduleJob() {
// [START dispatch_job]
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("my-job-tag")
.build();
dispatcher.schedule(myJob);
// [END dispatch_job]
}

/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}

/**
* Create and show a simple notification containing the received FCM message.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 1,24 @@
package com.google.firebase.quickstart.fcm;

import android.util.Log;

import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;

public class MyJobService extends JobService {

private static final String TAG = "MyJobService";

@Override
public boolean onStartJob(JobParameters jobParameters) {
Log.d(TAG, "Performing long running task in scheduled job");
// TODO(developer): add long running task here.
return false;
}

@Override
public boolean onStopJob(JobParameters jobParameters) {
return false;
}

}

0 comments on commit b756fd3

Please sign in to comment.