Wenn Sie eine Nachricht an mehrere Geräte senden möchten, verwenden Sie Topic Messaging. Mit dieser Funktion können Sie eine Nachricht an mehrere Geräte senden, die ein bestimmtes Thema abonniert haben.
In diesem Tutorial geht es darum, Themennachrichten von Ihrem App-Server mit der Admin SDK oder der REST API für FCM zu senden und sie in einer Apple-App zu empfangen und zu verarbeiten. Auf dieser Seite sind alle Schritte aufgeführt, die dafür erforderlich sind, von der Einrichtung bis zur Bestätigung. Wenn Sie bereits eine Apple-Client-App für FCM eingerichtet oder die Schritte zum Senden Ihrer ersten Nachricht durchlaufen haben, haben Sie möglicherweise einige dieser Schritte bereits ausgeführt.
Firebase zu Ihrem Apple-Projekt hinzufügen
In diesem Abschnitt werden Aufgaben beschrieben, die Sie möglicherweise bereits erledigt haben, wenn Sie andere Firebase-Funktionen für Ihre App aktiviert haben. Für FCM müssen Sie Ihren APNs-Authentifizierungsschlüssel hochladen und sich für Remote-Benachrichtigungen registrieren.
Vorbereitung
Installieren Sie Folgendes:
- Xcode 16.2 oder höher
Ihr Projekt muss die folgenden Anforderungen erfüllen:
- Ihr Projekt muss auf diese Plattformversionen oder höher ausgerichtet sein:
- iOS 13
- macOS 10.15
- tvOS 13
- watchOS 7
- Ihr Projekt muss auf diese Plattformversionen oder höher ausgerichtet sein:
Richten Sie ein physisches Apple-Gerät ein, auf dem Ihre App ausgeführt werden kann, und führen Sie die folgenden Aufgaben aus:
- Rufen Sie einen Apple Push Notification-Authentifizierungsschlüssel für Ihr Apple-Entwicklerkonto ab.
- Aktivieren Sie Push-Benachrichtigungen in Xcode unter App > Capabilities.
Wenn Sie noch kein Xcode-Projekt haben und nur ein Firebase-Produkt ausprobieren möchten, können Sie eines unserer Schnellstartbeispiele herunterladen.
Firebase-Projekt erstellen
Bevor Sie Firebase zu Ihrer Apple-App hinzufügen können, müssen Sie ein Firebase-Projekt erstellen, um eine Verbindung zu Ihrer App herzustellen. Weitere Informationen zu Firebase-Projekten finden Sie unter Firebase-Projekte.
App bei Firebase registrieren
Wenn Sie Firebase in Ihrer Apple-App verwenden möchten, müssen Sie Ihre App in Ihrem Firebase-Projekt registrieren. Die Registrierung Ihrer App wird oft auch als „Hinzufügen“ Ihrer App zu Ihrem Projekt bezeichnet.
Rufen Sie die Firebase-Konsole auf.
Klicken Sie in der Mitte der Projektübersicht auf das Symbol iOS+, um den Einrichtungsworkflow zu starten.
Wenn Sie Ihrem Firebase-Projekt bereits eine App hinzugefügt haben, klicken Sie auf App hinzufügen, um die Plattformoptionen aufzurufen.
Geben Sie die Bundle-ID Ihrer App in das Feld Bundle-ID ein.
Optional: Geben Sie weitere App-Informationen ein: App-Alias und App Store-ID.
Klicken Sie auf App registrieren.
Firebase-Konfigurationsdatei hinzufügen
Klicken Sie auf GoogleService-Info.plist herunterladen, um die Firebase-Konfigurationsdatei (
GoogleService-Info.plist
) Ihrer App zu erhalten.Verschieben Sie die Konfigurationsdatei in das Stammverzeichnis Ihres Xcode-Projekts. Wenn Sie dazu aufgefordert werden, wählen Sie aus, dass die Konfigurationsdatei allen Zielen hinzugefügt werden soll.
Wenn Sie mehrere Bundle-IDs in Ihrem Projekt haben, müssen Sie jede Bundle-ID mit einer registrierten App in der Firebase-Konsole verknüpfen, damit jede App eine eigene GoogleService-Info.plist
-Datei haben kann.
Firebase SDKs zu Ihrer App hinzufügen
Verwenden Sie Swift Package Manager, um Firebase-Abhängigkeiten zu installieren und zu verwalten.
- Öffnen Sie Ihr App-Projekt und gehen Sie in Xcode zu File > Add Packages (Datei > Pakete hinzufügen).
- Fügen Sie bei entsprechender Aufforderung das Firebase Apple Platforms SDK-Repository hinzu:
- Wählen Sie die Firebase Cloud Messaging-Bibliothek aus.
- Fügen Sie das Flag
-ObjC
dem Abschnitt Other Linker Flags (Weitere Linker-Flags) der Build-Einstellungen Ihres Ziels hinzu. - Für eine optimale Nutzung von Firebase Cloud Messaging empfehlen wir, Google Analytics in Ihrem Firebase-Projekt zu aktivieren und das Firebase SDK für Google Analytics in Ihre App einzubinden. Sie können entweder die Bibliothek ohne IDFA-Erfassung oder mit IDFA-Erfassung auswählen. Häufig gestellte Fragen zur aktuellen Organisation von Modulen im Google Analytics für das Firebase SDK
- Wenn Sie fertig, beginnt Xcode automatisch, Ihre Abhängigkeiten im Hintergrund aufzulösen und herunterzuladen.
https://github.com/firebase/firebase-ios-sdk.git
APNs-Authentifizierungsschlüssel hochladen
Laden Sie Ihren APNs-Authentifizierungsschlüssel in Firebase hoch. Wenn Sie noch keinen APNs-Authentifizierungsschlüssel haben, müssen Sie einen im Apple Developer Member Center erstellen.
-
Wählen Sie in Ihrem Projekt in der Firebase-Konsole das Zahnradsymbol aus, dann Projekteinstellungen und schließlich den Tab Cloud Messaging.
-
Klicken Sie unter Konfiguration der iOS-App bei APNs-Authentifizierungsschlüssel auf die Schaltfläche Hochladen.
-
Suchen Sie nach dem Speicherort, an dem Sie den Schlüssel gespeichert haben, wählen Sie ihn aus und klicken Sie auf Öffnen. Fügen Sie die Schlüssel-ID für den Schlüssel hinzu (verfügbar im Apple Developer Member Center) und klicken Sie auf Hochladen.
Firebase in Ihrer App initialisieren
Sie müssen Ihrer Anwendung Firebase-Initialisierungscode hinzufügen. Importieren Sie das Firebase-Modul und konfigurieren Sie eine gemeinsam genutzte Instanz wie unten dargestellt:
- Importieren Sie das
FirebaseCore
-Modul in IhreUIApplicationDelegate
sowie alle anderen Firebase-Module, die von Ihrem App-Delegaten verwendet werden. So verwenden Sie beispielsweise Cloud Firestore und Authentication:SwiftUI
import SwiftUI import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Swift
import FirebaseCore import FirebaseFirestore import FirebaseAuth // ...
Objective-C
@import FirebaseCore; @import FirebaseFirestore; @import FirebaseAuth; // ...
- Konfigurieren Sie eine freigegebene
FirebaseApp
-Instanz in der Methodeapplication(_:didFinishLaunchingWithOptions:)
des App-Delegaten:SwiftUI
// Use Firebase library to configure APIs FirebaseApp.configure()
Swift
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- Wenn Sie SwiftUI verwenden, müssen Sie ein Anwendungs-Delegate erstellen und es über
UIApplicationDelegateAdaptor
oderNSApplicationDelegateAdaptor
an IhreApp
-Struktur anhängen. Außerdem müssen Sie das Swizzling von App-Delegaten deaktivieren. Weitere Informationen finden Sie in der SwiftUI-Anleitung.SwiftUI
@main struct YourApp: App { // register app delegate for Firebase setup @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate var body: some Scene { WindowGroup { NavigationView { ContentView() } } } }
Für Remote-Benachrichtigungen registrieren
Registrieren Sie Ihre App entweder beim Start oder an der gewünschten Stelle im Anwendungsablauf für Remote-Benachrichtigungen. Rufen SieregisterForRemoteNotifications
wie folgt auf:
Swift
UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: { _, _ in } ) application.registerForRemoteNotifications()
Objective-C
[UNUserNotificationCenter currentNotificationCenter].delegate = self; UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge; [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { // ... }]; [application registerForRemoteNotifications];
Client-App für ein Thema abonnieren
Client-Apps können jedes vorhandene Thema abonnieren oder ein neues Thema erstellen. Wenn eine Client-App ein neues Thema abonniert, das noch nicht für Ihr Firebase-Projekt vorhanden ist, wird in FCM ein neues Thema mit diesem Namen erstellt. Jeder Client kann es dann abonnieren.
Wenn Sie ein Thema abonnieren möchten, rufen Sie die Abo-Methode über den Hauptthread Ihrer Anwendung auf (FCM ist nicht threadsicher). Wenn die Aboanfrage anfangs fehlschlägt, wird sie von FCM automatisch wiederholt. Wenn das Abo nicht abgeschlossen werden kann, wird ein Fehler ausgegeben, den Sie in einem Completion-Handler abfangen können, wie unten gezeigt:
Swift
Messaging.messaging().subscribe(toTopic: "weather") { error in print("Subscribed to weather topic") }
Objective-C
[[FIRMessaging messaging] subscribeToTopic:@"weather" completion:^(NSError * _Nullable error) { NSLog(@"Subscribed to weather topic"); }];
Mit diesem Aufruf wird eine asynchrone Anfrage an das FCM-Backend gesendet und der Client wird für das angegebene Thema registriert. Bevor Sie subscribeToTopic:topic
aufrufen, muss die Client-App-Instanz bereits ein Registrierungstoken über den Callback didReceiveRegistrationToken
erhalten haben.
Bei jedem Start der App prüft FCM, ob alle angeforderten Themen abonniert wurden. Wenn Sie sich abmelden möchten, rufen Sie unsubscribeFromTopic:topic
an. FCM meldet sich dann im Hintergrund vom Thema ab.
Themennachrichten empfangen und bearbeiten
FCM stellt Themennachrichten auf dieselbe Weise wie andere Downstream-Nachrichten zu.
Implementieren Sie application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
wie gezeigt:
Swift
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) async -> UIBackgroundFetchResult { // If you are receiving a notification message while your app is in the background, // this callback will not be fired till the user taps on the notification launching the application. // TODO: Handle data of notification // With swizzling disabled you must let Messaging know about the message, for Analytics // Messaging.messaging().appDidReceiveMessage(userInfo) // Print message ID. if let messageID = userInfo[gcmMessageIDKey] { print("Message ID: \(messageID)") } // Print full message. print(userInfo) return UIBackgroundFetchResult.newData }
Objective-C
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // If you are receiving a notification message while your app is in the background, // this callback will not be fired till the user taps on the notification launching the application. // TODO: Handle data of notification // With swizzling disabled you must let Messaging know about the message, for Analytics // [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; // ... // Print full message. NSLog(@"%@", userInfo); completionHandler(UIBackgroundFetchResultNewData); }
Sendeanfragen erstellen
Nachdem Sie ein Thema erstellt haben, entweder indem Sie Client-App-Instanzen auf Clientseite oder über die Server-API für das Thema abonnieren, können Sie Nachrichten an das Thema senden. Wenn Sie zum ersten Mal Sendeanfragen für FCM erstellen, finden Sie im Leitfaden zu Ihrer Serverumgebung und FCM wichtige Hintergrundinformationen und Informationen zur Einrichtung.
Geben Sie in Ihrer Sendelogik im Backend den gewünschten Themennamen wie unten gezeigt an:
Node.js
// The topic name can be optionally prefixed with "/topics/".
const topic = 'highScores';
const message = {
data: {
score: '850',
time: '2:45'
},
topic: topic
};
// Send a message to devices subscribed to the provided topic.
getMessaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
Java
// The topic name can be optionally prefixed with "/topics/".
String topic = "highScores";
// See documentation on defining a message payload.
Message message = Message.builder()
.putData("score", "850")
.putData("time", "2:45")
.setTopic(topic)
.build();
// Send a message to the devices subscribed to the provided topic.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
Python
# The topic name can be optionally prefixed with "/topics/".
topic = 'highScores'
# See documentation on defining a message payload.
message = messaging.Message(
data={
'score': '850',
'time': '2:45',
},
topic=topic,
)
# Send a message to the devices subscribed to the provided topic.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
Go
// The topic name can be optionally prefixed with "/topics/".
topic := "highScores"
// See documentation on defining a message payload.
message := &messaging.Message{
Data: map[string]string{
"score": "850",
"time": "2:45",
},
Topic: topic,
}
// Send a message to the devices subscribed to the provided topic.
response, err := client.Send(ctx, message)
if err != nil {
log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)
C#
// The topic name can be optionally prefixed with "/topics/".
var topic = "highScores";
// See documentation on defining a message payload.
var message = new Message()
{
Data = new Dictionary<string, string>()
{
{ "score", "850" },
{ "time", "2:45" },
},
Topic = topic,
};
// Send a message to the devices subscribed to the provided topic.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
REST
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"topic" : "foo-bar",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message"
}
}
}
cURL-Befehl:
curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
"message": {
"topic" : "foo-bar",
"notification": {
"body": "This is a Firebase Cloud Messaging Topic Message!",
"title": "FCM Message"
}
}
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Wenn Sie eine Nachricht an eine Kombination von Themen senden möchten, geben Sie eine Bedingung an. Das ist ein boolescher Ausdruck, der die Zielthemen angibt. Mit der folgenden Bedingung werden beispielsweise Nachrichten an Geräte gesendet, die TopicA
und entweder TopicB
oder TopicC
abonniert haben:
"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"
Bei FCM werden zuerst alle Bedingungen in Klammern und dann der Ausdruck von links nach rechts ausgewertet. Im obigen Ausdruck erhält ein Nutzer, der ein einzelnes Thema abonniert hat, die Nachricht nicht. Ebenso erhält ein Nutzer, der TopicA
nicht abonniert hat, die Nachricht nicht. Bei diesen Kombinationen ist sie enthalten:
TopicA
undTopicB
TopicA
undTopicC
Sie können bis zu fünf Themen in Ihren bedingten Ausdruck aufnehmen.
An eine Bedingung senden:
Node.js
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
const condition = '\'stock-GOOG\' in topics || \'industry-tech\' in topics';
// See documentation on defining a message payload.
const message = {
notification: {
title: '$FooCorp up 1.43% on the day',
body: '$FooCorp gained 11.80 points to close at 835.67, up 1.43% on the day.'
},
condition: condition
};
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
getMessaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
Java
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
String condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
Message message = Message.builder()
.setNotification(Notification.builder()
.setTitle("$GOOG up 1.43% on the day")
.setBody("$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.")
.build())
.setCondition(condition)
.build();
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
String response = FirebaseMessaging.getInstance().send(message);
// Response is a message ID string.
System.out.println("Successfully sent message: " + response);
Python
# Define a condition which will send to devices which are subscribed
# to either the Google stock or the tech industry topics.
condition = "'stock-GOOG' in topics || 'industry-tech' in topics"
# See documentation on defining a message payload.
message = messaging.Message(
notification=messaging.Notification(
title='$GOOG up 1.43% on the day',
body='$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.',
),
condition=condition,
)
# Send a message to devices subscribed to the combination of topics
# specified by the provided condition.
response = messaging.send(message)
# Response is a message ID string.
print('Successfully sent message:', response)
Go
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
condition := "'stock-GOOG' in topics || 'industry-tech' in topics"
// See documentation on defining a message payload.
message := &messaging.Message{
Data: map[string]string{
"score": "850",
"time": "2:45",
},
Condition: condition,
}
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
response, err := client.Send(ctx, message)
if err != nil {
log.Fatalln(err)
}
// Response is a message ID string.
fmt.Println("Successfully sent message:", response)
C#
// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
var message = new Message()
{
Notification = new Notification()
{
Title = "$GOOG up 1.43% on the day",
Body = "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.",
},
Condition = condition,
};
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
// Response is a message ID string.
Console.WriteLine("Successfully sent message: " + response);
REST
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
{
"message":{
"condition": "'dogs' in topics || 'cats' in topics",
"notification" : {
"body" : "This is a Firebase Cloud Messaging Topic Message!",
"title" : "FCM Message",
}
}
}
cURL-Befehl:
curl -X POST -H "Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA" -H "Content-Type: application/json" -d '{
"notification": {
"title": "FCM Message",
"body": "This is a Firebase Cloud Messaging Topic Message!",
},
"condition": "'dogs' in topics || 'cats' in topics"
}' https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1
Nächste Schritte
- Sie können Ihren Server verwenden, um Client-App-Instanzen für Themen zu abonnieren und andere Verwaltungsaufgaben auszuführen. Weitere Informationen finden Sie unter Themenabos auf dem Server verwalten.