Should I use Firebase Job Dispatcher











up vote
0
down vote

favorite












I have an android app that gets FCM Data messages (contains Image URL).
When Data Message is received in onMessageReceived() I create a FirebaseJobDispatcher and a Job, then schedule the Job for downloading and saving image in local storage dispatcher.mustSchedule(myJob).



The download and save image Job extends JobService. When a Job is started I create a Thread to in which I download and save the Image, then I send a BroadcastReceiver to the Activity so that it displays the new image from Local Storage in ImageView using Picasso (code below):



private String imageName = "image.jpg";

@Override
public boolean onStartJob(final JobParameters jobParameters) {
Log.d(TAG, "Update local storage content with new Image!");
Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
// Delegating work to a new Thread since it can be long task (downloading Image and storing it to local file system)
Thread storeImageThread = new Thread(new Runnable() {
@Override
public void run() {
// File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + imageName);

File file = new File(getApplicationContext().getFilesDir(), imageName);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
e.getLocalizedMessage();
}finally {
Intent intent = new Intent("com.example.SendBroadcast");
//intent.putExtra("imageURL", remoteMessage.getData().get("image"));
intent.setAction("com.example.SendBroadcast");
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// sendBroadcast(intent);
sendOrderedBroadcast(intent,null);
//Tell the framework that the job has completed and does not need to be reschedule
jobFinished(jobParameters, true);
}
}
});
storeImageThread.start();
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};

Picasso.with(getApplicationContext())
.load(jobParameters.getExtras().getString("image"))
.into(target);


return true;
}


I am facing some problems with this architecture;
- The Job Service starts many times very late although I use setTrigger(Trigger.executionWindow(0, 0)). So should I use a Firebase Job Dispatcher with Job Service for downloading and saving image in local storage? Is there any other alternative?




  • Is there more performance and efficient way to update the activity after finishing download and saving image?


  • I think Picasso uses separate Thread to download Image, so is my Thread use here correct?


  • Many times I am getting ARN dialog and app crashes. So is there a more performance architecture proposal for my purpose?



Thanks in advance for any help from experts










share|improve this question
























  • Is there any support from experts?
    – Tigris Laesus
    Nov 19 at 15:10















up vote
0
down vote

favorite












I have an android app that gets FCM Data messages (contains Image URL).
When Data Message is received in onMessageReceived() I create a FirebaseJobDispatcher and a Job, then schedule the Job for downloading and saving image in local storage dispatcher.mustSchedule(myJob).



The download and save image Job extends JobService. When a Job is started I create a Thread to in which I download and save the Image, then I send a BroadcastReceiver to the Activity so that it displays the new image from Local Storage in ImageView using Picasso (code below):



private String imageName = "image.jpg";

@Override
public boolean onStartJob(final JobParameters jobParameters) {
Log.d(TAG, "Update local storage content with new Image!");
Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
// Delegating work to a new Thread since it can be long task (downloading Image and storing it to local file system)
Thread storeImageThread = new Thread(new Runnable() {
@Override
public void run() {
// File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + imageName);

File file = new File(getApplicationContext().getFilesDir(), imageName);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
e.getLocalizedMessage();
}finally {
Intent intent = new Intent("com.example.SendBroadcast");
//intent.putExtra("imageURL", remoteMessage.getData().get("image"));
intent.setAction("com.example.SendBroadcast");
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// sendBroadcast(intent);
sendOrderedBroadcast(intent,null);
//Tell the framework that the job has completed and does not need to be reschedule
jobFinished(jobParameters, true);
}
}
});
storeImageThread.start();
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};

Picasso.with(getApplicationContext())
.load(jobParameters.getExtras().getString("image"))
.into(target);


return true;
}


I am facing some problems with this architecture;
- The Job Service starts many times very late although I use setTrigger(Trigger.executionWindow(0, 0)). So should I use a Firebase Job Dispatcher with Job Service for downloading and saving image in local storage? Is there any other alternative?




  • Is there more performance and efficient way to update the activity after finishing download and saving image?


  • I think Picasso uses separate Thread to download Image, so is my Thread use here correct?


  • Many times I am getting ARN dialog and app crashes. So is there a more performance architecture proposal for my purpose?



