hello everyBody it's my first question here so i hope to get an answer . i have an android app and with the help of FCM i am able to receive notifications in the foreground , background and when the app is killed .
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
final String jobTag="copyDb";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String notification_title = remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();
String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(notification_title)
.setContentText(notification_message);
Intent resultIntent = new Intent(click_action);
resultIntent.putExtra("user_id", from_user_id);
PendingIntent resultPendingIntent =
getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
I want to start a background job when the notification arrive , i used the firebase jobDispatcher :
SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("fromUser", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("fromUser",from_user_id);
editor.commit();
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(getApplicationContext()));
Job myJob = dispatcher.newJobBuilder()
.setService(jobDispatcher.class) // the JobService that will be called
.setTag(jobTag)
.build();
dispatcher.mustSchedule(myJob);
But it's not working while the app is in the background or killed , yet i receive notification .
i tried to move the background task to the FCM onMessageReceived and also got the same problem .
is there any way to trigger a background task when a notification received ??
unusual appreciation for a good answer :))
I'm not sure will this work but you can try. Create a class and extend Service
and inside create create AsyncTask
for example something like this:
public class TestServices extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
MyAsyncTask asyncTask = new MyAsyncTask();
asyncTask.execute();
return START_NOT_STICKY;
}
private class MyAsyncTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... voids) {
// preform your task here, downloading saving etc.
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Intent intent = new Intent(TestServices.this, TestServices.class);
stopService(intent);
}
}
}
Don't forget to register your Service
class inside manifest.
Then inside method onMessageReceived
make some changes. Try this:
Intent notificationIntent = new Intent(mContext, TestServices.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, notificationIntent, 0);
Hope the might help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.