微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

终止应用程序后,Flutter Background_fetch lib无法运行

如何解决终止应用程序后,Flutter Background_fetch lib无法运行

我正在尝试实现推送通知类型功能,以提醒用户一些事件。 我希望我的应用每天从服务器获取一次或两次内容

我正在尝试使用background_fetch库。问题是当应用终止时,即使我设置了stopOnTerminate: false,,Job也不会被调用。这是我的配置代码

var config = BackgroundFetchConfig(
  minimumFetchInterval: 15,forceAlarmManager: false,stopOnTerminate: false,startOnBoot: true,enableHeadless: true,requiresBatteryNotLow: false,requiresCharging: false,requiresstorageNotLow: false,requiresdeviceidle: false,requiredNetworkType: NetworkType.ANY,);

BackgroundFetch.configure(config,(String taskId) async {
  print("[BackgroundFetch] Event received $taskId");

  print("my code is here");

  BackgroundFetch.finish(taskId);
}).then((int status) {
  print('[BackgroundFetch] configure success: $status');

}).catchError((e) {
  print('[BackgroundFetch] configure ERROR: $e');
});

我的MainActivity类:

public class MainActivity extends FlutterActivity implements PluginRegistry.PluginRegistrantCallback{

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    BackgroundFetchPlugin.setPluginRegistrant(this);
}


@Override
public void registerWith(PluginRegistry registry) {
    GeneratedpluginRegistrant.registerWith((FlutterEngine) registry);
}
}

我还在清单中添加xmlns:tools="http://schemas.android.com/tools"tools:replace="android:label"

我正在使用以下命令模拟作业:adb shell cmd jobscheduler run -f com.mycompany.appname 999
当应用程序处于活动状态或处于前台时,命令会反复调用我的函数(没问题,工作正常),但是当我终止应用程序时,命令会显示以下消息:Could not find job 999 in package com.mycompany.appname / user 0
有时它只给我Running job [FORCED]一次,但是实际上我的函数没有被调用,而在另一次运行中它告诉我没有工作。

我听说这在调试模式下不起作用,所以我使用Flutter run --release运行我的应用程序。好吧,没有成功。

可能是什么问题?预先感谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。