DwayneCoussement / flutter_geofence

BSD 3-Clause "New" or "Revised" License
42 stars 40 forks source link

The notification only appears when I hot reload the app. #40

Open jpgSouza opened 3 years ago

jpgSouza commented 3 years ago

I'm testing the flutter_geofence example and I was able to show the notification on android but only when I hot reload the app. Can anyone help me?

My code:

import 'dart:math';

import 'package:flutter/material.dart'; import 'package:flutter_geofence/geofence.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:flutter_modular/flutter_modular.dart'; import 'geofence_test_controller.dart';

class GeofenceTestPage extends StatefulWidget { final String title; const GeofenceTestPage({Key key, this.title = "GeofenceTest"}) : super(key: key);

@override _GeofenceTestPageState createState() => _GeofenceTestPageState(); }

class _GeofenceTestPageState extends ModularState<GeofenceTestPage, GeofenceTestController> { @override void initState() { super.initState(); initPlatformState();

var initializationSettingsAndroid =
    AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS =
    IOSInitializationSettings(onDidReceiveLocalNotification: null);
var initializationSettings = InitializationSettings(
    android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
    onSelectNotification: null);

}

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

Future initPlatformState() async { if (!mounted) return; Geofence.initialize(); Geofence.startListening(GeolocationEvent.entry, (entry) { scheduleNotification("Entry of a georegion", "Welcome to: ${entry.id}"); });

Geofence.startListening(GeolocationEvent.exit, (entry) {
  print("Exit of a georegion Byebye to: ${entry.id}");
});
setState(() {});

}

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), // -23.5354, -46.7727 body: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Center( child: RaisedButton( onPressed: () { Geolocation location = Geolocation( latitude: -23.5398, longitude: -46.7729, radius: 510.0, id: "Loja osasco", ); Geofence.addGeolocation(location, GeolocationEvent.entry) .then((onValue) { print("great success"); print(("Georegion added Your geofence has been added!")); }).catchError((onError) { print("great failure"); }); }, child: Text("Add location"), ), ), Center( child: RaisedButton( child: Text("Listen to background updates"), onPressed: () { Geofence.startListeningForLocationChanges(); Geofence.backgroundLocationUpdated.stream.listen((event) { print( "You moved significantly,a significant location change just happened."); }); }), ), Center( child: RaisedButton( child: Text("get user location"), onPressed: () { Geofence.getCurrentLocation().then((coordinate) { print( "great got latitude: ${coordinate?.latitude} and longitude: ${coordinate?.longitude}"); }); }), ), Center( child: RaisedButton( child: Text("Remove regions"), onPressed: () { Geofence.removeAllGeolocations(); }, ), ), ], ), ); }

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