Register by 10/16 for $250 off the on-site price.

iOSクイックスタート

本クイックスタートではNotifyとTwilio NotifyサンプルiOS Appを使用してプッシュ通知を送信する方法をご紹介します。

To complete this guide you will need to have an Apple developer account, run Xcode on your machine, and have iOS Push Notifications configured.

コンソールでは、Notifyサービスを作成します。 SIDを書き留めておいてください! 後ほどコードを記述する際に使用します。

Notify SMSクイックスタート - Notifyサービスインスタンスのセットアップ

アカウント情報の収集


We need to get the necessary information from our Twilio account. Here's what we'll need:

設定値 概要
サービスインスタンスSID アプリケーションのすべてのデータが保存されスコープ範囲にあるServiceインスタンスです。 Console上で作成できます。

You will also need to create a push credential on the Twilio Console, and then configure it on your Notify service. You can upload your push credentials here. If you haven't set up the Apple Push Notification Service (APNS) for your app, you can do so by following the iOS push notification guide.

Set up Twilio Functions


When using the Notify service, you'll need to be able to register devices to receive notifications, and then send notifications to those devices. To get you going quickly, you can use Twilio Functions.

Functions is Twilio's serverless code execution environment that lets you write Node.js code that can respond to Twilio webhooks or to regular HTTP POST or GET requests. 

You can also always write your own server code using any of Twilio's supported server side languages (Node.js, Python, Ruby, C#, Java, or PHP) - check out how to register bindings or how to send a notification in those languages.

The sample mobile app is already set up to communicate with Twilio Functions to register a device for notifications. You just need to create two Functions in your account from a template, and then specify the URL for one of those Twilio Functions in the source code to the app.

To get started with this, create a new Twilio Function on the Twilio Console's Manage Functions page. Choose the Twilio Notify Quickstart template from the list of templates.

Twilio Notify Quickstart Function Template

After choosing the Twilio Notify Quickstart template, you'll be asked for one piece of configuration information - the Notification Service instance SID that you created at the beginning of this quickstart.

Notification Service SID Configuration

After entering that SID as the value, and clicking create, you will see two new Twilio Functions on your Functions page.

The first function is Register Binding - available at the **/register-binding** path. The quickstart mobile app sends an identity and the device token that it gets from the Apple Push Notification service to this function to create a binding. Notify uses these bindings to map the device that someone is using to their identity when you send a notification.

The second function is Send Notification - this is available at the **/send-notification** path. The mobile app does not use this function - instead you can use it to send notifications to yourself on the quickstart app.

If you select either function, you can see the Node.js source code, if you are curious to see how these are implemented.

Lastly, copy the URL for the register binding function - you will need that for the iOS app. Each account has a different subdomain for Twilio Functions - for instance, yours might be something like this:

https://sturdy-concrete-1657.twil.io

Now that you have the Twilio Functions created, it's time to set up the iOS app!

Twilio NotifyサンプルiOS Appのセットアップ


続いて必要になるもの:
  • Step 1: Twilio NotifyサンプルiOS Appを入手する
  • Step 2: まだ行っていない場合、iOSプッシュ通知を構成する
  • Step 3: Run the Twilio Notify sample iOS app on a device and have it pointing to the Twilio Functions URL

Step 1: Twilio NotifyサンプルiOS Appを入手する

Objective-CおよびSwiftですばやく使い始められるよう、サンプルアプリケーションを提供しています。

Download and unzip the version you prefer, and then open it in Xcode:

これらのアプリケーションはGitHub上で利用可能です(Objective-CSwift)。

Step 2: iOSプッシュ通知を構成する

まだ行なっていない場合は、このガイドにしたがってiOSプッシュ通知を構成します。

続いて、通知のプロジェクトの構成を開きます。 Generalタブで、Bundle IdentifierをiOSプッシュ通知の構成時に生成、認証を取得したものに一致するよう設定します。

Notify iOS Quickstart - サンプルモバイルApp bundle identifier

