0

我正在使用依赖项 flutter_geofence: ^0.4.1在接近地理位置点时尝试接收通知。我可以添加一个新的地理位置,但在进入和退出该区域时没有监听。

下面是不工作的代码。我该如何解决?

class _MyHomePageState extends State<MyHomePage> {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

@override
  void initState() {
    super.initState();

    initPlatformState();
  }

  Future<void> initPlatformState() async {

    if (!mounted) return;
    Geofence.initialize();

    Geolocation geolocation = const Geolocation(latitude: -23.52745196765867, longitude: -46.67853014552415, radius: 1000, id: 'allianzParque');

    Geofence.getCurrentLocation()
        .then((coordinate) {
      print("Your latitude is ${coordinate!.latitude} and longitude ${coordinate.longitude}");
    });

    Geofence.addGeolocation(geolocation, GeolocationEvent.entry).then((_) {
      print('ALLIANZ PARQUE ADDED');
      scheduleNotification("Georegion added", "Your geofence has been added!");
    }).catchError((error) {
      print("failed with $error");
    });

    Geofence.startListening(GeolocationEvent.entry, (entry) {
      scheduleNotification("Entry of a georegion", "Welcome to: ${entry.id}");
      print('ENTERING ALLIANZ PARQUE REGION');
    });

    Geofence.startListening(GeolocationEvent.exit, (entry) {
      scheduleNotification("Exit of a georegion", "Byebye to: ${entry.id}");
      print('EXITING ALLIANZ PARQUE REGION');
    });

    setState(() {});
  }

  void scheduleNotification(String title, String subtitle) {
    print("scheduling one with $title and $subtitle");
    var rng = Random();
    Future.delayed(const Duration(seconds: 1)).then((result) async {
      var androidPlatformChannelSpecifics = const AndroidNotificationDetails(
          'your channel id', 'your channel name', channelDescription: 'your channel description',
          importance: Importance.high,
          priority: Priority.high,
          ticker: 'ticker');
      var iOSPlatformChannelSpecifics = const IOSNotificationDetails();
      var platformChannelSpecifics = NotificationDetails(
          android: androidPlatformChannelSpecifics,
          iOS: iOSPlatformChannelSpecifics);
      await flutterLocalNotificationsPlugin.show(
          rng.nextInt(100000), title, subtitle, platformChannelSpecifics,
          payload: 'item x');
    });
  }
4

0 回答 0