Azure SDK を使った Google Firebase Cloud Messaging の移行

Google は、Firebase Cloud Messaging (FCM) レガシ API を 2024 年 7 月までに廃止する予定です。 レガシ HTTP プロトコルから FCM v1 への移行は 2024 年 3 月 1 日から開始できます。 移行は 2024 年 6 月までに完了する必要があります。 このセクションでは、Azure SDK を使って FCM レガシから FCM v1 に移行する手順について説明します。

重要

2024 年 6 月の時点で、FCM レガシ API はサポートされなくなり、廃止される予定です。 プッシュ通知サービスの中断を回避するには、できるだけ早く FCM v1 プロトコルに移行する必要があります。

前提条件

  • Firebase Cloud Messaging API (V1)[クラウド メッセージング] 下の Firebase プロジェクト設定で有効になっていることを確認します。
  • FCM 資格情報が更新されていることを確認します。 REST API ガイドの手順 1 に従います

Android SDK

  1. アプリケーションの build.gradle ファイルで SDK バージョンを 2.0.0 に更新します。 次に例を示します。

    // This is not a complete build.gradle file; it only highlights the portions you need to update. 
    
    dependencies { 
        // Ensure the following line is updated in your app/library's "dependencies" section.
    implementation 'com.microsoft.azure:notification-hubs-android-sdk:2.0.0' 
        // optionally, use the fcm optimized SKU instead: 
        // implementation 'com.microsoft.azure:notification-hubs-android-sdk-fcm:2.0.0' 
    }
    
  2. ペイロード テンプレートを更新します。 テンプレートを使っていない場合は、この手順をスキップできます。

    FCM v1 ペイロード構造については、FCM REST リファレンスを参照してください。 FCM レガシ ペイロードから FCM v1 ペイロードへの移行については、「送信リクエストのペイロードを更新する」を参照してください。

    たとえば、登録を使っている場合:

    NotificationHub hub = new NotificationHub(BuildConfig.hubName, BuildConfig.hubListenConnectionString, context);
    String template = "{\"message\":{\"android\":{\"data\":{\"message\":\"{'Notification Hub test notification: ' + $(myTextProp)}\"}}}}";
    hub.registerTemplate(token, "template-name", template);
    

    インストールを使っている場合:

    InstallationTemplate testTemplate = new InstallationTemplate(); 
    testTemplate.setBody("{\"message\":{\"android\":{\"data\":{\"message\":\"{'Notification Hub test notification: ' + $(myTextProp)}\"}}}}");  
    NotificationHub.setTemplate("testTemplate", testTemplate);
    

