返回

dart-当进入和退出区域时,Flatter应用程序上的Geofence未监听

发布时间:2022-05-05 02:36:36 323
# android# flutter# git

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

下面是不起作用的代码。我该怎么修?

class _MyHomePageState extends State {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();

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

    initPlatformState();
  }

  Future 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');
    });
  }
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像