Отправляйте сообщения на несколько устройств на платформах Apple.

Чтобы отправить сообщение на несколько устройств, используйте функцию «Тематическая рассылка» . Эта функция позволяет отправлять сообщения на несколько устройств, на которых выбрана определённая тема.

В этом руководстве мы рассмотрим отправку тематических сообщений с сервера приложений с помощью Admin SDK или REST API для FCM , а также их получение и обработку в приложении Apple. На этой странице перечислены все необходимые для этого шаги, от настройки до проверки. Возможно, вы уже выполнили их, если настроили клиентское приложение Apple для FCM или выполнили шаги по отправке первого сообщения .

Добавьте Firebase в свой проект Apple

В этом разделе рассматриваются задачи, которые вы, возможно, выполнили, если уже включили другие функции Firebase в своём приложении. В частности, для FCM вам потребуется загрузить ключ аутентификации APNs и зарегистрироваться для получения удалённых уведомлений .

Предпосылки

  • Установите следующее:

    • Xcode 16.2 или более поздняя версия
  • Убедитесь, что ваш проект соответствует следующим требованиям:

    • Ваш проект должен быть ориентирован на эти версии платформ или более поздние:
      • iOS 13
      • macOS 10.15
      • tvOS 13
      • watchOS 7
  • Настройте физическое устройство Apple для запуска вашего приложения и выполните следующие задачи:

Если у вас еще нет проекта Xcode и вы просто хотите опробовать продукт Firebase, вы можете загрузить один из наших примеров быстрого старта .

Создать проект Firebase

Прежде чем добавить Firebase в приложение Apple, необходимо создать проект Firebase для подключения к нему. Подробнее о проектах Firebase можно узнать в разделе «Понимание проектов Firebase».

Зарегистрируйте свое приложение в Firebase

Чтобы использовать Firebase в приложении Apple, необходимо зарегистрировать приложение в проекте Firebase. Регистрация приложения часто называется «добавлением» приложения в проект.

  1. Перейдите в консоль Firebase .

  2. В центре страницы обзора проекта щелкните значок iOS+, чтобы запустить рабочий процесс настройки.

    Если вы уже добавили приложение в свой проект Firebase, нажмите «Добавить приложение» , чтобы отобразить параметры платформы.

  3. Введите идентификатор пакета вашего приложения в поле идентификатора пакета .

  4. (Необязательно) Введите другую информацию о приложении: псевдоним приложения и идентификатор App Store .

  5. Нажмите «Зарегистрировать приложение» .

Добавьте файл конфигурации Firebase

  1. Нажмите «Загрузить GoogleService-Info.plist» , чтобы получить файл конфигурации Firebase вашего приложения ( GoogleService-Info.plist ).

  2. Переместите файл конфигурации в корневой каталог проекта Xcode. При появлении запроса выберите добавление файла конфигурации во все целевые объекты.

Если в вашем проекте несколько идентификаторов пакетов, необходимо связать каждый идентификатор пакета с зарегистрированным приложением в консоли Firebase , чтобы каждое приложение могло иметь свой собственный файл GoogleService-Info.plist .

Добавьте Firebase SDK в свое приложение

Используйте Swift Package Manager для установки и управления зависимостями Firebase.

  1. В Xcode откройте проект приложения и перейдите в Файл > Добавить пакеты .
  2. При появлении запроса добавьте репозиторий Firebase Apple Platforms SDK:
  3.   https://github.com/firebase/firebase-ios-sdk.git
  4. Выберите библиотеку Firebase Cloud Messaging .
  5. Добавьте флаг -ObjC в раздел «Другие флаги компоновщика» настроек сборки вашей целевой системы.
  6. Для оптимальной работы с Firebase Cloud Messaging рекомендуем включить Google Analytics в вашем проекте Firebase и добавить Firebase SDK для Google Analytics в приложение. Вы можете выбрать библиотеку без сбора IDFA или со сбором IDFA. См. раздел часто задаваемых вопросов о текущей организации модулей в Google Analytics для Firebase SDK .
  7. После завершения Xcode автоматически начнет разрешать и загружать ваши зависимости в фоновом режиме.

Загрузите свой ключ аутентификации APNs

Загрузите свой ключ аутентификации APNs в Firebase. Если у вас ещё нет ключа аутентификации APNs, обязательно создайте его в Центре разработчиков Apple .

  1. Внутри вашего проекта в консоли Firebase выберите значок шестеренки, выберите Настройки проекта , а затем выберите вкладку Облачные сообщения .

  2. В разделе «Ключ аутентификации APNs» в настройках приложения iOS нажмите кнопку «Загрузить» .

  3. Перейдите к месту сохранения ключа, выберите его и нажмите «Открыть» . Добавьте идентификатор ключа (доступен в Центре разработчиков Apple ) и нажмите «Загрузить» .

