public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
        {
            // Get current device token
            var DeviceToken = deviceToken.Description;

            if (!string.IsNullOrWhiteSpace(DeviceToken))
            {
                DeviceToken = DeviceToken.Trim('<').Trim('>');
                DeviceToken = Regex.Replace(DeviceToken, @"\s+", "");
            }

            // Get previous device token
            var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");

            // Has the token changed?
            if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
            {
                //TODO: Put your own logic here to notify your server that the device token has changed/been created!
                Console.WriteLine("RECIEVED TOKEN! " + DeviceToken);
                Settings.ApnsToken = DeviceToken;
                if (FireAuth.auth != null && !FireAuth.isTokenExpired() && FireDatabase.isDatabaseInit())
                {
                    FireDatabase.write("users/" + FireAuth.auth.User.LocalId + "/device_info/apns_token", DeviceToken);
                }
            }

            // Save new device token
            NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
        }
 public void StopLocationUpdates()
 {
     if (CLLocationManager.LocationServicesEnabled)
     {
         LocMgr.StopUpdatingLocation();
     }
     FireDatabase.write("cities/" + FireDatabase.currentCity + "/driver_locations/" + FireAuth.auth.User.LocalId, null);
 }
        public void locupdated(object sender, LocationUpdatedEventArgs e)
        {
            if (IOSRegisterDriverMode.shouldPushLocation)
            {
                Task.Factory.StartNew(() =>
                {
                    var userDB = FireDatabase.read("users/" + FireAuth.auth.User.LocalId).Result;
                    //Dictionary<string, Object> children = new Dictionary<string, Object>();
                    if (userDB.ContainsKey("stage_1_driving"))
                    {
                        string stageID = userDB["stage_1_driving"].ToString();
                        Dictionary <string, Object> tracker = new Dictionary <string, Object>();
                        tracker.Add("lat", e.Location.Coordinate.Latitude);
                        tracker.Add("lng", e.Location.Coordinate.Longitude);
                        tracker.Add("time", Time.CurrentTimeMillis());
                        FireDatabase.write("stage_1_ids/" + stageID + "/tracker/" + Time.CurrentTimeMillis(), tracker);
                    }

                    if (userDB.ContainsKey("is_driving"))
                    {
                        string rideID = userDB["is_driving"].ToString();
                        Dictionary <string, Object> tracker = new Dictionary <string, Object>();
                        tracker.Add("lat", e.Location.Coordinate.Latitude);
                        tracker.Add("lng", e.Location.Coordinate.Longitude);
                        tracker.Add("time", Time.CurrentTimeMillis());
                        FireDatabase.write("rides/" + rideID + "/tracker/" + Time.CurrentTimeMillis(), tracker);
                        FireDatabase.write("rides/" + rideID + "/latest_tracker", tracker);
                    }

                    Dictionary <string, Object> location = new Dictionary <string, Object>();
                    location.Add("lat", e.Location.Coordinate.Latitude);
                    location.Add("lng", e.Location.Coordinate.Longitude);
                    FireDatabase.write("cities/" + FireDatabase.currentCity + "/driver_locations/" + FireAuth.auth.User.LocalId, location);
                    if (!IOSRegisterDriverMode.shouldPushLocation)
                    {
                        FireDatabase.write("cities/" + FireDatabase.currentCity + "/driver_locations/" + FireAuth.auth.User.LocalId, null);
                    }
                    //FireDatabase.write("", children);
                });
            }
        }
        public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action <UIBackgroundFetchResult> completionHandler)
        {
            //Console.WriteLine("RECIEVED NOTIFICATION!");
            //FireDatabase.write("test", Time.CurrentTimeMillis().ToString());
            Console.WriteLine("Recieved a notification!");
            Console.WriteLine("test");
            var aps   = (NSDictionary)userInfo["aps"];
            var alert = aps["alert"];

            Console.WriteLine(alert.ToString());
            if (alert.ToString().Equals("No Drivers are in your area!"))
            {
                if (!backgrounded)
                {
                    if (MainPage.instance != null)
                    {
                        MainPage.instance.resetGUI();
                    }
                    showNotification(alert.ToString());
                }
                Settings.AppState = "DEFAULT";
            }
            else if (alert.ToString().Equals("You changed your Drop off Location!"))
            {
                var test = userInfo["verbose"];
                Instances.DropOffLocation = new DroverLocation(test.ToString(), Double.Parse(userInfo["lat"].ToString()), Double.Parse(userInfo["lng"].ToString()));
            }
            else if (alert.ToString().Equals("Rider changed their Drop off Location!"))
            {
                var test = userInfo["verbose"];
                Instances.DropOffLocation = new DroverLocation(test.ToString(), Double.Parse(userInfo["lat"].ToString()), Double.Parse(userInfo["lng"].ToString()));
            }
            else if (alert.ToString().Equals("SEARCHING FOR RIDE!"))
            {
                Settings.AppState = "SEARCHING";
            }
            else if (alert.ToString().Equals("No drivers accepted your request"))
            {
                if (!backgrounded)
                {
                    if (MainPage.instance != null)
                    {
                        MainPage.instance.resetGUI();
                    }
                    showNotification(alert.ToString());
                }
                Settings.AppState = "DEFAULT";
            }
            else if (alert.ToString().Equals("Your Drover is on the way!"))
            {
                if (!backgrounded)
                {
                    if (MainPage.instance != null)
                    {
                        var    dict   = FireDatabase.read("users/" + FireAuth.auth.User.LocalId).Result;
                        string rideID = dict["is_in_ride"].ToString();
                        Settings.LastRideID = rideID;
                        MainPage.instance.enterRiderGUI();
                    }
                    showNotification(alert.ToString());
                }
                Settings.AppState = "WAITING";
                Settings.InRide   = true;
            }
            else if (alert.ToString().Equals("You are being Requested!"))
            {
                if (!backgrounded)
                {
                    if (MainPage.instance != null)
                    {
                        //proposeRide();
                        //MainPage.instance.enterRiderGUI();
                    }
                    proposeRide();
                }
                Settings.AppState = "REQUESTED";
            }
            else if (alert.ToString().Equals("You have accepted a ride request."))
            {
                if (!backgrounded)
                {
                    MainPage.instance.enterDriverGui();
                }
                Settings.AppState = "DRIVING";
                var    dict   = FireDatabase.read("users/" + FireAuth.auth.User.LocalId).Result;
                string rideID = dict["is_driving"].ToString();
                Settings.LastRideID = rideID;
            }
            //base.DidReceiveRemoteNotification(application, userInfo, completionHandler);
            else if (alert.ToString().Equals("The ride is over!"))
            {
                Settings.InRide = false;
            }
        }