プッシュ通知用のサーバーの設定

Google Play は、発生したイベントに応じて通知を生成します。 企業に影響を及ぼす可能性があります。EMM ソリューション プロバイダは 通知し、その通知に基づいて、アラートやその他のメカニズムを 顧客の管理者に委任できます。

このガイドでは、必要なサーバー インフラストラクチャの構成方法について説明します。 EMM プッシュ通知のみを受信して処理する。 プル通知では、このガイドで説明する設定は必要ありません。

プッシュ通知では、このガイドで説明しているサーバーの設定に加えて、 適切な権限を付与し、その他の構成を行う必要もあります。 Google API Console で管理できます。詳しくは、 EMM プッシュ通知を有効にする をご覧ください。

Google Cloud Pub/Sub の詳細と例について詳しくは、 Cloud Pub/Sub のドキュメント

システムを設定して、プッシュ通知が 通知の送信を待機しているサーバーに送信できます。

push エンドポイントの構成について

push エンドポイントを構成するには、有効な SSL 証明書を持つサーバーが必要です。イン この例では、SSL 証明書を作成して認証局にアップロードします。 NGINX サーバーを構成します。最後に、テストコードをコンパイルして実行し、 設定が正しいことを確認します

この例では、Cloud Storage バケットを NGINX サーバーを サブスクライバー アプリに接続するリバース プロキシ モード(PushSubscriber.java) ポート 8093 で実行し、Ubuntu 14.04 を使用します。会社によっては、 ただし、サンプル設定は、変更なしですべての Debian ベースの 作成します他の分布(RedHat に基づく分布など)は似通っていますが、 構成ファイルの場所は異なる場合があります。

通知を受け取るには、事前に 次の基準を満たす:

  • ドメインを所有しており、 Google API Console:

  • ポート 443(SSL)でサービスを実行できる必要があります。

  • CA 署名付き SSL 証明書が必要です。自己署名証明書は機能しません。

  • 実行するウェブサーバーが Webhook

エンドポイントを Google App Engine で実行する必要はありません(実行は可能です)。

SSL 証明書を作成してアップロードする

1. セキュア ソケット レイヤ(SSL)証明書を生成します。

    myusername@myhost:/tmp$ sudo openssl req -x509 -nodes -days 365 \
      -newkey rsa:2048 -keyout cert.key -out cert.crt

これにより、次のレスポンスが生成されます。サンプル値を置換する (push.solarmora.commyusername@myhost など)を実際のサーバーに置き換えます name、company、address などです。任意の ただし、このサブドメインの A レコードがサーバーを参照している必要があります。

    Generating a 2048 bit RSA private key
    ...........................................................................
    .....+++
    writing new private key to 'cert.key'
    -----
    You are about to be asked to enter information that will be incorporated
    into your certificate request.
    What you are about to enter is what is called a Distinguished Name or a DN.
    There are quite a few fields but you can leave some blank
    For some fields there will be a default value,
    If you enter '.', the field will be left blank.
    -----
    Country Name (2 letter code) [AU]:GB
    State or Province Name (full name) [Some-State]:England
    Locality Name (eg, city) []:London
    Organization Name (eg, company) [Internet Widgits Pty Ltd]:Solarmora, Inc.
    Organizational Unit Name (eg, section) []:Creative Publications
    Common Name (e.g. server FQDN or YOUR name) []:push.solarmora.com
    Email Address []:admin@solarmora.com

2. 証明書ファイルが作成されたことを確認します。

$ myusername@myhost:/tmp$ ls cert*
cert.crt  cert.key

3. この証明書に署名するには、 証明書署名リクエスト(CSR)をコピーして署名者にアップロードします。

myusername@myhost:/tmp$ sudo openssl x509 -x509toreq -in cert.crt \
  -out cert.csr -signkey cert.key
Getting request Private Key
Generating certificate request
myusername@myhost:/tmp$ ls cert.*
cert.crt  cert.csr  cert.key

