Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

ETA Notifications with C# and ASP.NET MVC

Download the Code

Companies like Uber, TaskRabbit, and Instacart have built an entire industry around the fact that we, the customers, like to order things instantly, wherever we are. The key to those services working? Notifying customers when things change.

Uber relies on Twilio SMS to keep customers up to date on their ridesharing requests. Learn more here.

In this tutorial, we'll build a notification system for a fake on-demand laundry service in C# and the ASP.NET MVC.

Let's get started! Click the button below to begin.

Let's Add Some ETA Notifications

Trigger a Notifications

There are two cases we'd like to handle:

  1. Delivery person picks up laundry to be delivered ( /Pickup )
  2. Delivery person is arriving at the customer's house ( /Deliver )

In a production app we would probably trigger the second notification when the delivery person was physically near the customer, using GPS.

(In this case we'll just use a button!)

Loading Code Sample...

      Controller methods to trigger delivery and pickup notifications


      Next, let's look at how we set up the Twilio REST API Client to push out notifications.

      Check out how to set up the Twilio REST Client

      Setting up the Twilio REST Client

      Here we create a helper class with an authenticated Twilio REST API client that we can use anytime we need to send a text message.

      We initialize it with our Twilio account credentials stored as environment variables. You can find the Auth Token and Account SID in the console:

      console credentials

      Loading Code Sample...

          Set up the Twilio REST Client


          Twilio REST Client ready, routes in place? Let's now look at how we handle notification triggers.

          Handle a Delivery Notification Request

          Handle a Notification Trigger

          This code handles a HTTP POST request triggered by a delivery person in the field.

          It uses our INotificationServices instance to send an SMS message to the customer's phone number, which we have registered in our database. Simple!

          Loading Code Sample...

              Handle a notification trigger


              Now let's look closer at how we send out the SMS.

              Check Out SMS Sending

              Send an SMS (or MMS)

              This code demonstrates how we actually send the SMS.

              Something missing? An image of the clothes, perhaps?

              You can add an optional mediaUrl as well. Glad you pointed that out!

              var mediaUrls = new List<Uri> () {
              new Uri("")
              var to = new PhoneNumber(phoneNumber);
              await MessageResource.CreateAsync(
              from: new PhoneNumber(_twilioNumber),
              body: message,
              statusCallback: new Uri(statusCallback),
              client: _client,
              mediaUrl: mediaUrls);

              In addition to the required parameters, we can pass a `status_callback` url. Twilio will then POST there to let us know when the status of our outgoing message changes.

              Loading Code Sample...

                  Using Twilio C# client to send the SMS


                  Let's look at the status callback in greater detail.

                  Show Me the Status Callback

                  Handle a Twilio Status Callback

                  Twilio will make a POST request to this resource each time our message status changes to one of the following: queued, failed, sent, delivered, or undelivered.

                  We then update this notificationStatus on the Order and business logic can take over. This is a great place to add logic that would resend the message upon failure, or send out an automated survey a few minutes after a customer receives an order.

                  Loading Code Sample...

                      Handle Twilio status update callbacks


                      That's a wrap (fold?)! We've just implemented an on-demand notification service that alerts our customers when their order is picked up and arriving.

                      Now let's look at other easy to add features for your application.

                      What Else Can I Build?

                      Where to Next?

                      We love ASP.NET and C# here at Twilio. Here are a couple other tutorials we've written:

                      Workflow Automation

                      Increase your rate of response by automating the workflows that are key to your business. In this tutorial, learn how to build a ready-for-scale automated SMS workflow for a vacation rental company.

                      Masked Numbers

                      Protect your users' privacy by anonymously connecting them with Twilio Voice and SMS. Learn how to create disposable phone numbers on-demand so two users can communicate without exchanging personal information.

                      Did this Help?

                      Thanks for checking this tutorial out! Let us know what you've built - or what you're building - on Twitter.

                      Agustin Camino Jennifer Aprahamian Paul Kamp Andrew Baker David Prothero Kat King Hector Ortega

                      Need some help?

                      We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

                      Loading Code Sample...