Ejemplo n.º 1
0
        public void SendNewForecastNotification(WeatherForeCastModel forecast)
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
            {
                UNUserNotificationCenter.Current.GetNotificationSettings(settings =>
                {
                    if (settings.AlertSetting != UNNotificationSetting.Enabled)
                    {
                        return;
                    }

                    Debug.WriteLine("I want to notify you about sth: " + forecast);

                    var content = new UNMutableNotificationContent()
                    {
                        Title = "New  Forecast for " + forecast.Time,
                        Body  = $"{forecast.Temperature} {SystemSettings.MeasurementUnitTemperature}, {forecast.Description}",
                        //ToDo: get the pictures here, can be done over internet I think

                        /*Attachments = new UNNotificationAttachment[]
                         * {
                         *  UNNotificationAttachment.FromIdentifier("image",
                         *      new NSUrl(new FilePathService().GetLocalFilePath(forecast.Icon)),
                         *      new UNNotificationAttachmentOptions(),
                         *      out var attachmentError)
                         * }*/
                    };

                    var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
                    var request = UNNotificationRequest
                                  .FromIdentifier(forecast.Id.ToString(), content, trigger);
                    UNUserNotificationCenter.Current.AddNotificationRequest(request,
                                                                            error =>
                    {
                        if (error != null)
                        {
                            Debug.WriteLine("Upsiiii: " + error);
                        }
                    });
                });
            }
            else
            {
                Debug.WriteLine("The devices iOS Version should be 10 or higher", "error");
            }
        }
Ejemplo n.º 2
0
        public List <WeatherForeCastModel> GetForeCasts(Stream jsonStream)
        {
            var forecasts = new List <WeatherForeCastModel>();

            using (StreamReader sr = new StreamReader(jsonStream))
            {
                var jsonString = sr.ReadToEnd();
                if (string.IsNullOrWhiteSpace(jsonString))
                {
                    return(null);
                }
                else
                {
                    var jsonForecasts = JObject.Parse(jsonString)["list"];
                    foreach (var jsonForecast in jsonForecasts)
                    {
                        WeatherForeCastModel forecast = new WeatherForeCastModel();

                        try
                        {
                            forecast.Description   = jsonForecast["weather"]?[0]?["description"]?.Value <string>() ?? "";
                            forecast.CloudCover    = jsonForecast["clouds"]?["all"]?.Value <double>() ?? 0f;
                            forecast.Condition     = jsonForecast["weather"]?[0]?["id"]?.Value <int>() ?? 0;
                            forecast.Humidity      = jsonForecast["main"]?["humidity"]?.Value <double>() ?? 0f;
                            forecast.Icon          = jsonForecast["weather"]?[0]?["icon"]?.Value <string>() ?? "";
                            forecast.Pressure      = jsonForecast["main"]?["pressure"]?.Value <double>() ?? 0f;
                            forecast.Rain          = jsonForecast["rain"]?["3h"]?.Value <double>() ?? 0f;
                            forecast.Snow          = jsonForecast["snow"]?["3h"]?.Value <double>() ?? 0f;
                            forecast.Temperature   = jsonForecast["main"]?["temp"]?.Value <double>() ?? 0f;
                            forecast.WindDirection = jsonForecast["wind"]?["deg"]?.Value <double>() ?? 0f;
                            forecast.WindSpeed     = jsonForecast["wind"]?["speed"]?.Value <double>() ?? 0f;
                            forecast.Time          = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(jsonForecast["dt"]?.Value <double>() ?? DateTime.Now.ToOADate());
                        }
                        catch (Exception e)
                        {
                            Debug.WriteLine("An Error occured while parsing json: " + e.StackTrace);
                        }

                        forecasts.Add(forecast);
                    }
                }
            }
            return(forecasts);
        }
Ejemplo n.º 3
0
        public void SendNewForecastNotification(WeatherForeCastModel forecast)
        {
            Notification.Builder notificationBuilder = null;
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                notificationBuilder = new Notification.Builder(CrossCurrentActivity.Current.Activity, CHANNEL_ID);
            }
            else
            {
#pragma warning disable CS0618 // Type or member is obsolete
                notificationBuilder = new Notification.Builder(CrossCurrentActivity.Current.Activity);
#pragma warning restore CS0618 // Type or member is obsolete
            }

            notificationBuilder
            .SetContentTitle("New  Forecast for " + forecast.Time)
            .SetContentText($"{forecast.Temperature} {SystemSettings.MeasurementUnitTemperature}, {forecast.Description}")
            .SetSmallIcon(Android.App.Application.Context.Resources.GetIdentifier("pic" + forecast.Icon, "drawable", "com.companyname.WeatherApp"))
            .SetAutoCancel(true);

            // When the user clicks the notification, SecondActivity will start up.
            Intent resultIntent = new Intent(CrossCurrentActivity.Current.Activity, typeof(MainActivity));
            resultIntent.PutExtra("ID", forecast.Id);

            // Construct a back stack for cross-task navigation:
            TaskStackBuilder stackBuilder = TaskStackBuilder.Create(CrossCurrentActivity.Current.Activity);
            stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
            stackBuilder.AddNextIntent(resultIntent);

            // Create the PendingIntent with the back stack:
            PendingIntent resultPendingIntent =
                stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
            notificationBuilder.SetContentIntent(resultPendingIntent);

            Notification        notification        = notificationBuilder.Build();
            NotificationManager notificationManager =
                CrossCurrentActivity.Current.Activity.GetSystemService(Context.NotificationService) as NotificationManager;
            notificationManager.Notify(forecast.Id, notification);
        }
 public WeatherForeCastDetailPage(WeatherForeCastModel weatherForeCastModel)
 {
     InitializeComponent();
     Title = "Weather Details";
     this.BindingContext = new WeatherForeCastViewModel(weatherForeCastModel);
 }