Migrace služby Google Firebase Cloud Messaging s využitím sad Azure SDK

Google do července 2024 přestane používat starší rozhraní API služby Firebase Cloud Messaging (FCM). 1. března 2024 můžete začít migrovat ze starší verze protokolu HTTP na FCM v1. Migraci musíte dokončit do června 2024. Tato část popisuje kroky migrace ze starší verze FCM na FCM v1 pomocí sad SDK Azure.

Důležité

Od června 2024 se starší rozhraní API FCM už nebudou podporovat a budou vyřazena. Abyste se vyhnuli přerušení služby nabízených oznámení, musíte co nejdříve migrovat na protokol FCM v1.

Požadavky

  • Ujistěte se, že je v nastavení projektu Firebase v části Cloud Messaging povolené rozhraní FIREBASE CLOUD Messaging API (V1).
  • Ujistěte se, že jsou aktualizované přihlašovací údaje FCM. Postupujte podle kroku 1 v průvodci rozhraním REST API.

Android SDK

  1. Aktualizujte verzi sady SDK do 2.0.0souboru build.gradle vaší aplikace. Příklad:

    // 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. Aktualizujte šablonu datové části. Pokud nepoužíváte šablony, můžete tento krok přeskočit.

    Viz referenční informace k FCM REST pro strukturu datové části FCM v1. Informace o migraci ze starší datové části FCM na datovou část FCM v1 najdete v tématu Aktualizace datové části odesílání požadavků.

    Pokud například používáte registrace:

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

    Pokud používáte instalace:

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

Serverové sady SDK (rovina dat)

  1. Aktualizujte balíček sady SDK na nejnovější verzi (4.2.0):

    Název GitHubu sady SDK Název balíčku SADY SDK Verze
    azure-notificationhubs-dotnet Microsoft.Azure.NotificationHubs 4.2.0
    azure-notificationhubs-java-back-end com.windowsazure.Notification-Hubs-java-sdk 1.1.0
    azure-sdk-for-js @azure/notification-hubs 1.1.0

    Například v souboru .csproj :

    <PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.2.0" />
    
  2. Přidejte ho FcmV1Credential do centra oznámení. Tento krok je jednorázovým nastavením. Pokud nemáte mnoho center a chcete tento krok automatizovat, můžete pomocí rozhraní REST API nebo webu Azure Portal přidat přihlašovací údaje 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. Správa registrací a instalací K registraci použijte FcmV1RegistrationDescription registraci zařízení FCM v1. Příklad:

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

    Pro Javu použijte FcmV1Registration k registraci zařízení FCMv1:

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

    Pro JavaScript použijte createFcmV1RegistrationDescription k registraci zařízení FCMv1:

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

    Pro instalace použijte NotificationPlatform.FcmV1 jako platformu s Installationinstalací nebo použijte FcmV1Installation k vytvoření instalací 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);
    

    Pro Javu použijte NotificationPlatform.FcmV1 jako platformu:

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

    Pro JavaScript použijte createFcmV1Installation k vytvoření instalace 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);
    

    Všimněte si následujících aspektů:

    • Pokud k registraci zařízení dojde v klientské aplikaci, nejprve aktualizujte klientskou aplikaci, aby se zaregistrovala na platformě FCMv1.
    • Pokud se registrace zařízení provede na serveru, můžete načíst všechny registrace/instalace a aktualizovat je na server na FCMv1.
  4. Odešlete oznámení FCMv1. Používá se FcmV1Notification při odesílání oznámení, která cílí na FCMv1. Příklad:

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

Další kroky

Migrace služby Firebase Cloud Messaging pomocí rozhraní REST API