サーバー SDK (データ プレーン)

  1. SDK パッケージを最新バージョン (4.2.0) に更新します。

    SDK GitHub 名 SDK パッケージ名 バージョン
    azure-notificationhubs-dotnet Microsoft.Azure.NotificationHubs 4.2.0
    azure-notificationhubs-java-backend com.windowsazure.Notification-Hubs-java-sdk 1.1.0
    azure-sdk-for-js @azure/notification-hubs 1.1.0

    たとえば、.csproj ファイルの場合:

    <PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.2.0" />
    
  2. FcmV1Credential を通知ハブに追加します。 この手順は、1 回限りのセットアップです。 多くのハブがあり、この手順を自動化したい場合を除き、REST API または Azure portal を使って FCM v1 資格情報を追加できます。

    // Create new notification hub with FCM v1 credentials
    var hub = new NotificationHubDescription("hubname"); 
    hub.FcmV1Credential = new FcmV1Credential("private-key", "project-id", "client-email"); 
    hub = await namespaceManager.CreateNotificationHubAsync(hub); 
    
    // Update existing notification hub with FCM v1 credentials 
    var hub = await namespaceManager.GetNotificationHubAsync("hubname", CancellationToken.None); 
    hub.FcmV1Credential = new FcmV1Credential("private-key", "project-id", "client-email"); 
    hub = await namespaceManager.UpdateNotificationHubAsync(hub, CancellationToken.None);
    
    // Create new notification hub with FCM V1 credentials
    NamespaceManager namespaceManager = new NamespaceManager(namespaceConnectionString);
    NotificationHubDescription hub = new NotificationHubDescription("hubname");
    hub.setFcmV1Credential(new FcmV1Credential("private-key", "project-id", "client-email"));
    hub = namespaceManager.createNotificationHub(hub);
    
    // Updating existing Notification Hub with FCM V1 Credentials
    NotificationHubDescription hub = namespaceManager.getNotificationHub("hubname");
    hub.setFcmV1Credential(new FcmV1Credential("private-key", "project-id", "client-email"));
    hub = namespaceManager.updateNotificationHub(hub);
    
  3. 登録とインストールを管理します。 登録の場合は、FcmV1RegistrationDescription を使って FCM v1 デバイスを登録します。 次に例を示します。

    // Create new Registration
    var deviceToken = "device-token"; 
    var tags = new HashSet<string> { "tag1", "tag2" }; 
    FcmV1RegistrationDescription registration = await hub. CreateFcmV1NativeRegistrationAsync(deviceToken, tags);
    

    Java の場合、FcmV1Registration を使って FCMv1 デバイスを登録します。

    // Create new registration
    NotificationHub client = new NotificationHub(connectionString, hubName);
    FcmV1Registration registration =  client.createRegistration(new FcmV1Registration("fcm-device-token"));
    

    JavaScript の場合、createFcmV1RegistrationDescription を使って FCMv1 デバイスを登録します。

    // Create FCM V1 registration
    const context = createClientContext(connectionString, hubName);
    const registration = createFcmV1RegistrationDescription({
      fcmV1RegistrationId: "device-token",
    });
    const registrationResponse = await createRegistration(context, registration);
    

    インストールの場合は、NotificationPlatform.FcmV1Installation のプラットフォームとして使うか、FcmV1Installation を使って FCM v1 のインストールを作成します。

    // Create new installation
    var installation = new Installation 
    { 
        InstallationId = "installation-id", 
        PushChannel = "device-token", 
        Platform = NotificationPlatform.FcmV1 
    }; 
    await hubClient.CreateOrUpdateInstallationAsync(installation); 
    
    // Alternatively, you can use the FcmV1Installation class directly 
    var installation = new FcmV1Installation("installation-id", "device-token"); 
    await hubClient.CreateOrUpdateInstallationAsync(installation);
    

    Java の場合、プラットフォームとして NotificationPlatform.FcmV1 を使います。

    // Create new installation
    NotificationHub client = new NotificationHub(connectionString, hubName);
    client.createOrUpdateInstallation(new Installation("installation-id", NotificationPlatform.FcmV1, "device-token"));
    

    JavaScript の場合、createFcmV1Installation を使って FCMv1 のインストールを作成します。

    // Create FCM V1 installation
    const context = createClientContext(connectionString, hubName);
    const installation = createFcmV1Installation({
      installationId: "installation-id",
      pushChannel: "device-token",
    });
    const result = await createOrUpdateInstallation(context, installation);
    

    次の考慮事項に注意してください。

    • デバイスの登録がクライアント アプリ上で行われる場合は、まずクライアント アプリを更新して、FCMv1 プラットフォームに登録します。
    • デバイスの登録がサーバー上で行われる場合、すべての登録とインストールをフェッチし、サーバー上でそれらを FCMv1 に更新できます。
  4. 通知を FCMv1 に送信します。 FCMv1 をターゲットとする通知を送信する場合は、FcmV1Notification を使います。 次に例を示します。

    // Send FCM v1 notification
    var jsonBody = "{\"message\":{\"android\":{\"data\":{\"message\":\"Notification Hub test notification\"}}}}"; 
    var n = new FcmV1Notification(jsonBody); 
    NotificationOutcome outcome = await hub.SendNotificationAsync(n, "tag");
    
    // Send FCM V1 Notification 
    NotificationHub client = new NotificationHub(connectionString, hubName);
    NotificationOutcome outcome = client.sendNotification(new FcmV1Notification("{\"message\":{\"android\":{\"data\":{\"message\":\"Notification Hub test notification\"}}}}"));
    
    // Send FCM V1 Notification
    const context = createClientContext(connectionString, hubName);
    const messageBody = `{
      "message": {
          "android": {
              "data": {
                  "message": "Notification Hub test notification"
              }
          }
      }
    }`;
    
    const notification = createFcmV1Notification({
      body: messageBody,
    });
    const result = await sendNotification(context, notification);
    

次のステップ

REST API を使った Firebase Cloud Messaging の移行