Senden Sie Nachrichten an mehrere Geräte auf Apple-Plattformen

Um eine Nachricht gezielt an mehrere Geräte zu senden, verwenden Sie Themennachrichten . Mit dieser Funktion können Sie eine Nachricht an mehrere Geräte senden, die sich für ein bestimmtes Thema angemeldet haben.

Dieses Tutorial konzentriert sich auf das Senden von Themennachrichten von Ihrem App-Server mithilfe des Admin SDK oder der REST-API für FCM sowie auf den Empfang und die Verarbeitung dieser Nachrichten in einer Apple-App. Auf dieser Seite werden alle Schritte aufgeführt, um dies zu erreichen, von der Einrichtung bis zur Überprüfung. Sie deckt also möglicherweise Schritte ab, die Sie bereits abgeschlossen haben, wenn Sie eine Apple-Client-App für FCM eingerichtet oder die Schritte zum Senden Ihrer ersten Nachricht durchgearbeitet haben.

Fügen Sie Firebase zu Ihrem Apple-Projekt hinzu

In diesem Abschnitt werden Aufgaben behandelt, die Sie möglicherweise erledigt haben, wenn Sie bereits andere Firebase-Funktionen für Ihre App aktiviert haben. Speziell für FCM müssen Sie Ihren APNs-Authentifizierungsschlüssel hochladen und sich für Remote-Benachrichtigungen registrieren .

Voraussetzungen

  • Installieren Sie Folgendes:

    • Xcode 14.1 oder höher
  • Stellen Sie sicher, dass Ihr Projekt diese Anforderungen erfüllt:

    • Ihr Projekt muss auf diese Plattformversionen oder höher abzielen:
      • iOS 11
      • macOS 10.13
      • tvOS 12
      • watchOS 6
  • Richten Sie ein physisches Apple-Gerät zum Ausführen Ihrer App ein und führen Sie die folgenden Aufgaben aus:

    • Besorgen Sie sich einen Apple-Push-Benachrichtigungs-Authentifizierungsschlüssel für Ihr Apple-Entwicklerkonto .
    • Aktivieren Sie Push-Benachrichtigungen in XCode unter App > Capabilities .

Wenn Sie noch kein Xcode-Projekt haben und einfach nur ein Firebase-Produkt ausprobieren möchten, können Sie eines unserer Schnellstartbeispiele herunterladen.

Erstellen Sie ein Firebase-Projekt

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. Besuchen Sie „Firebase-Projekte verstehen“ , um mehr über Firebase-Projekte zu erfahren.

Registrieren Sie Ihre App bei Firebase

Um Firebase in Ihrer Apple-App verwenden zu können, müssen Sie Ihre App bei Ihrem Firebase-Projekt registrieren. Das Registrieren Ihrer App wird oft als „Hinzufügen“ Ihrer App zu Ihrem Projekt bezeichnet.

  1. Gehen Sie zur Firebase-Konsole .

  2. Klicken Sie in der Mitte der Projektübersichtsseite auf das iOS+ -Symbol, 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 anzuzeigen.

  3. Geben Sie die Bundle-ID Ihrer App in das Feld „Bundle-ID“ ein.

  4. (Optional) Geben Sie weitere App-Informationen ein: App-Spitzname und App Store-ID .

  5. Klicken Sie auf App registrieren .

Fügen Sie eine Firebase-Konfigurationsdatei hinzu

  1. Klicken Sie auf „GoogleService-Info.plist herunterladen“ , um Ihre Konfigurationsdatei für die Firebase-Apple-Plattformen ( GoogleService-Info.plist ) zu erhalten.

  2. Verschieben Sie Ihre 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 Ihr Projekt mehrere Bundle-IDs enthält, müssen Sie jede Bundle-ID einer registrierten App in der Firebase-Konsole zuordnen, damit jede App über eine eigene GoogleService-Info.plist Datei verfügen kann.

Fügen Sie Ihrer App Firebase-SDKs hinzu

