Windows 8.1 Universal および Windows 10 UWP でのプッシュ通知の処理

improve this page | report issue

概説

MobileFirst が提供する通知 API を使用して、デバイスの登録や登録抹消、タグへのサブスクライブやアンサブスクライブを実行できます。 このチュートリアルでは、C# を使用して、ネイティブの Windows 8.1 Universal アプリケーションおよび Windows 10 UWP アプリケーションでプッシュ通知を処理する方法について学習します。

前提条件

ジャンプ先:

通知構成

新しい Visual Studio プロジェクトを作成するか、または既存のプロジェクトを使用します。
MobileFirst Native Windows SDK がプロジェクトにまだ存在しない場合は、Windows アプリケーションへの MobileFirst SDK の追加チュートリアルの説明に従ってください。

プッシュ SDK の追加

  1. 「ツール」→「NuGet パッケージ マネージャー」→「パッケージ マネージャー コンソール」を選択します。
  2. MobileFirst プッシュ・コンポーネントをインストールするプロジェクトを選択します。
  3. Install-Package IBM.MobileFirstPlatformFoundationPush コマンドを実行して、MobileFirst プッシュ SDK を追加します。

WNS 構成の前提条件

  1. アプリケーションにトースト通知機能が備わっていることを確認します。 これは Package.appxmanifest 内で有効にできます。
  2. Package Identity NamePublisher が、WNS に登録されている値で更新されている必要があります。
  3. (オプション) TemporaryKey.pfx ファイルを削除します。

通知 API

MFPPush インスタンス

すべての API 呼び出しは、MFPPush のインスタンスから呼び出される必要があります。 これを行うには、変数 (private MFPPush PushClient = MFPPush.GetInstance(); など) を作成し、その後、クラス内で一貫して PushClient.methodName() を呼び出します。

代わりに、プッシュ API メソッドにアクセスする必要があるインスタンスごとに MFPPush.GetInstance().methodName() を呼び出すこともできます。

チャレンジ・ハンドラー

push.mobileclient スコープがセキュリティー検査にマップされる場合、プッシュ API を使用する前に、一致するチャレンジ・ハンドラーが存在し、登録済みであることを確認する必要があります。

チャレンジ・ハンドラーについて詳しくは、資格情報の検証チュートリアルを参照してください。

クライアント・サイド

C Sharp メソッド 説明
Initialize() 提供されたコンテキストの MFPPush を初期化します。
IsPushSupported() デバイスがプッシュ通知をサポートするかどうか。
RegisterDevice(JObject options) デバイスをプッシュ通知サービスに登録します。
GetTags() プッシュ通知サービス・インスタンス内で使用可能なタグを取得します。
Subscribe(String[] Tags) 指定されたタグにデバイスをサブスクライブします。
GetSubscriptions() デバイスが現在サブスクライブしているタグをすべて取得します。
Unsubscribe(String[] Tags) 特定のタグからアンサブスクライブします。
UnregisterDevice() プッシュ通知サービスからデバイスを登録抹消します。

初期化

初期化は、クライアント・アプリケーションが MFPPush サービスに接続するために必要です。

  • 最初に Initialize メソッドを呼び出してから、その他の MFPPush API を使用する必要があります。
  • このメソッドは、受け取ったプッシュ通知を処理するコールバック関数を登録します。
MFPPush.GetInstance().Initialize();

プッシュがサポートされるか

デバイスがプッシュ通知をサポートするかどうかをチェックします。

Boolean isSupported = MFPPush.GetInstance().IsPushSupported();

if (isSupported ) {
    // Push is supported
} else {
    // Push is not supported
}

デバイスの登録 & デバイス・トークンの送信

デバイスをプッシュ通知サービスに登録します。

JObject Options = new JObject();
MFPPushMessageResponse Response = await MFPPush.GetInstance().RegisterDevice(Options);
if (Response.Success == true)
{
    // Successfully registered
    } else {
    // Registration failed with error
    }

タグの取得

プッシュ通知サービスからすべての使用可能なタグを取得します。

MFPPushMessageResponse Response = await MFPPush.GetInstance().GetTags();
if (Response.Success == true)
{
    Message = new MessageDialog("Avalibale Tags: " + Response.ResponseJSON["tagNames"]);
} else{
    Message = new MessageDialog("Failed to get Tags list");
}

サブスクライブ

目的のタグにサブスクライブします。

string[] Tags = ["Tag1" , "Tag2"];

// Get subscription tag
MFPPushMessageResponse Response = await MFPPush.GetInstance().Subscribe(Tags);
if (Response.Success == true)
{
    //successfully subscribed to push tag
}
else
{
    //failed to subscribe to push tags
}

サブスクリプションの取得

デバイスが現在サブスクライブしているタグを取得します。

MFPPushMessageResponse Response = await MFPPush.GetInstance().GetSubscriptions();
if (Response.Success == true)
{
    Message = new MessageDialog("Avalibale Tags: " + Response.ResponseJSON["tagNames"]);
}
else
{
    Message = new MessageDialog("Failed to get subcription list...");
}

アンサブスクライブ

タグからアンサブスクライブします。

string[] Tags = ["Tag1" , "Tag2"];

// unsubscribe tag
MFPPushMessageResponse Response = await MFPPush.GetInstance().Unsubscribe(Tags);
if (Response.Success == true)
{
    //succes
}
else
{
    //failed to subscribe to tags
}

登録抹消

プッシュ通知サービス・インスタンスからデバイスを登録抹消します。

MFPPushMessageResponse Response = await MFPPush.GetInstance().UnregisterDevice();
if (Response.Success == true)
{
    // Successfully registered
    } else {
    // Registration failed with error
    }

プッシュ通知の処理

プッシュ通知を処理するためには、MFPPushNotificationListener をセットアップする必要があります。 これは、以下のメソッドを実装することで実現できます。

  1. MFPPushNotificationListener タイプのインターフェースを使用してクラスを作成します。

    internal class NotificationListner : MFPPushNotificationListener
    {
         public async void onReceive(String properties, String payload)
    {
         // Handle push notification here      
    }
    }
    
  2. MFPPush.GetInstance().listen(new NotificationListner()) を呼び出すことで、このクラスがリスナーになるように設定します。
  3. onReceive メソッド内でプッシュ通知を受け取り、目的の動作にあわせて通知を処理できます。

サンプル・アプリケーションのイメージ

Windows Universal Push Notifications Service

サーバー構成内で特定のポートをオープンする必要はありません。

WNS では通常の http 要求または https 要求が使用されます。

サンプル・アプリケーション

ここをクリック して、Windows 8.1 Universal プロジェクトをダウンロードします。
ここをクリック して、Windows 10 UWP プロジェクトをダウンロードします。

サンプルの使用法

サンプルの README.md ファイルの指示に従ってください。

Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on October 05, 2018