4. CSR ファイルの内容が次のようになっていることを確認します。

    Certificate Request:
    Data:
        Version: 0 (0x0)
        Subject: C=GB, ST=England, L=London, O=Solarmora, Inc.,
        OU=Creative Publications,
        CN=push.solarmora.com/emailAddress=admin@solarmora.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00:cc:0f:54:26:3d:d9:17:eb:8f:6c:f7:27:5e:77:
                    64:65:00:db:fe:2a:1f:fa:ea:de:21:7a:c5:5d:87:
                    ...
                    ...
                Exponent: 65537 (0x10001)
        Attributes:
            a0:00
    Signature Algorithm: sha256WithRSAEncryption
         1d:ea:12:b8:c2:6a:d6:f4:6e:92:2f:b9:12:5e:e3:91:15:a0:
         06:b5:81:ce:c5:cf:b7:d2:a7:dd:f2:78:ca:28:8e:21:cd:6d:
         ...
         ...
    -----BEGIN CERTIFICATE REQUEST-----
    MIIC6zCCAdMCAQAwgaUxCzAJBgNVBAYTAkdCMRAwDgYDVQQIDAdFbmdsYW5kMQ8w
    DQYDVQQHDAZMb25kb24xGDAWBgNVBAoMD0FDTUUgQ29ycCwgSW5jLjEYMBYGA1UE
    CwwPQ3JlYXRpdmUgQW52aWxzMRswGQYDVQQDDBJwdXNoLmFjbWUtY29ycC5jb20x
    IjAgBgkqhkiG9w0BCQEWE2FkbWluQGFjbWUtY29ycC5jb20wggEiMA0GCSqGSIb3
    ...
    ...
    -----END CERTIFICATE REQUEST-----

5. 証明書の一部を BEGIN と と END 行(両端を含む)が CA に送信されます。正確なプロセスは CA によって異なりますが、次の手順が含まれます。

  1. CSR ファイルを CA サイトにアップロードするか、ファイルの内容を CA サイトに貼り付けます。CA がこのデータを検証して処理します。
  2. CA によって生成された署名済み証明書をダウンロードします。

6. CA からの出力には複数のファイルが含まれているはずです。 権限があることを証明する CA の証明書を使用して、 提供します。ダウンロードしたすべての *.crt 証明書ファイルを連結します。 単一のバンドル ファイル(例: bundle.push.solarmora.com.crt)にバンドルします。

$ cat *.crt > bundle.push.solarmora.com.crt

プロキシ サーバーを構成する

このセクションでは、NGINX オープンソース ウェブサーバーを構成し、そのリバース プロキシ サーバーがエンドポイントを処理し、すべての受信リクエストを 購読者サーバーですここでは例として NGINX を使用していますが、 代わりに HTTP プロキシを使用してください。

1. NGINX をサーバーにインストールします。

    $ sudo apt-get update
    $ sudo apt-get install nginx
    $ nginx -v
    $ nginx version: nginx/1.4.6 (Ubuntu)

