Azure Functions における Notification Hubs の出力バインド

この記事では、Azure Functions で Azure Notification Hubs のバインドを使用してプッシュ通知を送信する方法について説明します。 Azure Functions は、Notification Hubs の出力バインドをサポートします。

Microsoft Azure Notification Hubs は、使用するプラットフォーム通知サービス (PNS) 用に構成する必要があります。 Notification Hubs からのプッシュ通知をクライアント アプリで取得するには、Notification Hubs の使用に関する記事を参照し、ページの上部にあるドロップダウン リストでターゲット クライアント プラットフォームを選択します。

重要

Google では Google Cloud Messaging (GCM) は非推奨となり、Firebase Cloud Messaging (FCM) が推奨されます。 この出力バインドは、FCM をサポートしていません。 FCM を使用して通知を送信するには、Firebase API を関数に直接使用するか、テンプレート通知を使用します。

パッケージ - Functions 1.x

Notification Hubs バインディングは Microsoft.Azure.WebJobs.Extensions.NotificationHubs NuGet パッケージ、バージョン 1.x で提供されます。 パッケージのソース コードは、azure-webjobs-sdk-extensions GitHub リポジトリにあります。

次の表に、各開発環境でこのバインディングのサポートを追加する方法を示します。

開発環境 サポートを追加するバージョン:
Functions 1.x
ローカル開発 - C# クラス ライブラリ パッケージをインストールする
ローカル開発で - C# スクリプト、JavaScript、F# 自動
Portal 開発 自動

パッケージ - Functions 2.x 以降

このバインディングは、Functions 2.x 以降では使用できません。

例 - テンプレート

送信できる通知は、ネイティブ通知またはテンプレート通知です。 ネイティブ通知は、出力バインドの platform プロパティで構成された特定のクライアント プラットフォームを対象としています。 テンプレート通知は、複数のプラットフォームを対象として使用できます。

言語固有の例をご覧ください。

C# スクリプト テンプレートの例 - out パラメーター

次の例では、テンプレートに message プレースホルダーを含むテンプレート登録の通知を送信します。

using System;
using System.Threading.Tasks;
using System.Collections.Generic;

public static void Run(string myQueueItem,  out IDictionary<string, string> notification, TraceWriter log)
{
    log.Info($"C# Queue trigger function processed: {myQueueItem}");
    notification = GetTemplateProperties(myQueueItem);
}

private static IDictionary<string, string> GetTemplateProperties(string message)
{
    Dictionary<string, string> templateProperties = new Dictionary<string, string>();
    templateProperties["message"] = message;
    return templateProperties;
}

C# スクリプト テンプレートの例 - 非同期

非同期コードを使用している場合は、out パラメーターを使用できません。 その場合は、IAsyncCollector を使用してテンプレート通知を返します。 次のコードでは、上記のコードの非同期の例を示します。

using System;
using System.Threading.Tasks;
using System.Collections.Generic;

public static async Task Run(string myQueueItem, IAsyncCollector<IDictionary<string,string>> notification, TraceWriter log)
{
    log.Info($"C# Queue trigger function processed: {myQueueItem}");

    log.Info($"Sending Template Notification to Notification Hub");
    await notification.AddAsync(GetTemplateProperties(myQueueItem));    
}

private static IDictionary<string, string> GetTemplateProperties(string message)
{
    Dictionary<string, string> templateProperties = new Dictionary<string, string>();
    templateProperties["user"] = "A new user wants to be added : " + message;
    return templateProperties;
}

C# スクリプト テンプレートの例 - JSON

次の例では、有効な JSON 文字列を使用して、テンプレートに message プレースホルダーを含むテンプレート登録の通知を送信します。

using System;

public static void Run(string myQueueItem,  out string notification, TraceWriter log)
{
    log.Info($"C# Queue trigger function processed: {myQueueItem}");
    notification = "{\"message\":\"Hello from C#. Processed a queue item!\"}";
}

C# スクリプト テンプレートの例 - ライブラリのタイプ

次の例では、Microsoft Azure Notification Hubs ライブラリに定義されたタイプの使用方法を示します。

#r "Microsoft.Azure.NotificationHubs"

using System;
using System.Threading.Tasks;
using Microsoft.Azure.NotificationHubs;

public static void Run(string myQueueItem,  out Notification notification, TraceWriter log)
{
   log.Info($"C# Queue trigger function processed: {myQueueItem}");
   notification = GetTemplateNotification(myQueueItem);
}

private static TemplateNotification GetTemplateNotification(string message)
{
    Dictionary<string, string> templateProperties = new Dictionary<string, string>();
    templateProperties["message"] = message;
    return new TemplateNotification(templateProperties);
}

F# テンプレートの例

次の例では、locationmessage を含むテンプレート登録の通知を送信します。

let Run(myTimer: TimerInfo, notification: byref<IDictionary<string, string>>) =
    notification = dict [("location", "Redmond"); ("message", "Hello from F#!")]

JavaScript テンプレートの例

次の例では、locationmessage を含むテンプレート登録の通知を送信します。

module.exports = async function (context, myTimer) {
    var timeStamp = new Date().toISOString();

    if (myTimer.IsPastDue)
    {
        context.log('Node.js is running late!');
    }
    context.log('Node.js timer trigger function ran!', timeStamp);  
    context.bindings.notification = {
        location: "Redmond",
        message: "Hello from Node!"
    };
};

例 - APNS ネイティブ

この C# スクリプトの例では、ネイティブ APNS 通知を送信する方法を示します。

#r "Microsoft.Azure.NotificationHubs"
#r "Newtonsoft.Json"

using System;
using Microsoft.Azure.NotificationHubs;
using Newtonsoft.Json;

