Menu

Expand
Rate this page:

Sending and Receiving Notifications

We are no longer allowing customers to onboard to Notify. We intend to deprecate the Notify product on April 25, 2024. Learn more in our Notify API End of Life Notice. We prepared this Transition Guide to assist in supporting your push notification use cases.

In this guide, we will review different scenarios for sending notifications with Notify, like using identity and tags, and show different useful tricks on how to receive Push notifications on iOS and Android devices.

Note, that to be able to send notifications, you first need to create bindings and to receive Push notifications on both iOS and Android, you first need to configure Push Notifications and register a device to receive Push Notifications.

Check out our other guides, that explain how to do that:

Once you have that done, you will be able to send and receive notifications. So lets get to it!

Table of Contents

Sending Notifications


We'll want to send a POST request to Twilio from notify.js in our server app. The request should authenticate using your Twilio Account SID and Auth Token. It should also identify who to send a notification to by either their identity or tags.

We'll use identity if we want a notification to be sent to all devices with Bindings associated with a given identity.

Loading Code Sample...
        
        

        Send a Notification (identity)

        If we want to send a notification to particular Binding of a user, we can use identity and tags together.

        Loading Code Sample...
              
              

              Send a Notification (Identity and Tag)

              Make sure you have consent from users to receive notifications.

              It is best practice, and also potentially required by law in certain jurisdictions, for you to have consent from your end users before sending messages to them, and you should respect your end users' choice to not receive messages from you. It is also important to make sure your database is up to date. This is particularly important for number-based communications like SMS because over time phone numbers may be reassigned to different individuals. If your database is out of date, you could inadvertently send a message to someone who did not consent but was reassigned a phone number that was previously subscribed to your service by another person. Check out the Twilio Marketplace for Add-ons from our partners that can help you keep your database up to date.

              Twilio recommends that you consult with your legal counsel to make sure that you are complying with all applicable laws in connection with communications you transmit using Twilio.

              Notifications on iOS


              In this section, we will review how to receive notifications with an iOS device and go through different scenarios of receiving notifications like App in the Background, App in the Foreground or using apn payload to display a badge.

              Receiving Notifications on iOS

              iOS has a native listener functions that fire whenever a notification is received. This happens in our AppDelegate.

              Loading Code Sample...
                    
                    

                    Receiving a Notification in iOS

                    iOS Notification Scenarios

                    There are a number of ways we can potentially receive a notification depending on the state of our app, our device type and channel specific payload specified in a notifications. Let's take look at some of these scenarios.

                    App is in the Background

                    • If it's an alert notification, it will show on the home screen and in notification center. The notification can be retrieved by parsing the launch options dictionary in application:willFinish LaunchingWithOptions: and application:didFinishLaunchingWithOptions

                    • If it's a silent notification, it will be delivered to application:didReceiveRemoteNotification :fetchCompletionHandler: and no alert will be raised

                    App is in the Foreground

                    When your app is in the foreground, the message is not shown in the device's notification center; instead, it is delivered to your app and needs to be processed. Please note that the app may stay in foreground mode up to 10 minutes after switching to another application.

                    • The application:didReceiveRemoteNotification:fetchCompletionHandler: method is called but no alert will be raised.

                    Displaying a badge

                    To send a notification to an iOS device dislaying a badge, you need to use apn payload.

                    Notify Sending and Receiving Notifications - displaying iOS badge

                    Notifications resource has channel specific parameters (payload) where you can specify information on how the user should be notified of a Push Notification, including displaying a badge on the app icon. To do that, just add "badge" : X (X being the number the app icon will be badged with).

                    Loading Code Sample...
                          
                          

                          Send a Detailed Notification with Badge

                          Note, device you want to send notifications to has to be registered for receiving Push notifications, including badges. See how to do that in our Registering for iOS Push Notifications guide

                          Yes, its that easy! Now all you need to do is implement badge count, to pass on the correct badge number depending on the logic of your application.

                          Notifications on Android


                          In this section, we will review how to receive notifications with an Android device and go through different scenarios of receiving notifications like App in the Background, App in the Foreground.

                          Receiving Notifications on Android

                          Android has a native listener functions that fire whenever a notification is received. This happens in our MyFcmListenerService.

                          Loading Code Sample...
                                
                                

                                Receiving a Notification in Android

                                The notification will arrive in JSON format and contains the message body as a String that we can then use locally.

                                Android Notifications Scenarios

                                Now lets see how notifications can be received depending on the state of your app.

                                App is in the Background

                                • Data message (default): If there is only a data bundle then the app is woken up in the background and the onMessageReceived function is called. This is the default behavior in Notify as we map all parameters to the data bundle.

                                In the Notification page, you can see how Notification request attributes are mapped to channel specific parameters.

                                • Notification message: If there is nothing in the data section, then it's added to the notification
                                center.

                                • Hybrid message: If there is a data section and a notification section, then it is added to the notification center and if the user clicks on the notification then the app is woken up and the data bundle is handed to the onMessageReceived function.

                                App is in the Foreground

                                The notification is delivered to onMessageReceived of the FcmListenerService. The data key-value pairs can be found in the data bundle, but the key-value pairs of the notification bundle are not available.

                                What's next


                                To get more in-depth look at the Notifications resources check out our REST API Guide.

                                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