2. モジュールで作成した追加のサーバー設定ファイルは、 sites-enabled ディレクトリは NGINX によって処理されます。/etc/nginx/nginx.conf を編集します。 次の内容を含めます。

    $ include /etc/nginx/conf.d/*.conf;
    $ include /etc/nginx/sites-enabled/*;

3. 証明書ファイルを安全な場所にコピーします。これは、 www-data ユーザーですが、他のユーザーからは読み取りできないようにします( ウェブサーバーが別のユーザーとして実行されている場合は、 ユーザー):

    $ sudo mkdir -p /var/openssl/push.solarmora.com
    $ sudo mv /tmp/cert.key 
/var/openssl/push.solarmora.com/push.solarmora.com.key $ sudo mv /tmp/bundle.push.solarmora.com.crt
/var/openssl/push.solarmora.com/bundle.push.solarmora.com.crt

4. 新しい server 構成を作成します。push.solarmora.comを編集 に/etc/nginx/sites-enabledして、実際の サーバーの完全修飾ドメイン名を指定する必要があります。

server {
   listen 443;
   server_name push.solarmora.com;

   ssl on;
   ssl_certificate
     /var/openssl/push.solarmora.com/bundle.push.solarmora.com.crt;
   ssl_certificate_key
     /var/openssl/push.solarmora.com/push.solarmora.com.key;

   # it is usually very convenient to have separate files for your
   # access and error log to analyze for possible problems
   access_log /var/log/nginx/nginx.push.solarmora.com.log;
   error_log /var/log/nginx/nginx.push.solarmora.com.log;

   location / {
            # assuming the subscriber will run on the same machine
            # on port 8093
            proxy_pass http://localhost:8093;
   }
}

5. NGINX を再起動して変更を実装します。

    myusername@myhost:/etc/nginx$ sudo service nginx restart
    * Restarting nginx nginx
    ...done.

6. これでサーバーが構成されました。確認するには、 curl を使用してサーバーにクエリしてみます。

    [myusername@myhost:~]$ curl push.solarmora.com
    <html>
    <head><title>502 Bad Gateway</title></head>
    <body bgcolor="white">
    <center><h1>502 Bad Gateway</h1></center>
    <hr><center>nginx/1.4.6 (Ubuntu)</center>
    </body>
    </html>

ダウンストリーム サーバーが構成されていないため、これは想定されるレスポンスです。 (構成ファイル内の localhost:8093)。

サンプルのコンパイルと実行

このセクションの例を実行するには、アクティブな Google API Console が必要です。 できます。テスト用と作成用のスナップショットを作成することを 本番環境プロジェクトとは分ける必要があります。テストプロジェクトを作成したら サービス アカウントを作成する必要があります。 サービス アカウントのメールアドレスをメモし、関連する .p12 ファイルをサーバーの任意の場所に保存します。

ソースコード ツリーをセットアップする

1. play-work.git リポジトリのクローンを作成します。

    myusername@myhost:~/code$ git clone
    https://github.com/google/play-work.git
    Cloning into 'play-work'...
    Username for 'https://github.com': username
    Password for 'https://username@github.com':
    remote: Counting objects: 110, done.
    remote: Compressing objects: 100% (60/60), done.
    remote: Total 110 (delta 24), reused 95 (delta 9), pack-reused 0
    Receiving objects: 100% (110/110), 23.88 KiB | 0 bytes/s, done.
    Resolving deltas: 100% (24/24), done.
    Checking connectivity... done.
2.Debian ベースのシステムでは、MavenGoogle Protocol Buffers コンパイラ:
    $ sudo apt-get install maven protobuf-compiler

3. Maven と Google Protocol Buffers コンパイラの両方が 正しくインストールされています。

    myusername@myhost:~$ mvn -v
    Apache Maven 3.0.5
    Maven home: /usr/share/maven
    Java version: 1.7.0_75, vendor: Oracle Corporation
    Java home: /usr/lib/jvm/java-7-openjdk-amd64/jre
    Default locale: en_US, platform encoding: UTF-8
    OS name: "linux", version: "3.16.0-30-generic", arch: "amd64", family: "unix"
    myusername@myhost:~$ protoc --version
    libprotoc 2.5.0

4. Maven 構成ファイル pom.xml は、プロトコル バッファ コンパイラが /usr/bin/protoc ディレクトリにインストールされていることを前提としています。

    myusername@myhost:~$ which protoc
    /usr/bin/protoc
それ以外の場合は、pom.xml またはシンボリック リンク protoc を変更できます。
    $ sudo ln -s which protoc /usr/bin/protoc
5.サンプルをコンパイルします。mvn clean compile assembly:single を実行して、コードをビルドできることを確認します。これにより、 emm-notifications-[version-number]-jar-with-dependencies.jar、ここで [version number] は、この例の現在のバージョンです(例: 1.0-SNAPSHOT)。
    myusername@myhost:~/code/play-work/examples/emm-notifications$ ls target/*
    target/emm-notifications-1.0-SNAPSHOT-jar-with-dependencies.jar
6.コンパイルされた TestPublisher コードを実行できることを確認します。コードが失敗することが予想されます。

    myusername@myhost:~/code/play-work/examples/emm-notifications$ java -cp \
      target/emm-notifications-1.0-SNAPSHOT-jar-with-dependencies.jar \
      com.google.android.work.emmnotifications.TestPublisher
    Exception in thread "main" java.lang.IllegalArgumentException:
    You must specify non-default ServiceAccountEmail in
    settings.properties
        at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:119)
        at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:69)
        at com.google.android.work.emmnotifications.Settings.verifyVariable(Settings.java:129)
        at com.google.android.work.emmnotifications.Settings.getSettings(Settings.java:103)
        at com.google.android.work.emmnotifications.TestPublisher.main(TestPublisher.java:39)

7. settings.properties ファイル内の一部の値をオーバーライドする必要があります。そのためには、 ファイルのコピーを作成し、そのコピーのプロパティを次のように変更します。

    # This should be your own service account's email address
    ServiceAccountEmail=368628613713-t4hfexampledn5lhpdcu1qqfgio01626@developer.gserviceaccount.com
    ServiceAccountP12KeyFile=/opt/secret/secret.p12
    # This will be the name of the service account
    ProjectName=enterprise-cloud-pub-sub
    SubscriptionName=projects/enterprise-cloud-pub-sub/subscriptions/default
    TopicName=projects/enterprise-cloud-pub-sub/topics/default
    # The push endpoint in your API Console project
    PushEndpoint=https://push.solarmora.com

8. TestPublisher コードを再度実行して、クラッシュしなくなったことを確認します。( (1 つのエラーがログ出力に含まれる場合)

パブリッシャー テストコードを実行する

通知を公開するためのサンプルコードを実行して、 サブスクライバーが読み取るメッセージがあります。

次の例のコードは、 my_settings.properties で指定されているトピックが見つからないため、トピックが作成されます。 次に、メッセージをトピックにパブリッシュします。この例は、 Google Play EMM API から送信されたメッセージをエミュレートできるテストツール。

    myusername@myhost:~/code/play-work/examples/emm-notifications$ DEVELOPER_CONSOLE_SETTINGS=./my_settings.properties java -cp \
      target/emm-notifications-1.0-SNAPSHOT-jar-with-dependencies.jar com.google.android.work.emmnotifications.TestPublisher
    Feb 27, 2015 1:39:59 PM com.google.android.work.emmnotifications.RetryHttpInitializerWrapper$1 handleResponse
    INFO: RetryHandler: {
      "error": {
        "code": 404,
        "message": "Resource not found (resource=default).",
        "errors": [
          {
            "message": "Resource not found (resource=default).",
            "domain": "global",
            "reason": "notFound"
          }
        ],
        "status": "NOT_FOUND"
      }
    }

    Feb 27, 2015 1:39:59 PM com.google.android.work.emmnotifications.TestPublisher main
    INFO: Topic projects/enterprise-cloud-pub-sub/topics/default doesn't exists, creating it
    Feb 27, 2015 1:40:02 PM com.google.android.work.emmnotifications.TestPublisher main
    INFO: Topic projects/enterprise-cloud-pub-sub/topics/default created
    Feb 27, 2015 1:40:02 PM com.google.android.work.emmnotifications.TestPublisher main
    INFO: Publishing a request: {messages=[{data=CjEKFQoIMTIzMjEzMjESCXJpZ2h0IG5vdxIWY29tLmdvb2dsZS5hbmRyb2lkLmdtcxgA}]}

サブスクライバー テストコードを実行する

サブスクライバー テストコードは、Pub/Sub によってパブリッシュされたメッセージを TestPublisher コード。

1. コードが最新の状態でコンパイルされていることを確認してから、 登録者テストコード:

    myusername@myhost:~/code/play-work/examples/emm-notifications$ DEVELOPER_CONSOLE_SETTINGS=./my_settings.properties 
java -cp target/emm-notifications-1.0-SNAPSHOT-jar-with-dependencies.jar
com.google.android.work.emmnotifications.PushSubscriber Feb 27, 2015 1:46:37 PM com.google.android.work.emmnotifications.PushSubscriber main INFO: Will be using topic name: projects/enterprise-cloud-pub-sub/topics/default, subscription name:
projects/enterprise-cloud-pub-sub/subscriptions/default Feb 27, 2015 1:46:38 PM com.google.android.work.emmnotifications.PushSubscriber main INFO: Trying to get subscription named projects/enterprise-cloud-pub-sub/subscriptions/default Feb 27, 2015 1:46:38 PM com.google.android.work.emmnotifications.RetryHttpInitializerWrapper$1 handleResponse INFO: RetryHandler: { "error": { "code": 404, "message": "Resource not found (resource=default).", "errors": [ { "message": "Resource not found (resource=default).", "domain": "global", "reason": "notFound" } ], "status": "NOT_FOUND" } } Feb 27, 2015 1:46:38 PM com.google.android.work.emmnotifications.PushSubscriber main INFO: Subscription doesn't exist, will try to create projects/enterprise-cloud-pub-sub/subscriptions/default Feb 27, 2015 1:46:43 PM com.google.android.work.emmnotifications.PushSubscriber main INFO: Created: { "ackDeadlineSeconds" : 600, "name" : "projects/enterprise-cloud-pub-sub/subscriptions/default", "pushConfig" : { "pushEndpoint" : "https://push.solarmora.com" }, "topic" : "projects/enterprise-cloud-pub-sub/topics/default" }
サブスクライバーは実行中で、着信を受け入れる準備が整いました ブロックすることもできます。

2. パブリッシャーを再度実行すると、新しいメッセージが 次のログに追加されます。

    Feb 27, 2015 1:47:24 PM com.google.android.work.emmnotifications.PushSubscriber$1 handle
    INFO: Raw request: {"message":{"data":"CjEKFQoIMTIzMjEzMjESCXJpZ2h0IG5vdxIWY29tLmdvb2dsZS5hbmRyb2lkLmdtcxgA",
"attributes":{},"message_id":"71571141246"},"subscription":"/subscriptions/enterprise-cloud-pub-sub/default"} Feb 27, 2015 1:47:24 PM com.google.android.work.emmnotifications.PushSubscriber$1 handle INFO: Pubsub message received: { "attributes" : { }, "data" : "CjEKFQoIMTIzMjEzMjESCXJpZ2h0IG5vdxIWY29tLmdvb2dsZS5hbmRyb2lkLmdtcxgA", "message_id" : "71571141246" } Feb 27, 2015 1:47:24 PM com.google.android.work.emmnotifications.PushSubscriber$1 handle INFO: Message received: product_approval_event { common_event_information { enterprise_id: "12321321" event_notification_sent_timestamp: "right now" } product_id: "com.google.android.gms" approved: false }
メッセージは正常に受信され、処理されました。