public static async Task Run(string myQueueItem, IAsyncCollector<Notification> notification, TraceWriter log)
{
    log.Info($"C# Queue trigger function processed: {myQueueItem}");

    // In this example the queue item is a new user to be processed in the form of a JSON string with 
    // a "name" value.
    //
    // The JSON format for a native APNS notification is ...
    // { "aps": { "alert": "notification message" }}  

    log.LogInformation($"Sending APNS notification of a new user");    
    dynamic user = JsonConvert.DeserializeObject(myQueueItem);    
    string apnsNotificationPayload = "{\"aps\": {\"alert\": \"A new user wants to be added (" + 
                                        user.name + ")\" }}";
    log.LogInformation($"{apnsNotificationPayload}");
    await notification.AddAsync(new AppleNotification(apnsNotificationPayload));        
}

例 - WNS ネイティブ

この C# スクリプトの例では、Microsoft Azure Notification Hubs ライブラリに定義されたタイプを使用してネイティブの WNS トースト通知を送信する方法を示します。

#r "Microsoft.Azure.NotificationHubs"
#r "Newtonsoft.Json"

using System;
using Microsoft.Azure.NotificationHubs;
using Newtonsoft.Json;

public static async Task Run(string myQueueItem, IAsyncCollector<Notification> notification, TraceWriter log)
{
    log.Info($"C# Queue trigger function processed: {myQueueItem}");

    // In this example the queue item is a new user to be processed in the form of a JSON string with 
    // a "name" value.
    //
    // The XML format for a native WNS toast notification is ...
    // <?xml version="1.0" encoding="utf-8"?>
    // <toast>
    //      <visual>
    //     <binding template="ToastText01">
    //       <text id="1">notification message</text>
    //     </binding>
    //   </visual>
    // </toast>

    log.Info($"Sending WNS toast notification of a new user");    
    dynamic user = JsonConvert.DeserializeObject(myQueueItem);    
    string wnsNotificationPayload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                                    "<toast><visual><binding template=\"ToastText01\">" +
                                        "<text id=\"1\">" + 
                                            "A new user wants to be added (" + user.name + ")" + 
                                        "</text>" +
                                    "</binding></visual></toast>";

    log.Info($"{wnsNotificationPayload}");
    await notification.AddAsync(new WindowsNotification(wnsNotificationPayload));        
}

属性

C# クラス ライブラリでは、NotificationHub 属性を使用します。

この属性のコンストラクター パラメーターとプロパティについては、「構成」セクションをご覧ください。

構成

次の表は、function.json ファイルと NotificationHub 属性で設定したバインド構成のプロパティを説明しています。

function.json のプロパティ 属性のプロパティ 説明
type 該当なし notificationHub に設定する必要があります。
direction 該当なし out に設定する必要があります。
name 該当なし 通知ハブ メッセージの関数コードで使用される変数名。
tagExpression TagExpression タグ式。これにより、タグ式に一致する通知を受信するように登録した一連のデバイスに通知を配信するように指定できます。 詳細については、「ルーティングとタグ式」を参照してください。
hubName HubName Azure Portal 内の通知ハブ リソースの名前。
connection ConnectionStringSetting Notification Hubs 接続文字列を含むアプリ設定の名前。 この接続文字列は、使用している通知ハブの DefaultFullSharedAccessSignature 値に設定される必要があります。 この記事で後述する「接続文字列の設定」をご覧ください。
platform プラットフォーム platform プロパティは、通知の対象となるクライアント プラットフォームを示します。 既定では、プラットフォームのプロパティが出力バインドから省略されている場合、テンプレート通知は、Azure 通知ハブで構成されている任意のプラットフォームを対象として使用できます。 Azure Notification Hub でテンプレートを使用してクロスプラットフォームの通知を送信する一般的な方法の詳細については、「Templates (テンプレート)」を参照してください。 設定されている場合、platform には、次のいずれかの値を指定する必要があります。

ローカルで開発する場合は、 コレクション内の local.settings.json ファイルにアプリケーション設定を追加します。

function.json ファイルの例

次に示すのは、function.json ファイルでの Notification Hubs バインディングの例です。

{
  "bindings": [
    {
      "type": "notificationHub",
      "direction": "out",
      "name": "notification",
      "tagExpression": "",
      "hubName": "my-notification-hub",
      "connection": "MyHubConnectionString",
      "platform": "apns"
    }
  ],
  "disabled": false
}

接続文字列の設定

通知ハブの出力バインドを使用するには、ハブの接続文字列を設定する必要があります。 既存の通知ハブを選択するか、Azure ポータルの [統合] タブから新しいハブを作成できます。 接続文字列を手動で構成することもできます。

既存の通知ハブに対する接続文字列を構成するには:

  1. Azure Portal で通知ハブに移動し、[アクセスポリシー] を選択して、[DefaultFullSharedAccessSignature] ポリシーの横にあるコピー ボタンを選択します。 これにより、DefaultFullSharedAccessSignature ポリシーの接続文字列が通知ハブにコピーされます。 この接続文字列を使用して、関数からハブに通知メッセージを送信できます。 通知ハブの接続文字列をコピーする
  2. Azure portal の関数アプリに移動し、[アプリケーション設定] を選択し、MyHubConnectionString などのキーを追加します。次に、通知ハブ用にコピーされた DefaultFullSharedAccessSignature を値として貼り付けて、[保存] をクリックします。

このアプリケーション設定の名前が、function.json または .NET 属性の出力バインディング接続設定で使用されます。 この記事で前述した「構成」セクションをご覧ください。

ローカルで開発する場合は、 コレクション内の local.settings.json ファイルにアプリケーション設定を追加します。

例外とリターン コード

バインド リファレンス
Notification Hub 運用ガイド

次のステップ