Инициализируйте Firebase в вашем приложении

Вам потребуется добавить код инициализации Firebase в ваше приложение. Импортируйте модуль Firebase и настройте общий экземпляр, как показано ниже:

  1. Импортируйте модуль FirebaseCore в UIApplicationDelegate , а также любые другие модули Firebase, используемые вашим делегатом приложения. Например, чтобы использовать Cloud Firestore и Authentication :

    SwiftUI

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

    Быстрый

    import FirebaseCore
    import FirebaseFirestore
    import FirebaseAuth
    // ...
          

    Objective-C

    @import FirebaseCore;
    @import FirebaseFirestore;
    @import FirebaseAuth;
    // ...
          
  2. Настройте общий экземпляр FirebaseApp в методе application(_:didFinishLaunchingWithOptions:) делегата вашего приложения:

    SwiftUI

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

    Быстрый

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

    Objective-C

    // Use Firebase library to configure APIs
    [FIRApp configure];
  3. Если вы используете SwiftUI, необходимо создать делегат приложения и прикрепить его к структуре App через UIApplicationDelegateAdaptor или NSApplicationDelegateAdaptor . Также необходимо отключить подмену делегата приложения. Подробнее см. в инструкциях по SwiftUI .

    SwiftUI

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

Зарегистрируйтесь для получения удаленных уведомлений

Зарегистрируйте приложение для получения удалённых уведомлений при запуске или в нужном месте в потоке работы приложения. Вызовите registerForRemoteNotifications , как показано ниже:

Быстрый

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

Подпишите клиентское приложение на тему

Клиентские приложения могут подписываться на любую существующую тему или создавать новую. Когда клиентское приложение подписывается на новую тему (которой ещё нет в вашем проекте Firebase), в FCM создаётся новая тема с этим именем, и любой клиент может впоследствии подписаться на неё.

Чтобы подписаться на тему, вызовите метод подписки из основного потока приложения ( FCM не является потокобезопасным). Если запрос на подписку изначально не удалось выполнить, FCM автоматически повторяет попытку. В случаях, когда подписка не может быть завершена, она выдаёт ошибку, которую можно перехватить в обработчике завершения, как показано ниже:

Быстрый

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");
}];

Этот вызов выполняет асинхронный запрос к бэкенду FCM и подписывает клиента на заданную тему. Перед вызовом subscribeToTopic:topic убедитесь, что экземпляр клиентского приложения уже получил регистрационный токен через обратный вызов didReceiveRegistrationToken .

При каждом запуске приложения FCM проверяет, подписаны ли все запрошенные темы. Чтобы отписаться, вызовите unsubscribeFromTopic:topic , и FCM отпишется от темы в фоновом режиме.

Получать и обрабатывать тематические сообщения

FCM доставляет тематические сообщения таким же образом, как и другие нижестоящие сообщения.

Реализуйте application(_:didReceiveRemoteNotification:fetchCompletionHandler:) , как показано:

Быстрый

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

Запросы на отправку сборки

После создания темы, подписав на неё экземпляры клиентского приложения на стороне клиента или через API сервера , вы можете отправлять сообщения в эту тему. Если вы впервые создаёте запросы на отправку для FCM , см. руководство по вашей серверной среде и FCM для получения важной информации о настройке и условиях.

В логике отправки на бэкэнде укажите желаемое название темы, как показано:

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

Ява

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

Питон

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

Идти

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

С#

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

ОТДЫХ

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:

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

Чтобы отправить сообщение в несколько тем, укажите условие — логическое выражение, определяющее целевые темы. Например, следующее условие отправит сообщения устройствам, подписанным на TopicA и либо на TopicB , либо TopicC :

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

FCM сначала оценивает все условия в скобках, а затем вычисляет выражение слева направо. В приведённом выше выражении пользователь, подписанный на любую отдельную тему, не получает сообщение. Аналогично, пользователь, не подписанный на TopicA , не получает сообщение. Его получают следующие комбинации:

  • TopicA и TopicB
  • TopicA и TopicC

В условное выражение можно включить до пяти тем.

Чтобы отправить в состояние:

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

Ява

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

Питон

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

Идти

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

С#

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

ОТДЫХ

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:

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

Следующие шаги