Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now


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?

iOS で通知を登録する

Ready to send your first notification? Of course you're not! That's why you're here. But don't worry, we'll get you registered so you can start sending notifications faster than a collision-free hash table lookup (OK, maybe not that fast).

A Notifications application involves two components:

  • 通知を登録、受信するクライアント (iOS) App
  • A server app that creates bindings and sends notifications

The aim of this guide is to show you how these two components work together when you register for notifications. Let's get to it!


クライアント: APNSでデバイスを登録する

Twilioの通知を使い始める前に、デバイスをAPNSに登録する必要があります。 この登録はAppの起動時にAppDelegateで毎回行われ、どのような通知タイプ(音、警告、バッヂ)を受け取りたいか指定できます。 最初の起動では、iOSはプッシュ通知を有効にするためポップアップ警告で知らせます。


        通知が有効にされると、iOSはdidRegisterForRemoteNotifications関数経由で非同期のレスポンスをAppに送信します。 iOS Appが通知を受け取るよう正しく構成していることを確認してください。 さもないと登録は失敗します。

        Client + Server: Creating a Binding


        Once we've received a successful registration response from the didRegisterForRemote NotificationsWithDeviceToken method, it's time to create a Binding. A “Binding” represents a unique device that can receive a notification. It associates a unique device token (provided by iOS) with an identity and an optional set of tags that you define for your application. When you want to send a notification, Twilio will send your notification only to Bindings that match the parameters you specify (more on that later).

        In iOS 13, the format of the string returned when you call the description method on an NSData object has changed from previous iOS versions. This means that code that parses the device token string from a call like [deviceToken description] or similar, where deviceToken is an NSData object, will break in iOS 13.
        The preferred way of creating a string of hex characters from bytes would be to iterate through each of the bytes and create a new string using a format string. There is an example in the Objective-C snippet for Creating a Binding.

        The client app should send a request to the server containing the device token and any additional information the server might need to create the binding (typically Identity and BindingType). We'll use the NSURLSession networking API that is part of the iOS standard library to make the request. You could also use another library, such as AFNetworking or Alamofire.

        The last step on the client side is storing the Endpoint identifier generated by Twilio and included in the response. Storing the Endpoint and reusing it in subsequent requests will allow us to avoid creating duplicated Bindings when the device token changes. Here we use the KeychainAccess helper class from our quickstart app but you can use your own way of accessing the keychain.



              In our server app, we’ll receive the POST request. It’ll contain the following four parameters.

              name description
              Identity The Identity to which this Binding belongs. Identity is defined by your application and can have multiple endpoints.
              BindingType The type of the Binding determining the transport technology to use. We will use apn here.
              住所 iOSより取得されたデバイストークンです。
              Endpoint The identifier of the device to which this registration belongs. This is generated the first time you create a Binding. Then you need to store it in the keychain and provide it in your subsequent registrations to avoid duplicating Bindings even if the device token changes.


              Notify uses Identity as a unique identifier of a user. You should not use directly identifying information (aka personally identifiable information or PII) like a person's name, home address, email or phone number, etc., as Identity because the systems that will process this attribute assume it is not directly identifying information.

              サーバーアプリケーション上でindex.jsとこれら4つのパラメーターを使用して、TwilioへのAPI呼び出しを行い、次世代Twilio Nodeヘルパーライブラリーを使用します。 これは、新規バインディングの作成に役立つでしょう。 /registerエンドポイントにこのロジックを実装しますが、必須ではありません。 あるいは、ログインやセッション初期化のフロー内にこれを統合する独自のAPIを思いつくかもしれません。


                    これでクライアントAppをAPNSに登録し、バインディングを作成できたので、いよいよ通知を送信してみましょう! さらなる詳細については、通知の送信ガイドをご覧ください。

                    Next: Sending Notifications »

                    Rate this page:


                    誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。