首页 > 解决方案 > 收到远程通知时在后台运行该方法而不启动应用程序

问题描述

我需要android中的机制,比如ios中的静默通知,需要在后台运行方法而不在我们收到通知时启动应用程序,怎么做?

public void onMessageReceived(RemoteMessage remoteMessage) {

       AsyncTaskExample asyncTask=new AsyncTaskExample();
       asyncTask.execute("http://100:5000/api/v1/todos");

   }

在前台它正在工作,在后台不工作甚至在 firebaseJson 中也包含数据

标签: android

解决方案


不幸的是(或幸运的是),如今的 Android 严重限制了后台行为,尤其是网络调用。请参考https://developer.android.com/about/versions/oreo/background

如果您要执行一些长时间运行的任务或使用 JobScheduler/WorkManager,您可能应该启动前台服务。

希望这可以帮助!


推荐阅读