Bagikan melalui


Migrasi Google Firebase Cloud Messaging menggunakan Azure SDK

Google akan menghentikan API warisan Firebase Cloud Messaging (FCM) pada Juli 2024. Anda dapat mulai bermigrasi dari protokol HTTP warisan ke FCM v1 pada 1 Maret 2024. Anda harus menyelesaikan migrasi pada Juni 2024. Bagian ini menjelaskan langkah-langkah untuk bermigrasi dari warisan FCM ke FCM v1 menggunakan Azure SDK.

Penting

Pada Juni 2024, API warisan FCM tidak akan lagi didukung dan akan dihentikan. Untuk menghindari gangguan dalam layanan pemberitahuan push, Anda harus bermigrasi ke protokol FCM v1 sesegera mungkin.

Prasyarat

  • Pastikan Firebase Cloud Messaging API (V1) diaktifkan dalam pengaturan proyek Firebase di bawah Cloud Messaging.
  • Pastikan kredensial FCM diperbarui. Ikuti langkah 1 dalam panduan REST API.

Android SDK

  1. Perbarui versi SDK ke 2.0.0 dalam file build.gradle aplikasi Anda. Contohnya:

    // 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. Perbarui templat payload. Jika Anda tidak menggunakan templat, Anda dapat melewati langkah ini.

    Lihat referensi FCM REST untuk struktur payload FCM v1. Untuk informasi tentang migrasi dari payload warisan FCM ke payload FCM v1, lihat Memperbarui payload permintaan pengiriman.

    Misalnya, jika Anda menggunakan pendaftaran:

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

    Jika Anda menggunakan penginstalan:

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

SDK Server (Data Plane)

  1. Perbarui paket SDK ke versi terbaru (4.2.0):

    Nama GitHub SDK Nama paket SDK Versi
    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

    Misalnya, dalam file .csproj :

    <PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.2.0" />
    
  2. Tambahkan ke FcmV1Credential hub pemberitahuan. Langkah ini adalah pengaturan satu kali. Kecuali Anda memiliki banyak hub, dan ingin mengotomatiskan langkah ini, Anda dapat menggunakan REST API atau portal Azure untuk menambahkan kredensial 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. Mengelola pendaftaran dan penginstalan. Untuk pendaftaran, gunakan FcmV1RegistrationDescription untuk mendaftarkan perangkat FCM v1. Contohnya:

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

    Untuk Java, gunakan FcmV1Registration untuk mendaftarkan perangkat FCMv1:

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

    Untuk JavaScript, gunakan createFcmV1RegistrationDescription untuk mendaftarkan perangkat FCMv1:

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

    Untuk penginstalan, gunakan NotificationPlatform.FcmV1 sebagai platform dengan Installation, atau gunakan FcmV1Installation untuk membuat penginstalan 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);
    

    Untuk Java, gunakan NotificationPlatform.FcmV1 sebagai platform:

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

    Untuk JavaScript, gunakan createFcmV1Installation untuk membuat penginstalan 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);
    

    Perhatikan pertimbangan berikut:

    • Jika pendaftaran perangkat terjadi pada aplikasi klien, perbarui aplikasi klien terlebih dahulu untuk mendaftar di bawah platform FCMv1.
    • Jika pendaftaran perangkat terjadi di server, Anda dapat mengambil semua pendaftaran/penginstalan dan memperbaruinya ke FCMv1 di server.
  4. Kirim pemberitahuan ke FCMv1. Gunakan FcmV1Notification saat Anda mengirim pemberitahuan yang menargetkan FCMv1. Contohnya:

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

Langkah berikutnya

Migrasi Firebase Cloud Messaging menggunakan REST API