Esempio n. 1
0
        public override void OnCreate()
        {
            base.OnCreate();
            Log.Info(TAG, "OnCreate: the service is initializing.");

            timestamper = new UtcTimestamper();
            handler     = new Handler();

            runnable = new Action(async() =>
            {
                if (timestamper == null)
                {
                    Log.Wtf(TAG, "Why isn't there a Timestamper initialized?");
                }
                else
                {
                    //  TODO get all the pending sms, then send the sms, then update
                    var items = new List <ShortMessage>();

                    //var url = "http://104.215.158.168/catering/";
                    var url = "http://batangas.southeastasia.cloudapp.azure.com/catering/";
                    using (var client = new HttpClient())
                    {
                        // send a GET request
                        var uri    = $"{url}api/message/forSend";
                        var result = await client.GetStringAsync(uri);

                        items.AddRange(JsonConvert.DeserializeObject <List <ShortMessage> >(result));
                    }

                    using (var post = new HttpClient(new NativeMessageHandler()))
                    {
                        foreach (var item in items.Where(p => p.DateSent == null))
                        {
                            try
                            {
                                //  SEND SMS
                                var piSent      = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0);
                                var piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);

                                _smsManager.SendTextMessage(item.Receiver, null, item.Body, piSent, piDelivered);

                                var uri  = $"{url}api/message/sent2/?id={item.ShortMessageId}";
                                var resp = await post.GetAsync(new Uri(uri));

                                //resp.EnsureSuccessStatusCode();
                                Log.Debug(TAG, item.Body);
                            }
                            catch (Exception ex)
                            {
                                Log.Error(TAG, ex.ToString());
                            }
                        }
                    }

                    string msg = timestamper.GetFormattedTimestamp();
                    Log.Debug(TAG, msg);
                    Intent i = new Intent(Constants.NOTIFICATION_BROADCAST_ACTION);
                    i.PutExtra(Constants.BROADCAST_MESSAGE_KEY, msg);
                    Android.Support.V4.Content.LocalBroadcastManager.GetInstance(this).SendBroadcast(i);
                    handler.PostDelayed(runnable, Constants.DELAY_BETWEEN_LOG_MESSAGES);
                }
            });
        }
Esempio n. 2
0
 /// <summary>
 /// This method will return a formatted timestamp to the client.
 /// </summary>
 /// <returns>A string that details what time the service started and how long it has been running.</returns>
 string GetFormattedTimestamp()
 {
     return(timestamper?.GetFormattedTimestamp());
 }