Migration de Google Firebase Cloud Messaging avec les SDK Azure

Google va déprécier l’API héritée Firebase Cloud Messaging (FCM) en juillet 2024. Vous pourrez commencer la migration de l’ancien protocole HTTP vers FCM v1 le 1er mars 2024. Vous devez terminer la migration d’ici juin 2024. Cette section décrit les étapes à suivre pour migrer de l’API FCM héritée vers FCM v1 avec les SDK Azure.

Prérequis

  • Vérifiez que l’API Firebase Cloud Messaging (V1) est activée dans le paramètre de projet Firebase sous Messagerie cloud.
  • Vérifiez que les informations d’identification du FCM ont été mises à jour. Suivez l’étape 1 du guide de l’API REST.

Android SDK

  1. Mettez à jour la version du SDK vers 2.0.0 dans le fichier build.gradle de votre application. Par exemple :

    // 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. Mettez à jour le modèle de charge utile. Si vous n’utilisez pas de modèles, vous pouvez ignorer cette étape.

    Consultez les informations de référence sur REST FCM pour connaître la structure de charge utile de FCM v1. Pour plus d’informations sur la migration de la charge utile héritée FCM vers la charge utile FCM v1, consultez Mettre à jour la charge utile des demandes d’envoi.

    Par exemple, si vous utilisez des inscriptions :

    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);
    

    Si vous utilisez des installations :

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

SDK serveur (plan de données)

  1. Mettez à jour package du SDK vers la dernière version (4.2.0) :

    Nom du SDK GitHub Nom du package SDK Version
    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

    Par exemple, dans le fichier .csproj :

    <PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.2.0" />
    
  2. Ajoutez FcmV1Credential au hub de notification. Il s’agit d’une configuration unique. À moins que vous ayez de nombreux hubs et que vous vouliez automatiser cette étape, vous pouvez utiliser l’API REST ou le portail Azure pour ajouter les informations d’identification 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. Gérez les inscriptions et les installations. Pour les inscriptions, utilisez FcmV1RegistrationDescription pour inscrire des appareils FCM v1. Par exemple :

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

    Pour Java, utilisez FcmV1Registration pour inscrire des appareils FCMv1 :

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

    Pour JavaScript, utilisez createFcmV1RegistrationDescription pour inscrire des appareils FCMv1 :

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

    Pour les installations, utilisez NotificationPlatform.FcmV1 comme plateforme avec Installation, ou utilisez FcmV1Installation pour créer des installations 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);
    

    Pour Java, utilisez NotificationPlatform.FcmV1 comme plateforme :

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

    Pour JavaScript, utilisez createFcmV1Installation pour créer une installation 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);
    

    Tenez compte des points suivants :

    • Si l’inscription de l’appareil se produit sur l’application cliente, mettez d’abord à jour l’application cliente pour faire l’inscription sous la plateforme FCMv1.
    • Si l’inscription de l’appareil se produit sur le serveur, vous pouvez récupérer toutes les inscriptions/installations et les mettre à jour vers FCMv1 sur le serveur.
  4. Envoyez la notification à FCMv1. Utilisez FcmV1Notification quand vous envoyez des notifications ciblant FCMv1. Par exemple :

    // 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);
    

Étapes suivantes

Migration de Firebase Cloud Messaging avec l’API REST