AndroidとJavaを使用したChat
As the Programmable Chat API is set to sunset in 2022, we will no longer maintain these chat tutorials.
Please see our Conversations API QuickStart to start building robust virtual spaces for conversation.
Programmable Chat will no longer be available or supported after July 25, 2022. Instead, we'll be focusing on the next generation of chat: Twilio Conversations. Find out more about the EOL process here.
If you're starting a new project, please visit the Conversations Docs to begin. If you've already built on Programmable Chat, please visit our Migration Guide to learn about how to switch.
Ready to implement a chat application using Twilio Programmable Chat Client? Here is how it works at a high level:
- Twilio Programmable Chatはチャット機能の処理に使用される、核となる製品です。
- サーバー側のアプリケーションを使用して、皆さんの全Twilioアカウント情報を含むユーザーのアクセストークンを生成します。Programmable ChatクライアントはこのトークンをAPIとの接続に使用します。
Properatiは不動産の買い手と売り手をリアルタイムにつなぐ手助けをするモバイルメッセージングAppを構築しました。 さらに詳しく。
For your convenience, we consolidated the source code for this tutorial in a single GitHub repository. Feel free to clone it and tweak as required.
クライアントを初期化する - Part 1: アクセストークンを取得する
The first thing you need to create a client is an access token. This token holds information about your Twilio account and Programmable Chat API keys. We have created a web version of Twilio chat in different languages. You can use any of these to generate the token:
Volley を使用して、サーバーに対するリクエストを作成し、アクセストークンを取得します。
さて、いよいよトークンを使用してTwilioクライアントを初期化しましょう。
クライアントを初期化する - Part 2: クライアントの構築
Before creating a Programmable Chat Client instance we need to decide the region it will connect to and the synchronization strategy used to initialize it.
We can then pass this information along with the access token generated in the previous step and wait for the client to be ready.
次のステップはChannel一覧の取得です。
Channel一覧を取得する
Our ChannelManager
class takes care of everything related to channels. The first thing we need to do when the class is initialized, is to store a list of channels of type Channel
. To do this we call the method getChannels
from the Programmable Chat Client and extract a Channel
object from each ChannelDescriptor
returned.
Let's see how we can listen to events from the chat client so we can update our app's state.
クライアントイベントをリスニングする
Programmable Chatクライアントはアプリケーション上でonChannelAdded
やonChannelDeleted
といったイベントをトリガーします。 Channelの作成時や削除時には、スライディングパネルのChannelの一覧を再読み込みします。 Channelが削除されたものの、そのチャネル上に現在いる場合は、アプリケーションは自動的にgeneral Channelに参加します。
ChatClientListenerを使用して、イベントをリッスンするためにChatClient
を設定することが必要です。 この特定のケースでは、MainChatActivity
はChatClientListenerを実装しますが、そのメソッドはChatClientListener(クライアントのリスナー)も実装するChannelManager
クラスから呼ばれます。 ChannelManager
はイベントハンドラープロクシーとして使用されます。 Channelの読み込み時にTwilio Chatはリスナーを設定します。
Next, we need a default channel.
一般チャンネルに参加する
このアプリケーションは、起動時に"General Channel"という名前のChannelに参加しようとします。 このChannelが存在しない場合、その名前でChannelを作成します。 このサンプルアプリケーションでは、公開チャネルの挙動を紹介していますが、ChannelクライアントではプライベートChannelを作成して招待の処理を行ったりといったことも可能です。
アプリケーションを起動するたびに一般チャンネルを新たに作成したいわけではないので、一般チャンネルに一意の名前を設定します。
ここで、Channelイベントをいくつかリッスンしてみましょう。
チャンネルイベントをリスニングする
チャンネルのリスナーを ChannelListener
を実装する MainChatFragment
に設定しました。ここに、チャンネルイベントをリスニングする以下のメソッドを実装しました。
onMessageAdded
:接続しているチャンネルに誰かがメッセージを送信した場合。onMemberAdded
:誰かがチャンネルに参加した場合。onMemberDeleted
:誰かがチャンネルから退出した場合。
お気づきかもしれませんが、これらの各メソッドにはパラメーターとして便利なオブジェクトが含まれています。 チャネルに追加された実際のメッセージがそのひとつです。
ここで実際のチャットアプリケーションが用意できましたが、複数Channelを使用してもっと面白いものを作ってみましょう。
他のChannelに参加する
アプリケーションでは、Drawer Layout を使用して、該当の Twilio アカウントに作成されたチャンネルのリストを示します。
When you tap on the name of a channel, from the sidebar, that channel is set on the MainChatFragment
. The setCurrentChannel
method takes care of joining to the selected channel and loading the messages.
他のChannelに参加可能な場合、管理者向けに新規Channelを作成する(そして古いChannelを削除する)何らかの方法が必要です。
チャンネルを作成する
We use an input dialog so the user can type the name of the new channel. The only restriction here is that the user can't create a channel called "General Channel". Other than that, creating a channel is as simple as using the channelBuilder
as shown and providing at the very least a channel type.
You can provide additional parameters to the builder as we did with general channel to set a unique name. There's a list of methods you can use in the client library API docs.
良いですね、これでChannelの作成方法が分かったので、例えば誤って多くのChannelを作成するとしましょう。 この場合はこれら不要なChannelを削除できると便利です。 次はこれをやってみましょう!
チャンネルを削除する
Deleting a channel is easier than creating one. The application lets the user delete the channel they are currently joined to through a menu option. In order to delete the channel from Twilio you have to call the destroy
method on the channel you are trying to delete. But you still need to provide a StatusListener
to handle the success or failure of the operation.
That's it! We've built an Android application with the Twilio Chat SDK. Now you are more than prepared to set up your own chat application.
次はどこでしょうか?
Twilio を使う Java 開発者であれば、他のチュートリアルも調べてみてください。
Authyを使ってウェブアプリに二要素認証(2FA)を実装する方法を学びます。二要素認証はパスワードだけで守るよりも強力にユーザーのデータを守ります。
サーバー停止状態が発生した場合に SMS 経由ですべての管理者に警告するサーバー通知システムを構築する方法を学びます。
これは役に立ちましたか?
このチュートリアルをお読みいただき、ありがとうございます。ご意見やご感想などございましたら、ぜひお聞かせください。@twilio へツイートいただき、ご意見をお知らせください。
ヘルプが必要ですか?
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.