Hello, guys today we learn about what is FirebaseDispatcher and how to use our android project but before know about FirebaseRequestDispatcher, we know about JobScheduler.
What is JobScheduler?
The job scheduler is an android system service available on API level 21(Lollypop). It provides an API for scheduling unit of work that will executed in your app process.
FirebaserequestDispatcher
The firebase jobDispatcher is a library for scheduling jobs in your android app. It provides a JobScheduler compatible API that works on all recent version of Android( API 14) that have google play service installed.
Dependency
Add following line in your project build.gradle's dependencies section
How to use?
Simple extends with JobService class the JobService by default used Service class
Now create a simple dispatcher class and initialize google play driver
See source code
MainActivity.java
MyJobService.java
Thank you
enjoy with this source code
Full Source Code FirebaseJobDispatcher
What is JobScheduler?
The job scheduler is an android system service available on API level 21(Lollypop). It provides an API for scheduling unit of work that will executed in your app process.
FirebaserequestDispatcher
The firebase jobDispatcher is a library for scheduling jobs in your android app. It provides a JobScheduler compatible API that works on all recent version of Android( API 14) that have google play service installed.
Dependency
Add following line in your project build.gradle's dependencies section
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
Simple extends with JobService class the JobService by default used Service class
import com.firebase.jobdispatcher.JobParameters; import com.firebase.jobdispatcher.JobService; public class MyJobService extends JobService { @Override public boolean onStartJob(JobParameters job) { // Do some work here return false; // Answers the question: "Is there still work going on?" } @Override public boolean onStopJob(JobParameters job) { return false; } }
Now create a simple dispatcher class and initialize google play driver
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
See source code
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String JOB_TAG = "MyJobService"; private FirebaseJobDispatcher mDispatcher; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnstart).setOnClickListener(this); findViewById(R.id.btnstop).setOnClickListener(this); mDispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this)); } @Override public void onClick(View view) { int id = view.getId(); if (R.id.btnstart == id) { scheduleJob(); } else { cancelJob(JOB_TAG); } } private void scheduleJob() { Job myJob = mDispatcher.newJobBuilder() .setService(MyJobService.class) // your job class name .setTag(JOB_TAG) // define your tag name .setRecurring(true) .setTrigger(Trigger.executionWindow(5, 60)) // trigger job every 1 min .setLifetime(Lifetime.UNTIL_NEXT_BOOT) .setReplaceCurrent(false) // if you want when open your app then job auto replace with new job then this flag make true .setConstraints(Constraint.ON_ANY_NETWORK) .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR) // this is trigger linear time inteval like.. 1min,2min,3min,4min.... .build(); mDispatcher.mustSchedule(myJob); Toast.makeText(this, "job_scheduled", Toast.LENGTH_LONG).show(); } // cancel job private void cancelJob(String jobTag) { if ("".equals(jobTag)) { mDispatcher.cancelAll(); } else { mDispatcher.cancel(jobTag); } Toast.makeText(this, "job_cancelled", Toast.LENGTH_LONG).show(); } }
MyJobService.java
public class MyJobService extends JobService { private int ii = 0; private static final String TAG = "MyJobService"; @Override public boolean onStartJob(JobParameters jobParameters) { String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date()); Log.e(TAG, "Job " + currentDateTimeString); sendNotification("DispatcherRequest","Current noti "+currentDateTimeString); return false; } @Override public boolean onStopJob(JobParameters jobParameters) { Log.e(TAG, "Job cancelled!"); return false; } private void sendNotification(String contentTitle, String message) { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //notificationManager.cancelAll(); Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 1 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); String channelId = "default_notification_channel_id"; Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(contentTitle) .setContentText(message) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); // Since android Oreo notification channel is needed. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(channelId, "Channel human readable title", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel); } notificationManager.notify(1 /* ID of notification */, notificationBuilder.build()); } }
Thank you
enjoy with this source code
Full Source Code FirebaseJobDispatcher
No comments:
Post a Comment