Thanks in advance for any help from experts










share|improve this question
























  • Is there any support from experts?
    – Tigris Laesus
    Nov 19 at 15:10













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an android app that gets FCM Data messages (contains Image URL).
When Data Message is received in onMessageReceived() I create a FirebaseJobDispatcher and a Job, then schedule the Job for downloading and saving image in local storage dispatcher.mustSchedule(myJob).



The download and save image Job extends JobService. When a Job is started I create a Thread to in which I download and save the Image, then I send a BroadcastReceiver to the Activity so that it displays the new image from Local Storage in ImageView using Picasso (code below):



private String imageName = "image.jpg";

@Override
public boolean onStartJob(final JobParameters jobParameters) {
Log.d(TAG, "Update local storage content with new Image!");
Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
// Delegating work to a new Thread since it can be long task (downloading Image and storing it to local file system)
Thread storeImageThread = new Thread(new Runnable() {
@Override
public void run() {
// File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + imageName);

File file = new File(getApplicationContext().getFilesDir(), imageName);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
e.getLocalizedMessage();
}finally {
Intent intent = new Intent("com.example.SendBroadcast");
//intent.putExtra("imageURL", remoteMessage.getData().get("image"));
intent.setAction("com.example.SendBroadcast");
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// sendBroadcast(intent);
sendOrderedBroadcast(intent,null);
//Tell the framework that the job has completed and does not need to be reschedule
jobFinished(jobParameters, true);
}
}
});
storeImageThread.start();
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};

Picasso.with(getApplicationContext())
.load(jobParameters.getExtras().getString("image"))
.into(target);


return true;
}


I am facing some problems with this architecture;
- The Job Service starts many times very late although I use setTrigger(Trigger.executionWindow(0, 0)). So should I use a Firebase Job Dispatcher with Job Service for downloading and saving image in local storage? Is there any other alternative?




  • Is there more performance and efficient way to update the activity after finishing download and saving image?


  • I think Picasso uses separate Thread to download Image, so is my Thread use here correct?


  • Many times I am getting ARN dialog and app crashes. So is there a more performance architecture proposal for my purpose?



Thanks in advance for any help from experts










share|improve this question















I have an android app that gets FCM Data messages (contains Image URL).
When Data Message is received in onMessageReceived() I create a FirebaseJobDispatcher and a Job, then schedule the Job for downloading and saving image in local storage dispatcher.mustSchedule(myJob).



The download and save image Job extends JobService. When a Job is started I create a Thread to in which I download and save the Image, then I send a BroadcastReceiver to the Activity so that it displays the new image from Local Storage in ImageView using Picasso (code below):



private String imageName = "image.jpg";

@Override
public boolean onStartJob(final JobParameters jobParameters) {
Log.d(TAG, "Update local storage content with new Image!");
Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
// Delegating work to a new Thread since it can be long task (downloading Image and storing it to local file system)
Thread storeImageThread = new Thread(new Runnable() {
@Override
public void run() {
// File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + imageName);

File file = new File(getApplicationContext().getFilesDir(), imageName);
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream);
ostream.flush();
ostream.close();
} catch (IOException e) {
e.getLocalizedMessage();
}finally {
Intent intent = new Intent("com.example.SendBroadcast");
//intent.putExtra("imageURL", remoteMessage.getData().get("image"));
intent.setAction("com.example.SendBroadcast");
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// sendBroadcast(intent);
sendOrderedBroadcast(intent,null);
//Tell the framework that the job has completed and does not need to be reschedule
jobFinished(jobParameters, true);
}
}
});
storeImageThread.start();
}

@Override
public void onBitmapFailed(Drawable errorDrawable) {

}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {

}
};

Picasso.with(getApplicationContext())
.load(jobParameters.getExtras().getString("image"))
.into(target);


return true;
}


