Menu

Expand
Rate this page:

ETA Notifications with C# and ASP.NET MVC

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 Laundr.io 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...
        
        
        ETANotifications/Controllers/OrdersController.cs

        Controller methods to trigger delivery and pickup notifications

        ETANotifications/Controllers/OrdersController.cs

        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:

        Account Credentials

        Loading Code Sample...
              
              
              ETANotifications/Services/NotificationService.cs

              Set up the Twilio REST Client

              ETANotifications/Services/NotificationService.cs

              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...
                    
                    
                    ETANotifications/Controllers/OrdersController.cs

                    Handle a notification trigger

                    ETANotifications/Controllers/OrdersController.cs

                    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("http://lorempixel.com/image_output/fashion-q-c-640-480-1.jpg")
                    };
                    var to = new PhoneNumber(phoneNumber);
                    await MessageResource.CreateAsync(
                    to,
                    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...
                          
                          
                          ETANotifications/Services/NotificationService.cs

                          Using Twilio C# client to send the SMS

                          ETANotifications/Services/NotificationService.cs

                          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...
                                
                                
                                ETANotifications/Controllers/OrdersController.cs

                                Handle Twilio status update callbacks

                                ETANotifications/Controllers/OrdersController.cs

                                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.

                                Hector Ortega David Prothero Kat King Andrew Baker Paul Kamp Stephanie Marchante
                                Rate this page:

                                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 by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

                                Loading Code Sample...
                                      
                                      
                                      

                                      Thank you for your feedback!

                                      Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

                                      Sending your feedback...
                                      🎉 Thank you for your feedback!
                                      Something went wrong. Please try again.

                                      Thanks for your feedback!

                                      thanks-feedback-gif