Event Notificationsを使用したAndroid携帯へのプッシュ通知の作成と送信
Event Notifications サービスを作成し、Firebase Cloud Messaging (FCM) のプッシュ宛先を追加し、Android デバイスにメッセージを送信します。
Event Notifications とは何ですか?
Event Notificationsは、IBM Cloudアカウントで発生した重要なイベントを通知したり、Webhookを使用して自動化されたアクションをトリガするイベント通知ルーティングサービスです。 You can filter and route event notifications from IBM Cloud services like Availability Monitoring, to email, SMS, push notifications, and webhooks.
クライアントは Android プッシュ通知をどのように使用しますか。
以下の図は、クライアントが Android プッシュ通知を使用する方法を示しています。
目標
このチュートリアルでは、以下のようにプッシュ通知を送信する方法について説明します。
- Event Notifications を使用してモバイル・アプリケーションを作成します。
- FCM 資格情報を取得します。
- コードをダウンロードし、通知のセットアップを完了します。
- Android プッシュ通知を構成してモバイル・デバイスに送信します。
開始前に
以下の前提条件を満たしている必要があります。
- コードをインポートして拡張できるように、Android Studio をダウンロードしてインストールします。
- Firebase コンソールにログインして
project_id
、private_key
、およびclient_email
を取得するための Google アカウント。 - IBM Cloud アカウント。 持っていない場合は、 IBM Cloud アカウントを作成します。
このドキュメントで使用している手順は、FCMのHTTPv1APIを使用しています。 HTTPv1APIには、セキュアなアクセストークン、効率的なカスタマイズ、将来性の証明、拡張可能なクライアント・プラットフォーム・バージョンなどの利点がある。 FCMレガシーHTTP APIからHTTPv1APIへの移行の詳細については、FCMレガシーHTTP APIからHTTPv1APIへの移行 を参照してください。
Event Notifications サービス・インスタンスの作成
- IBM Cloudアカウント にログインします。
- IBM Cloud カタログ で、
Event Notifications > Event Notifications
を検索して選択します。 - サポートされるリージョンのリストから
Region
を選択し、pricing plan
を選択します。 Service name
を提供します。resource group
を選択します。- チェック・ボックスをクリックして、ご使用条件と条件に同意します。
Create
をクリックします。
FCM 資格情報の取得
Firebase Cloud Messaging (FCM) は、Android デバイスにプッシュ通知を配信するゲートウェイです。 コンソールでAndroid Pushの送信先を設定するには、FCMのクレデンシャル'project_id
、'private_key
、'client_email
を取得する必要があります。
-
Firebase コンソールに移動します。 Google ユーザー・アカウントが必要です。
-
Create a project
をクリックします。 既にプロジェクトがある場合は、Add Project
をクリックします。 -
Create a project window
で、プロジェクト名を入力し、条件に同意し、トグル・スイッチを選択して Google アナリティクスを有効または無効にします (オプション)。Continue
をクリックします。 -
Google アナリティクスが有効になっている場合は、「
Configure Google Analytics
」ウィンドウでAnalytics location
を選択し、条件に同意します。 -
Create Project
をクリックします。 -
新規プロジェクトの準備ができたら、
Continue
をクリックします。 -
ナビゲーション・パネルで、
Project Overview
の横にあるsettings
アイコンを選択し、Settings > Project settings
を選択します。 -
Service Accounts
タブをクリックする。caption-side=bottom" -
「新規秘密鍵の生成」 をクリックして、プロジェクト資格情報を生成します。 ダウンロードしたファイルには、
project_id
、private_key
、およびclient_email
が含まれます。
生成google-services.json
google-services.json
ファイルも生成する必要があります。 以下のステップを実行します。
-
Firebase コンソールの「プロジェクトの概要」セクションで、
Get started by adding Firebase to your app
セクションの下にあるAndroid
アイコンをクリックします。を始める -
「
Add Firebase to your Android app
」ウィンドウで、パッケージ名としてcom.ibm.cloud.eventnotifications.destination.android
を追加します。App nickname
フィールドはオプションです。 -
**「アプリケーションの登録」**をクリックします。
{: caption="に追加するFirebaseをAndroid" caption-side="bottom"}に追加するFirebaseをAndroidアプリに追加する
-
アプリケーションのパッケージ名を含めます。
Add Firebase to your Android app
ウィンドウにパッケージ名を入力します。App nickname
フィールドはオプションです。 -
**「アプリケーションの登録」**をクリックします。 以下の例を参照してください。
{: caption="を登録するAndroid" caption-side="bottom"}を登録するAndroidアプリを登録する
-
google-services.json
ファイルが生成されます。 -
アプリの下に最新の構成ファイル
google-services.json
をダウンロードします。
汎用 API ソースの追加
次のステップを実行します。
- Event Notifications ダッシュボードの
Sources
セクションに移動します。 Add
をクリックし、API ソースを選択します。- 名前とオプションの説明を入力し、
Add
をクリックします。
Event Notifications 宛先の作成
Event Notifications コンソールでDestinations
をクリックし、以下の宛先詳細を追加します。
Name
: 宛先の名前を追加します。Description
: 宛先の説明 (オプション) を追加します。Type
: ドロップダウンリストから'Android Push Notifications (FCM)
タイプを選択する。- 宛先計画 (実動前宛先または実動宛先) を選択します。
Pre-production destination
-開発環境およびテスト環境用の低コストのプッシュ宛先としてこの宛先を選択します。Production destination
-この宛先の全機能を使用します。 デバイスと送信メッセージは無制限。
- 前にダウンロードしたファイルの
project_id
、private_key
、およびclient_email
を使用して、FCM プッシュ資格情報を更新します。 - 追加 をクリックします。
Event Notifications トピックの作成
Event Notifications コンソールでTopics
を選択し、Create
をクリックします。 以下のトピックの詳細を入力します。
Name
: トピックの名前を入力します。Description
: トピックのオプションの説明を追加します。Source
: ドロップダウン・リストからソースを選択します。Event type
: ドロップダウン・リストからイベント・タイプを選択します。Event sub type
: イベント・サブタイプ・ドロップダウン・リストからイベント・サブタイプを選択します。Severity
: 重大度ドロップダウン・リストから重大度を選択します。Advanced conditions
: 独自のカスタム条件を作成します。jsonpath の指定に従う必要があります。
Event Notifications サブスクリプションの作成
Event Notifications コンソールでSubscriptions
をクリックします。 以下のサブスクリプションの詳細を入力します。
Click
をクリックして、サブスクリプション・ウィザードを表示します。- 以下のサブスクリプションの詳細を入力します。
Subscription name
: サブスクリプションの名前。Subscription description
: オプションの説明を追加します。
Subscribe to a topic
セクションの下で、ドロップダウン・リストからトピックを選択し、宛先ドロップダウン・リストから宛先を選択します。Destination type
:Destination
でタイプを選択し、 「追加」 をクリックします。
Event Notifications Android SDK のセットアップ
Android SDK は、Android アプリケーションがプッシュ通知を受信できるようにします。 以下のステップを実行して、Event Notifications Android SDK をインストールし、SDK を初期化し、Android アプリケーションの通知を登録します。
-
Gradle を使用して Event Notifications をインストールします。
compile 'com.ibm.cloud:eventnotifications-destination-android:0.0.1'
-
event-notifications-destination-android-sdk に従って SDK をインストールします。
-
Gradle がインストールされている場合は、SDK をインポートおよび初期化します。
import com.ibm.cloud.eventnotifications.destination.android.ENPush; String instanceGUID = "<instance_guid>>"; String destinationID = "<instance_destination_id>"; String apiKey = "<instance_apikey>"; ENPush enPush = ENPush.getInstance(); enPush.setCloudRegion(ENPush.REGION_US_SOUTH); // Set your region enPush.initialize(getApplicationContext(),instanceGUID,destinationID, apiKey);
-
SDK が初期化されると、プッシュ通知に登録します。
// Register the device to Event Notifications enPush.registerDeviceWithUserId("userId",new ENPushResponseListener<String>() { @Override public void onSuccess(String deviceId) { //handle successful device registration here } @Override public void onFailure(ENPushException ex) { //handle failure in device registration here } });
-
オプションで、プッシュタグの購読を作成することもできます subscribeToPushTag subscribe API は、特定のタグをデバイスにサブスクライブする。 デバイスが特定のタグにサブスクライブすると、デバイスはそのタグについて送信された通知を受け取ることができます。 以下のコード・スニペットをAndroidモバイル・アプリケーションに追加して、タグのリストを購読する。
// Subscribe to the given tag, if tagname is not available it will first get create then push device will get subscribe to it. enPush.subscribe(tagName, new ENPushResponseListener<String>() { @Override public void onSuccess(String arg) { System.out.println("Succesfully Subscribed to: "+ arg); } @Override public void onFailure(ENPushException ex) { System.out.println("Error subscribing to Tag1.." + ex.getMessage()); } });
-
アプリケーションで通知を受信するための通知リスナーを追加します。
//Handles the notification when it arrives ENPushNotificationListener notificationListener = new ENPushNotificationListener() { @Override public void onReceive (final ENSimplePushNotification message){ // Handle Push Notification } };
if(enPush != null) { enPush.listen(notificationListener); }
-
セットアップが完了したら、アプリケーションを実行し、プッシュ通知に登録します。
Android デバイスへの通知の送信
通知送信 API を使用して、Android デバイスのプッシュ通知を送信します。 API を直接呼び出す代わりに、ノードまたは実行管理 SDK を使用できます。