To be able to receive Push Notifications, we need to run the Twilio Notify sample iOS app on a device, as Push Notifications do not work on the iOS Simulator.

Step 3: Run Mobile app on a device and have it pointing to your Twilio Functions URL

To have the mobile app, running on your device, talking to your Twilio Functions, replace the server URL at the top of the ViewController file with your Twilio Functions subdomain URL.

Twilio Notify Functions URL

 

それでは、ご自身のデバイス上でAppをビルドしましょう。 Appの起動時、ユーザーのアイデンティティーを入力でいる簡単なUIがご覧いただけるはずです。

続いてユーザーのアイデンティティーとこのデバイスとのバインディングを作成します。

iOSプッシュ通知 (APNS) 用のバインディングの作成


Next we need to create a Binding between user Identity and the device running the app. User Identity can be any unique identifier you choose, like a GUID, or a primary key. User identity should not be personally identifiable information (PII), such as a name or an email address.

In the Twilio Console, navigate to the Twilio Notify Quickstart (Register Bindings) Function that you just created, and leave that web page open while you register with the app. That way, you can see logging information from your function.

App内では選択したアイデティティーを入力し、Registerボタンをクリックします。

Notify iOS クイックスタート - バインディングを作成するサンプルモバイルApp

 

このアクションは、アプリケーションで実行されている特定のデバイス上のユーザーを一意に識別するバインディングを作成します。

If you have the Register Bindings Twilio Function page open, you should see something like this

BindingInstance {
  _version: 
   V1 {
     _domain: 
      Notify {
        twilio: [Object],
        baseUrl: 'https://notify.twilio.com',
        _v1: [Circular] },
     _version: 'v1',
     _credentials: undefined,
     _services: 
      { [Function: ServiceListInstance]
        _version: [Circular],
        _solution: {},
        _uri: '/Services',
        create: [Function: create],
        each: [Function: each],
        list: [Function: list],
        page: [Function: page],
        get: [Function: get] } },
  sid: 'BSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  accountSid: 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX,
  serviceSid: 'ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX,
  dateCreated: 2017-01-17T13:07:39.000Z,
  dateUpdated: 2017-01-17T13:07:39.000Z,
  notificationProtocolVersion: '3',
  endpoint: 'user999999999XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  identity: 'user999999999',
  bindingType: 'apn',
  address: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  tags: [],
  url: 'https://notify.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Bindings/BSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
  _context: undefined,
  _solution: 
   { serviceSid: 'ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
     sid: 'BSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' } }

If the binding request wasn't successful, you'll see an error message printed in the Twilio Function logs instead. If there was a problem with the iOS app connecting to the Twilio Function, the error message may be printed out to Xcode's console by the iOS app.

アイデンティティーに個人を識別できる情報を使用しないでください

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 as Identity because the systems that will process this attribute assume it is not directly identifying information.

バインディングが用意できたので、Notifyを使用して最初のプッシュ通知を送信する準備は完了です。

Send a Push Notification to a device running the Twilio Notify sample iOS app


To send a notification, you can use the Twilio Notify Quickstart (Send Notification) Twilio Function you just created.

You can either send an HTTP GET or an HTTP POST request to the function. Typically, you would send a POST request to the function from your application (as you are performing an action, not retrieving information). If you don't have an easy way to POST a request (like Postman or curl), using GET is fine for testing your quickstart app.

For instance, you might make a request like:

https://sturdy-concrete-1657.twil.io/send-notification?identity=user1&body=Hello

Use the identity you used in the app. Because you registered a binding with Twilio, the server will send your device the 'Hello' message as a notification.

できましたね!ここからはあなたがご自身のアプリを作る番です。

関連トピック


Notifyで可能なことはさらにたくさんあります。 通知の送信に、他のクイックスタートもお試しください。

またはハウツー記事もご覧ください:

Notify APIの詳細情報については、REST APIドキュメントを参照してください。

ヘルプが必要ですか?

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