简体   繁体   English

收到通知时如何启动后台作业

[英]how to start background job when a notification received

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 . 我有一个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());

}

I want to start a background job when the notification arrive , i used the firebase jobDispatcher : 我想在通知到达时开始后台作业,我使用了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 . 我试图将后台任务移到FCM onMessageReceived,也遇到了同样的问题。

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: 创建一个类并扩展Service然后在内部创建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);
       }
    }
 }

Don't forget to register your Service class inside manifest. 不要忘记在清单中注册您的Service类。

Then inside method onMessageReceived make some changes. 然后在方法onMessageReceived内部进行一些更改。 Try this: 尝试这个:

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

Hope the might help you. 希望可能对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在锁定的android或ios中收到后台通知时,是否可以启动应用程序? - Is it possible to start an application when a background notification is received in a locked android or ios? 如何在应用程序在后台时收到的通知中保存数据 - How to save data from notification received when the app is in the background 如何取消应用程序在后台运行时收到的通知? - how to cancel a notification that received when app run in the background? 如何在收到通知且应用程序处于后台时调用 android 中的另一个活动 - How to call another activity in android when notification received and app is in background 收到Pushover通知后启动应用程序 - Start Application when Pushover notification has received 无法“打开”应用在后台时收到的通知 - Cannot "open" notification that is received when the app is in background 在后台收到通知时,如何让我的android应用执行代码? - How can I get my android app to execute code when a notification is received when it's in background? 收到推送通知(firebase)时如何在后台触发一些声音〜React Native - How to trigger some sound on background when push notification(firebase) received ~ React Native 当应用程序在后台或被杀死时,如何在收到通知时启动我的应用程序或活动? - how can i launch my app or an activity on notification received when app is in background or killed? 如何使用IBM Mobilefirst接收到推送通知时知道应用程序是在后台还是在前台 - how to know if app is in background or foreground when push notification is received using IBM Mobilefirst
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM