【问题标题】:How to work with background services Flutter?如何使用后台服务 Flutter?
【发布时间】:2022-12-12 19:46:44
【问题描述】:

您好我正在尝试创建一个 flutter 应用程序,即使在应用程序关闭时也会显示通知。

我设法找到了一些与那个东西相关的插件flutter_background_service,但是当我尝试复制示例中显示的代码时,它显示了一些与 AndroidServiceInstance 和 setAsBackgroundService() 相关的错误,说它们未定义。如何解决这个问题?有没有更好的方法来实现我想要的?谢谢之前。

这是我当前的代码:

Future<void> initializeService() async {
  final service = FlutterBackgroundService();

  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    notificationChannelId, // ID
    'MY FOREGROUND SERVICE', // Title
    description:
    'This channel is used for important notifications.', // Description
    importance: Importance.low, // importance must be at low or higher level
  );

  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

  if (Platform.isIOS) {
    await flutterLocalNotificationsPlugin.initialize(
      const InitializationSettings(
        iOS: IOSInitializationSettings(),
      ),
    );
  }

  await flutterLocalNotificationsPlugin
      .resolvePlatformSpecificImplementation<
      AndroidFlutterLocalNotificationsPlugin>()
      ?.createNotificationChannel(channel);

  await service.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,

      notificationChannelId: 'my_foreground',
      initialNotificationTitle: 'AWESOME SERVICE',
      initialNotificationContent: 'Initializing',
      foregroundServiceNotificationId: 888,
    ),

    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );
  service.startService();
}

onStart(ServiceInstance service) async {
  DartPluginRegistrant.ensureInitialized();

  if (service is AndroidServiceInstance) {
    service.on('setAsForeground').listen((event) {
      service.setAsForegroundService();
    });
    service.on('setAsBackground').listen((event) {
      service.setAsBackgroundService();
    });
  }
  service.on('stopService').listen((event) {
    service.stopSelf();
  });
}

【问题讨论】:

    标签: flutter


    【解决方案1】:

    最终 FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); final AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher'); final InitializationSettings 初始化设置 = InitializationSettings(android: initializationSettingsAndroid); 等待 flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: (payload) 异步 => handleLocalNotification(有效载荷,消息:消息)); debugPrint('在前台收到消息!'); const AndroidNotificationChannel 通道 = AndroidNotificationChannel( 'high_importance_channel', // id '高重要性通知', // titledescription 重要性:Importance.max, ); 等待 flutterLocalNotificationsPlugin .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(频道); RemoteNotification notification = message.notification!; AndroidNotification android = message.notification!.android!;

    【讨论】:

      猜你喜欢
      • 2021-09-16
      • 1970-01-01
      • 2021-08-08
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多