FCMレガシーHTTP APIからHTTPv1APIへの移行
FCMのレガシーHTTP APIを使用しているアプリは、このセクションの説明に従ってHTTPv1APIへの移行を検討してください。 HTTPv1APIには、セキュアなアクセストークン、効率的なカスタマイズ、将来性の証明、拡張可能なクライアント・プラットフォーム・バージョンなどの利点がある。
Event Notificationsで、FCM の基準に従って Android の宛先を作成できます。 レガシーHTTP APIについては、2つの必須パラメーター server_key
と sender_id
でサポートしてきた。 For the v1 HTTP API, FCM introduced three new parameters: project_id
, client_email
,
and private_key
. ここで観察すべき重要な点は、レガシーHTTP APIとv1HTTP APIの両方が相互に排他的であり、Event Notificationsでそれに対処しているということです。
古い構成から新しい構成にマイグレーションする方法を以下に示します。
-
既存の FCM Android 宛先構成の下に、
server_key
とsender_id
が用意されています。 ここでは、project_id
、client_email
、およびprivate_key
を指定して PATCH /destinations/{id} API 呼び出しを使用することで、既存の宛先を更新するだけで済みます。既存の Android 宛先構成:
{ "name": "Existing android destination", "description": "Android destination with legacy parameters", "type": "push_android", "config": { "params": { "sender_id": "xxxxxx", "server_key": "xxxxxx" } } }
新しい Android 宛先構成:
{ "name": "New android destination", "description": "Android destination with V1 HTTP parameters", "type": "push_android", "config": { "params": { "project_id": "xxxxx", "private_key": "xxxxx", "client_email": "abc@xyz.pqr" } } }
FCM ペイロードの変更の例
-
FCMレガシーHTTP API
{ "notification": { "title": "Incidunt qui porro sequi iste assumenda esse animi.", "body": "Minus reprehenderit ut nisi. Aut earum qui est iure eos fuga." }, "data": { "name": "Willie Greenholt", "description": "Voluptas voluptatem sed quia expedita error a at sit rerum. Numquam unde debitis incidunt qui impedit et necessitatibus. Cupiditate exercitationem enim ut laborum itaque et." } }
-
FCM HTTPv1API
{ "message": { "android": { "notification": { "title": "Incidunt qui porro sequi iste assumenda esse animi.", "body": "Minus reprehenderit ut nisi. Aut earum qui est iure eos fuga." }, "data": { "name": "Willie Greenholt", "description": "Voluptas voluptatem sed quia expedita error a at sit rerum. Numquam unde debitis incidunt qui impedit et necessitatibus. Cupiditate exercitationem enim ut laborum itaque et." } } } }