收到通知时如何开始后台工作

时间:2018-01-09 18:13:47

标签: android firebase-cloud-messaging background-task firebase-job-dispatcher

你好everyBody这是我在这里的第一个问题,所以我希望得到一个答案。 我有一个Android应用程序,在FCM的帮助下,我能够在前台,后台和应用程序被杀时接收通知。

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());

}

我想在通知到达时启动后台作业,我使用了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);

但是当应用程序处于后台或被杀时,它无法正常工作,但我会收到通知。

我试图将后台任务移动到FCM onMessageReceived,并且也遇到了同样的问题。

有没有办法在收到通知后触发后台任务?

对于一个好的答案不寻常的赞赏:))

1 个答案:

答案 0 :(得分:0)

我不确定这会有效,但你可以试试。创建一个类并扩展Service并在内部创建create AsyncTask,例如:

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);
       }
    }
 }

不要忘记在清单中注册您的Service课程。

然后在方法onMessageReceived内进行一些更改。试试这个:

Intent notificationIntent = new Intent(mContext, TestServices.class);
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0,    notificationIntent, 0);

希望可能会帮助你。