Verwenden Sie Swift Package Manager, um Firebase-Abhängigkeiten zu installieren und zu verwalten.

  1. Navigieren Sie in Xcode bei geöffnetem App-Projekt zu File > Add Packages .
  2. Wenn Sie dazu aufgefordert werden, fügen Sie das Firebase Apple Platforms SDK-Repository hinzu:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. Wählen Sie die Firebase Cloud Messaging-Bibliothek.
  5. Fügen Sie das Flag -ObjC zum Abschnitt „Andere Linker-Flags“ der Build-Einstellungen Ihres Ziels hinzu.
  6. Für ein optimales Erlebnis mit Firebase Cloud Messaging empfehlen wir, Google Analytics in Ihrem Firebase-Projekt zu aktivieren und das Firebase SDK für Google Analytics zu Ihrer App hinzuzufügen. Sie können entweder die Bibliothek ohne IDFA-Sammlung oder mit IDFA-Sammlung auswählen.
  7. Wenn Sie fertig sind, beginnt Xcode automatisch mit der Auflösung und dem Herunterladen Ihrer Abhängigkeiten im Hintergrund.

Laden Sie Ihren APNs-Authentifizierungsschlüssel hoch

Laden Sie Ihren APNs-Authentifizierungsschlüssel auf Firebase hoch. Wenn Sie noch keinen APNs-Authentifizierungsschlüssel haben, erstellen Sie unbedingt einen im Apple Developer Member Center .

  1. Wählen Sie in Ihrem Projekt in der Firebase-Konsole das Zahnradsymbol aus, wählen Sie Projekteinstellungen und dann die Registerkarte Cloud-Messaging aus.

  2. Klicken Sie unter „APNs-Authentifizierungsschlüssel“ unter „iOS-App-Konfiguration“ auf die Schaltfläche „Hochladen“ .

  3. Navigieren Sie zu dem Speicherort, an dem Sie Ihren 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 .

Initialisieren Sie Firebase in Ihrer App

Sie müssen Ihrer Anwendung Firebase-Initialisierungscode hinzufügen. Importieren Sie das Firebase-Modul und konfigurieren Sie eine gemeinsame Instanz wie gezeigt:

  1. Importieren Sie das FirebaseCore Modul in Ihr UIApplicationDelegate sowie alle anderen Firebase-Module, die Ihr App-Delegierter verwendet. Um beispielsweise Cloud Firestore und Authentifizierung zu verwenden:

    SwiftUI

    import SwiftUI
    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    Schnell

    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    Ziel c

    @import FirebaseCore;
    @import FirebaseFirestore;
    @import FirebaseAuth;
    // ...
          
  2. Konfigurieren Sie eine gemeinsam genutzte FirebaseApp Instanz in der application(_:didFinishLaunchingWithOptions:) Methode Ihres App-Delegierten:

    SwiftUI

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    Schnell

    // Use Firebase library to configure APIs
    FirebaseApp.configure()

    Ziel c

    // Use Firebase library to configure APIs
    [FIRApp configure];
  3. Wenn Sie SwiftUI verwenden, müssen Sie einen Anwendungsdelegaten erstellen und ihn über UIApplicationDelegateAdaptor oder NSApplicationDelegateAdaptor an Ihre App Struktur anhängen. Sie müssen auch das Swizzling von App-Delegierten deaktivieren. Weitere Informationen finden Sie in den SwiftUI-Anweisungen .

    SwiftUI

    @main
    struct YourApp: App {
      // register app delegate for Firebase setup
      @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
      var body: some Scene {
        WindowGroup {
          NavigationView {
            ContentView()
          }
        }
      }
    }
          

Registrieren Sie sich für Fernbenachrichtigungen

Registrieren Sie Ihre App entweder beim Start oder an der gewünschten Stelle in Ihrem Anwendungsablauf für Remote-Benachrichtigungen. Rufen Sie registerForRemoteNotifications wie gezeigt auf:

Schnell


UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
  options: authOptions,
  completionHandler: { _, _ in }
)