I am facing some problems with this architecture;
- The Job Service starts many times very late although I use setTrigger(Trigger.executionWindow(0, 0)). So should I use a Firebase Job Dispatcher with Job Service for downloading and saving image in local storage? Is there any other alternative?




  • Is there more performance and efficient way to update the activity after finishing download and saving image?


  • I think Picasso uses separate Thread to download Image, so is my Thread use here correct?


  • Many times I am getting ARN dialog and app crashes. So is there a more performance architecture proposal for my purpose?



Thanks in advance for any help from experts







android firebase-cloud-messaging android-broadcastreceiver android-jobscheduler firebase-job-dispatcher






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 12:12









Ali

1,4192929




1,4192929










asked Nov 19 at 11:51









Tigris Laesus

4110




4110












  • Is there any support from experts?
    – Tigris Laesus
    Nov 19 at 15:10


















  • Is there any support from experts?
    – Tigris Laesus
    Nov 19 at 15:10
















Is there any support from experts?
– Tigris Laesus
Nov 19 at 15:10




Is there any support from experts?
– Tigris Laesus
Nov 19 at 15:10












1 Answer
1






active

oldest

votes

















up vote
0
down vote














So should I use a Firebase Job Dispatcher with Job Service for
downloading and saving image in local storage?




Because your application is running state when onMessageReceived is executed for FCM message. no need to use Firebase Job Dispatcher




Is there any other alternative?




Yes, use WorkManager for downloading image if app is not visible to user. save image in cache or sdcard and show it when app opened by user.




The Job Service starts many times very late although I
use setTrigger(Trigger.executionWindow(0, 0))




WorkManager providing options for checking task is finished or not before starting same task again.



OR



If app is already running then use normal service like IntentService or Service.






share|improve this answer





















  • Thanks. I googled WorkManager, it seems that it uses JobScheduler service to schedule jobs else FirebaseJobDispatcher if JobScheduler is not supported. Is there a big defference by using WorkManager? Is it riggered immediately when Message received? Is it possible to use Background Threads instead? onMessageReceived() Picasso starts a Thread for downloading Image from a URL and onBitmapLoaded() I start a new Thread for saving the downloaded Image on file system. So when to stop the Thread? how to know Activity status (on or off) before sending Broadcast?
    – Tigris Laesus
    Nov 19 at 14:29













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374068%2fshould-i-use-firebase-job-dispatcher%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote














So should I use a Firebase Job Dispatcher with Job Service for
downloading and saving image in local storage?




Because your application is running state when onMessageReceived is executed for FCM message. no need to use Firebase Job Dispatcher




Is there any other alternative?




Yes, use WorkManager for downloading image if app is not visible to user. save image in cache or sdcard and show it when app opened by user.




The Job Service starts many times very late although I
use setTrigger(Trigger.executionWindow(0, 0))




WorkManager providing options for checking task is finished or not before starting same task again.



OR



If app is already running then use normal service like IntentService or Service.






share|improve this answer





















  • Thanks. I googled WorkManager, it seems that it uses JobScheduler service to schedule jobs else FirebaseJobDispatcher if JobScheduler is not supported. Is there a big defference by using WorkManager? Is it riggered immediately when Message received? Is it possible to use Background Threads instead? onMessageReceived() Picasso starts a Thread for downloading Image from a URL and onBitmapLoaded() I start a new Thread for saving the downloaded Image on file system. So when to stop the Thread? how to know Activity status (on or off) before sending Broadcast?
    – Tigris Laesus
    Nov 19 at 14:29

















up vote
0
down vote














So should I use a Firebase Job Dispatcher with Job Service for
downloading and saving image in local storage?




Because your application is running state when onMessageReceived is executed for FCM message. no need to use Firebase Job Dispatcher




Is there any other alternative?




Yes, use WorkManager for downloading image if app is not visible to user. save image in cache or sdcard and show it when app opened by user.




The Job Service starts many times very late although I
use setTrigger(Trigger.executionWindow(0, 0))




WorkManager providing options for checking task is finished or not before starting same task again.



OR



If app is already running then use normal service like IntentService or Service.






