即使关闭我的应用程序,也可以在后台运行dart代码

亚瑟

在flutter中,我正在使用adhara_socket_io打开套接字并从服务器接收数据,以使用flutter_local_notifications推送通知,并且在应用程序运行时可以正常工作,现在我需要在后台运行此程序,以便在我的应用程序未运行时运行仍然从我的服务器接收数据并推送通知,无论如何还是要这样做吗?

   void initSocket() async {
    final deviceInfo = await Constants.getDeviceDetails();
    final String deviceId = deviceInfo[2];
    Uuid uuid = Uuid();
    print(deviceInfo);
    final id = uuid.v5(Uuid.NAMESPACE_URL, deviceId);
    print('id ' + id);

    SocketIOManager manager = SocketIOManager();
    SocketIO socket = await manager.createInstance(
      SocketOptions(URI,
          enableLogging: false,
          transports: [Transports.WEB_SOCKET /*, Transports.POLLING*/]),
    );
    socket.onConnect((data) {
      print("connected...");
      print(data);
      socket.emit('settings', [
        {'device_id': id}
      ]);
    });
    socket.onConnectError((_) => print('connect error'));
    socket.onConnectTimeout((_) => print('timeout'));
    socket.onError((_) => print('error'));
    socket.onDisconnect((_) => print('disconnect'));
    socket.on('notif', (data) {
      print('notif');
      _showNotificationWithDefaultSound(data['heading'],data['content']);
    });
    socket.connect();
  }

  initNotifications() {
    var initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializationSettingsIOS = IOSInitializationSettings();
    var initializationSettings = InitializationSettings(initializationSettingsAndroid,initializationSettingsIOS);
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings,onSelectNotification: onSelectNotification);
  }

  Future onSelectNotification(String payload)async{
    print('payload');
  }

  Future _showNotificationWithDefaultSound(heading,content) async {
  var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
      'your channel id', 'your channel name', 'your channel description',
      importance: Importance.Max, priority: Priority.High);
  var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
  var platformChannelSpecifics = new NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.show(
    0,
    heading,
    content,
    platformChannelSpecifics,
    payload: 'Default_Sound',
  );
}
叛徒

https://medium.com/vrt-digital-studio/flutter-workmanager-81e0cfbd6f6e
希望这会有所帮助,或者您可以使用fcm https://fireship.io/lessons/flutter-push-notifications-fcm-guide/

但是我是乞讨者。我对此不太了解。抱歉,这对您没有帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

即使关闭应用程序,在后台在Dart和Flutter中运行定期任务

我可以在后台运行Cordova应用程序吗

如何制作即使在后台模式下仍可以运行的计时器应用程序

即使应用程序在后台运行功能

我怎么知道我的应用程序在后台运行?

如何在后台运行我的 Web 应用程序?

在Tizen Web应用程序中,我可以使用javascript Web Worker在关闭的应用程序上运行后台代码吗?

如果未关闭应用程序(在后台运行),是否会导致应用程序崩溃?

即使Worklight应用程序未运行(包括不在后台)也可以调用wifi /地理触发器吗?

即使强制停止了应用程序,也要重新启动服务;关闭应用程序后,仍要在后台运行服务。

即使应用程序关闭,Android 应用程序也会通过在后台检查服务器(排球)中运行的服务显示通知

即使应用程序在后台,如何保持后台计时器在本机运行

在后台运行的WPF应用程序

使Web应用程序在后台运行

从最近使用的应用程序清除后,是否可以使应用程序在后台运行?

即使应用程序关闭,如何运行代码

即使应用程序关闭,如何运行代码

iOS企业版应用程序-如何确保我的应用程序在后台运行?

在 Ionic Vue 关闭或应用程序在后台运行之前运行操作

即使关闭应用程序也可以运行作业(Android Oreo)

即使在睡眠或重新启动后,仍在后台连续运行IOS应用程序

当应用程序在后台运行时,我可以拨打网络服务电话吗

我可以创建一个即使 Flutter 应用程序未运行也始终运行的后台服务?

该应用程序在后台运行超过10分钟后,Websocket关闭。(本机)

iOS-在关闭应用程序的情况下在后台运行网络请求

关闭后,Win32应用程序仍在后台运行

终端-在后台运行Java应用程序以及如何关闭它?

可以在后台运行应用程序时运行CADisplayLink吗?

即使应用程序在后台,如何发出通知?