application.registerForRemoteNotifications()

Ziel c


[UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
    UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
    requestAuthorizationWithOptions:authOptions
    completionHandler:^(BOOL granted, NSError * _Nullable error) {
      // ...
    }];

[application registerForRemoteNotifications];

Abonnieren Sie die Client-App für ein Thema

Client-Apps können jedes vorhandene Thema abonnieren oder ein neues Thema erstellen. Wenn eine Client-App einen neuen Themennamen abonniert (einen, der für Ihr Firebase-Projekt noch nicht vorhanden ist), wird in FCM ein neues Thema mit diesem Namen erstellt und jeder Client kann es anschließend abonnieren.

Um ein Thema zu abonnieren, rufen Sie die Abonnementmethode vom Hauptthread Ihrer Anwendung aus auf (FCM ist nicht threadsicher). Wenn die Abonnementanforderung zunächst fehlschlägt, versucht FCM es automatisch erneut. In Fällen, in denen das Abonnement nicht abgeschlossen werden kann, löst das Abonnement einen Fehler aus, den Sie wie folgt in einem Abschlusshandler abfangen können:

Schnell

Messaging.messaging().subscribe(toTopic: "weather") { error in
  print("Subscribed to weather topic")
}

Ziel c

[[FIRMessaging messaging] subscribeToTopic:@"weather"
                                completion:^(NSError * _Nullable error) {
  NSLog(@"Subscribed to weather topic");
}];

Dieser Aufruf stellt eine asynchrone Anfrage an das FCM-Backend und abonniert den Client für das angegebene Thema. Stellen Sie vor dem Aufruf subscribeToTopic:topic sicher, dass die Client-App-Instanz über den Rückruf didReceiveRegistrationToken bereits ein Registrierungstoken erhalten hat.

Bei jedem Start der App stellt FCM sicher, dass alle angeforderten Themen abonniert wurden. Um sich abzumelden, rufen Sie unsubscribeFromTopic:topic auf, und FCM meldet sich im Hintergrund vom Thema ab.

Themennachrichten empfangen und bearbeiten

FCM übermittelt Themennachrichten auf die gleiche Weise wie andere Downstream-Nachrichten.

Implementieren Sie application(_:didReceiveRemoteNotification:fetchCompletionHandler:) wie gezeigt:

Schnell

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
}

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

Erstellen Sie Sendeanfragen

Nachdem Sie ein Thema erstellt haben, entweder durch Abonnieren von Client-App-Instanzen für das Thema auf der Clientseite oder über die Server-API , können Sie Nachrichten an das Thema senden. Wenn Sie zum ersten Mal Sendeanforderungen für FCM erstellen, finden Sie im Handbuch zu Ihrer Serverumgebung und FCM wichtige Hintergrund- und Einrichtungsinformationen.

Geben Sie in Ihrer Sendelogik im Backend den gewünschten Themennamen wie 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)

Gehen

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

AUSRUHEN

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

Um eine Nachricht an eine Kombination von Themen zu senden, geben Sie eine Bedingung an, bei der es sich um einen booleschen Ausdruck handelt, der die Zielthemen angibt. Die folgende Bedingung sendet beispielsweise Nachrichten an Geräte, die TopicA und entweder TopicB oder TopicC abonniert haben:

"'TopicA' in topics && ('TopicB' in topics || 'TopicC' in topics)"

FCM wertet zunächst alle Bedingungen in Klammern aus und wertet dann den Ausdruck von links nach rechts aus. Im obigen Ausdruck erhält ein Benutzer, der ein einzelnes Thema abonniert hat, die Nachricht nicht. Ebenso erhält ein Benutzer, der TopicA nicht abonniert, die Nachricht nicht. Diese Kombinationen erhalten es:

  • TopicA und TopicB
  • TopicA und TopicC

Sie können bis zu fünf Themen in Ihren bedingten Ausdruck aufnehmen.

So senden Sie an eine Bedingung:

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)

Gehen

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

AUSRUHEN

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