LoginSignup
6
3

More than 5 years have passed since last update.

AWS Mobile Hubを使って速攻でAndroidアプリにAmazon SNS(with Google Cloud Messaging)でプッシュ通知機能を実装

Posted at

Mobile Hubを使うとよく使用する機能の幾つかを自動で生成してくれます。現在は以下のような機能を提供している。

1. User Sign-in
2. NoSQL Database
3. User Data Storage
4. App Analytics
5. Push Notifications
6. Cloud Logic
7. App Content Delivery

このうちのPush Notificationsの機能を使って、Androidに通知機能を組み込んでいく。

SNSがプッシュ通知機能に対応するプラットフォームは現在のところ、以下の7種類のものに対応している。

1. Amazon Device Messaging(ADM)
2. Apple production
3. Apple development
4. Baidu Cloud Push for Android in China
5. Google Cloud Messaging (GCM)
6. Microsoft MPNS for Windows Phone 7+
7. Microsoft WNS for Windows 8+ & Windows Phone 8.1+

このうちのGoogle Cloud Messaging(GCM)を使用する。

Google Cloud Messaging(以下、GCM)の設定

Google Developers Consoleの設定

  1. Google Developers Console(https://console.developers.google.com/ )を開き、[プロジェクトを作成]を選択
  2. 現れるポップアップに以下のように入力して[作成]を選択
    プロジェクト名: MyGCMTest

  3. ライブラリのページから、Mobile API中のGoogle Cloud Messagingを選択

  4. 画面上部の[有効にする]を選択

  5. GCMが有効になると現れる、[認証情報に進む]を選択

  6. 認証情報画面で以下のように入力する

必要な認証情報の種類を調べる

使用するAPI: Google Cloud Messaging
APIを呼び出す場所: ウェブサーバー (node.js、 Tomcatなど)

[必要な認証情報]を選択

APIキーを作成する

以下ともにデフォルトの値のままである。

名前: サーバーキー1
リクエストを受け入れるサーバーIPアドレス: 空欄

[APIキーを作成する]を選択

認証情報を取得する

内容を確認して、[完了]を選択

Google Developersの設定

Google DevelopersのAndroidの設定ページに以下のリンクからアクセスする
https://developers.google.com/mobile/add?platform=android&cntapi=gcm&cntapp=Default%20Demo%20App&cntpkg=gcm.play.android.samples.com.gcmquickstart&cnturl=https:%2F%2Fdevelopers.google.com%2Fcloud-messaging%2Fandroid%2Fstart%3Fconfigured%3Dtrue&cntlbl=Continue%20with%20Try%20Cloud%20Messaging

Create or choose app

1. 以下のように入力する。全てデフォルト値のままである。

App name: Default Demo App
Android package name: gcm.play.android.samples.com.gcmquickstart

[Choose and configure services]を選択

2. 発行されるServer API Key, Sender IDは何度か使用するので控えておく。

3. [Generate configure files]を選択
4. [Download google-services.json]を選択してgoogle-services.jsonをダウンロードする

AWS Mobile HubでSNSによるプッシュ通知

1. マネジメントコンソールからMobile Hubを選択
2. Create new mobile projectを選択
以下のように入力していく。

Project name: MyMobileHubTest

なお、デフォルトではResources for your project will be created in the US East (Virginia) region.
の記述があるようにVirginiaがリージョンとなるようなので、変更しておく。現地点(2016年8月)では、Virginia, Oregon, Sydney, Tokyo, Frankfurt, Irelandの6つのリージョンが選択できる。

[Create project]を選択

3. Push Notificationsの項目の[+]を選択
以下のように入力していく。

Do you wanto to send push notifications to your app?: Enable push

What platforms do you wanto to send messages to?: Android

API Key: <先ほどGoogle Developersの設定で生成されたAPI Key>
Sender ID: <先ほどGoogle Developersの設定で生成されたAPI Key>

What topics would you liked to send push push notifications to from your app?

[Create new topic]を選択
Topic name: MyMobileHubSNSTopic

[Save changes]を選択

4. [Build your app]を選択
5. [Android]を選択
6. [Download Android source package]を選択してソースコードをダウンロードする

Android Studioを開き、File > New > Import project...を選択し先ほどダウンロードしたファイルを選択してRunを実行してビルドすることで基本的な準備は終了である。

AWS側が作成したソースコードの使い方をマニュアルを用意しているのでそれを基本的には参考にすると良い。以下のステップを順々に追っていきながら、[I'm done with this step]を選択していく。

1. Setup
* Integrated Development...
2. Compile and Run
* Launch App
* User Identity - Try It!
* Push Notifications - Try It!
3. Discover
* What's in here?
* Where can I learn more?
4. Develop
* Use MySampleAPp as a Starter Aoo
* Use MySampleApp as an Example
* Update a MySampleApp b...
* Software License 
5. Troubleshoot
* Development Environment
* Push Notifications Issues

ソースコードの簡単な説明は、3. DiscoverのWhat's in here?中にある。

AWS Mobile SDK for Androidのマニュアルは、3. DiscoerのWhere can I learn more?中にURLが記載されている。
http://docs.aws.amazon.com/mobile/sdkforandroid/developerguide/

以上でAndroidアプリのソースコードの準備が完了である。

Device tokenの取得

GCMのサンプルアプリのビルド

Google Cloud MessagingのTry Cloud Messaging for Androidへアクセスする
https://developers.google.com/cloud-messaging/android/start

基本的には、ページ中に流れに沿うだけですぐに実装できる。
日本語で簡単にまとめると以下のようになる。

1. サンプルコードのダウンロード

$ git clone https://github.com/googlesamples/google-services.git

2. 設定情報の記述
先ほどダウンロードしたgoogle-services.jsonをcloneしてきたファイルのapp/以下に置く。
GcmSender.java中のAPI_KEYを先ほどGoogle Developersの設定でSender IDと共に控えたApi keyにする。

Androidで[Run]を実行する。

起動の際に以下のような内容がLogcatに表示される。このTokenは後ほど使用するので控えておく。

08-13 00:00:00.000 17811-17869/gcm.play.android.samples.com.gcmquickstart I/RegIntentService: GCM Registration Token: ********************************************************************************************************************

テストプッシュ

google-services/android/gcm/ へ移動し、以下のコマンドを実行する。

./gradlew run -Pmsg="Hello, world"

SNSによるプッシュ通知

1. Mobile Hubのナビゲーションペインから[Resources]を選択
2. Manage your resourcesの項目でAmazon SNS Platformから先ほど作成したSNSのトピックを選択

3. 作成したApplicationsにチェックを入れ、[Create platform endpoint]を選択

4. 現れるダイアログボックスに以下の内容を入力する。

Device token: <先ほど控えておいた, Android studioでサンプルアプリを起動した際にLogcatに表示されるToken>
User data: <空欄>

[Add endpoint]を選択

5. 作成したApplicationsのARNを選択

6. 登録したTokenにチェックを入れ、[Publish to endpoint]を選択
以下のように入力する

Message format: JSON
Message: [JSON message generator]を選択

7. 現れるダイアログボックスに以下のように入力する。
Message: Hello, world

[Generate JSON]を選択

8. 元のページに戻るので、[Publish message]を選択

以上で通知が来ることが確認できる。

(Appendix)Amazon SNS CLIコマンド一覧(1.10.56)

add-permission
check-if-phone-number-is-opted-out
confirm-subscription
create-platform-application
create-platform-endpoint
create-topic
delete-endpoint
delete-platform-application
delete-topic
get-endpoint-attributes
get-platform-application-attributes
get-sms-attributes
get-subscription-attributes
get-topic-attributes
list-endpoints-by-platform-application
list-phone-numbers-opted-out
list-platform-applications
list-subscriptions
list-subscriptions-by-topic
list-topics
opt-in-phone-number
publish
remove-permission
set-endpoint-attributes
set-platform-application-attributes
set-sms-attributes
set-subscription-attributes
set-topic-attributes
subscribe
unsubscribe

参考

GCMの実装
http://qiita.com/BlackCat/items/ef7cf50b893bd54718a7
SNSの設定
http://qiita.com/miyatay/items/b122ca1c019c985c78eb
MobileHubの説明
http://qiita.com/RyoheiMorimoto/items/ae905051593e98dc9790#aws-mobile-hubが生み出す価値
SNSのノウハウ
http://dev.classmethod.jp/cloud/aws/sns-mobile-token/

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3