share|improve this answer





















  • Thanks. I googled WorkManager, it seems that it uses JobScheduler service to schedule jobs else FirebaseJobDispatcher if JobScheduler is not supported. Is there a big defference by using WorkManager? Is it riggered immediately when Message received? Is it possible to use Background Threads instead? onMessageReceived() Picasso starts a Thread for downloading Image from a URL and onBitmapLoaded() I start a new Thread for saving the downloaded Image on file system. So when to stop the Thread? how to know Activity status (on or off) before sending Broadcast?
    – Tigris Laesus
    Nov 19 at 14:29















up vote
0
down vote










up vote
0
down vote










So should I use a Firebase Job Dispatcher with Job Service for
downloading and saving image in local storage?




Because your application is running state when onMessageReceived is executed for FCM message. no need to use Firebase Job Dispatcher




Is there any other alternative?




Yes, use WorkManager for downloading image if app is not visible to user. save image in cache or sdcard and show it when app opened by user.




The Job Service starts many times very late although I
use setTrigger(Trigger.executionWindow(0, 0))




WorkManager providing options for checking task is finished or not before starting same task again.



OR



If app is already running then use normal service like IntentService or Service.






share|improve this answer













So should I use a Firebase Job Dispatcher with Job Service for
downloading and saving image in local storage?




Because your application is running state when onMessageReceived is executed for FCM message. no need to use Firebase Job Dispatcher




Is there any other alternative?




Yes, use WorkManager for downloading image if app is not visible to user. save image in cache or sdcard and show it when app opened by user.




The Job Service starts many times very late although I
use setTrigger(Trigger.executionWindow(0, 0))




WorkManager providing options for checking task is finished or not before starting same task again.



OR



If app is already running then use normal service like IntentService or Service.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 12:23









ρяσѕρєя K

113k26158187




113k26158187












  • Thanks. I googled WorkManager, it seems that it uses JobScheduler service to schedule jobs else FirebaseJobDispatcher if JobScheduler is not supported. Is there a big defference by using WorkManager? Is it riggered immediately when Message received? Is it possible to use Background Threads instead? onMessageReceived() Picasso starts a Thread for downloading Image from a URL and onBitmapLoaded() I start a new Thread for saving the downloaded Image on file system. So when to stop the Thread? how to know Activity status (on or off) before sending Broadcast?
    – Tigris Laesus
    Nov 19 at 14:29




















  • Thanks. I googled WorkManager, it seems that it uses JobScheduler service to schedule jobs else FirebaseJobDispatcher if JobScheduler is not supported. Is there a big defference by using WorkManager? Is it riggered immediately when Message received? Is it possible to use Background Threads instead? onMessageReceived() Picasso starts a Thread for downloading Image from a URL and onBitmapLoaded() I start a new Thread for saving the downloaded Image on file system. So when to stop the Thread? how to know Activity status (on or off) before sending Broadcast?
    – Tigris Laesus
    Nov 19 at 14:29


















Thanks. I googled WorkManager, it seems that it uses JobScheduler service to schedule jobs else FirebaseJobDispatcher if JobScheduler is not supported. Is there a big defference by using WorkManager? Is it riggered immediately when Message received? Is it possible to use Background Threads instead? onMessageReceived() Picasso starts a Thread for downloading Image from a URL and onBitmapLoaded() I start a new Thread for saving the downloaded Image on file system. So when to stop the Thread? how to know Activity status (on or off) before sending Broadcast?
– Tigris Laesus
Nov 19 at 14:29






Thanks. I googled WorkManager, it seems that it uses JobScheduler service to schedule jobs else FirebaseJobDispatcher if JobScheduler is not supported. Is there a big defference by using WorkManager? Is it riggered immediately when Message received? Is it possible to use Background Threads instead? onMessageReceived() Picasso starts a Thread for downloading Image from a URL and onBitmapLoaded() I start a new Thread for saving the downloaded Image on file system. So when to stop the Thread? how to know Activity status (on or off) before sending Broadcast?
– Tigris Laesus
Nov 19 at 14:29




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374068%2fshould-i-use-firebase-job-dispatcher%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen