Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
Google v červenci 2024 zastaralá rozhraní API firebase Cloud Messaging (FCM). Tato část popisuje kroky migrace ze starší verze FCM na FCM v1 pomocí sad SDK Azure.
Důležité
Od června 2024 jsou starší rozhraní API FCM podporována a jsou 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 Messagingpovolené 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
Aktualizujte verzi sady SDK do
2.0.0souboru build.gradle vaší aplikace. Napří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' }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.
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 (datový plán)
Aktualizujte balíček sady SDK na nejnovější verzi (4.2.0):
Název sady SDK na GitHubu Název balíčku 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" />Přidejte
FcmV1Credentialdo 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);Správa registrací a instalací K registraci použijte
FcmV1RegistrationDescriptionpro registraci zařízení FCM v1. Napří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
FcmV1Registrationk 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
createFcmV1RegistrationDescriptionk 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.FcmV1jako platformu sInstallationinstalací nebo použijteFcmV1Installationk 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.FcmV1jako platformu:// Create new installation NotificationHub client = new NotificationHub(connectionString, hubName); client.createOrUpdateInstallation(new Installation("installation-id", NotificationPlatform.FcmV1, "device-token"));Pro JavaScript použijte
createFcmV1Installationk 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.
Odešlete oznámení FCMv1. Používá se
FcmV1Notificationpři odesílání oznámení, která cílí na FCMv1. Napří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