LoginSignup
5
3

More than 5 years have passed since last update.

aws cliでAmazon Pinpoint経由でFCMを叩くまで

Last updated at Posted at 2019-02-05

あんまりAWS Pinpointを使った系の記事がなかったので、備忘録として残しておきたいと思います。

前提

  • FCMとAndroidアプリのセットアップは終わっている
  • PinpointにFCMのサーバーキーを登録済み

Application ID

まずはapplication Idを取得する。

  • Pinpointのコンソール画面でプロジェクトを選択。
  • SettingsのタブのGeneral settingsをクリック。
  • Project detailsの中のProject IDがapplication Id。

aws cliからpinpointへの疎通確認

application Idが正しいかどうかを下記コマンドでチェック

aws pinpoint get-app --application-id 上記ID

なお、python2.x系のaws cliだとServer Name Indicationに対応していないとのことでエラーになる。
その場合、一度aws cliをアンインストールしてからpython3系をインストール(※1)して、pipがpython3系で動いていることを確認の上(pip --version)、下記コマンドで再度awscliをインストールすること。
※1.参照 https://qiita.com/f-akazawa/items/5ec741f8a5cca999d7b3

pip install awscli --upgrade --user

話をapplication Idの確認に戻す。
この際、普段使用しているprofileのregionがap-north-east-2だったりすると

Could not connect to the endpoint URL:~

とエラーが出る。
現在のprofileは下記で確認できる。

aws configure list

Pinpointで使えるRegionは下記4つ。

Region Name Region
US East (N. Virginia) us-east-1
US West (Oregon) us-west-2
EU (Frankfurt) eu-central-1
EU (Ireland) eu-west-1

今回はus-west-2でpinpointを使用しているので、us-west-2用のprofileが必要となる。
~/.aws/credentialsと~/.aws/configを下記のように編集する。アクセスキーのセットは同一。

~/.aws/credentials
[default]
aws_access_key_id = アクセスキー
aws_secret_access_key = シークレットアクセスキー

[pinpoint]
aws_access_key_id = アクセスキー
aws_secret_access_key = シークレットアクセスキー
~/.aws/config
[default]
output = json
region = ap-northeast-2

[profile pinpoint]
output = json
region = us-west-2

そして下記を実行

export AWS_PROFILE=pinpoint

aws configure listでregionがus-west-2になっていることを確認する。

その上で、再度下記を実行して、

aws pinpoint get-app --application-id 上記ID

下記のレスポンスが得られればaws cliからpinpointへの疎通確認は成功である。

{
    "ApplicationResponse": {
        "Id": "Project Id",
        "Name": "project Name"
    }
}

実際にメッセージを送る際

send-messagesを使う。
下記コマンドでjsonのひな型を取得する。

aws pinpoint send-messages --generate-cli-skeleton > test.json

test.jsonを編集

test.json
{
    "ApplicationId": "プロジェクトID",
    "MessageRequest": {
        "Addresses": {
            "Androidアプリのdevice Token": {
                "ChannelType": "GCM"
             }
        },
        "MessageConfiguration": {
            "GCMMessage": {
                "Action": "OPEN_APP",
                "Body": "small match!!!",
                "CollapseKey": "testApp",
                "Data": {
                    "message": "test message"
                },
                "IconReference": "",
                "ImageIconUrl": "",
                "ImageUrl": "",
                "Priority": "high",
                "RestrictedPackageName": "",
                "SilentPush": false,
                "SmallImageIconUrl": "",
                "Sound": "son.m4a",
                "Substitutions": {
                    "KeyName": [
                        ""
                    ]
                },
                "TimeToLive": 0,
                "Title": "Portugal vs Denmark",
                "Url": ""
            }
        },
        "TraceId": ""
    }
}

このjsonを使う際には下記のようにする。
jsonファイルのパスにfile://をつけること。

aws pinpoint send-messages --cli-input-json file://test.json

Androidアプリ側で取得できる内容は下記

Bundle[
  {
    google.delivered_priority=high, 
    google.sent_time=1547699952834, 
    google.original_priority=high, 
    google.message_id=********9995284153************, 
    from=****10578***, 
    pinpoint.jsonBody={"message":"test message"}, 
    pinpoint.openApp=true, 
    pinpoint.notification.sound=son.m4a, 
    pinpoint.notification.title=Portugal vs Denmark, 
    pinpoint.notification.body=small match!!!, 
    pinpoint.campaign.campaign_id=_DIRECT, 
    pinpoint.notification.silentPush=0, 
    collapse_key=testApp
  }
]
5
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
5
3