لاستهداف رسالة إلى أجهزة متعددة، استخدِم ميزة المراسلة حسب الموضوع. تتيح لك هذه الميزة إرسال رسالة إلى أجهزة متعدّدة وافقت على تلقّي إشعارات بشأن موضوع معيّن.
يركّز هذا البرنامج التعليمي على إرسال رسائل حول مواضيع معيّنة من خادم تطبيقك باستخدام 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 فعلي لتشغيل تطبيقك، وإكمال المهام التالية:
- احصل على مفتاح مصادقة لخدمة الإشعارات الفورية من Apple لحسابك على Apple Developer.
- فعِّل الإشعارات الفورية في XCode ضمن التطبيق > الإمكانات.
- سجِّل الدخول إلى Firebase باستخدام حسابك على Google.
إذا لم يكن لديك مشروع Xcode وتريد فقط تجربة أحد منتجات Firebase، يمكنك تنزيل أحد نماذج التشغيل السريع.
إنشاء مشروع Firebase
قبل أن تتمكّن من إضافة Firebase إلى تطبيق Apple، عليك إنشاء مشروع على Firebase لربطه بتطبيقك. انتقِل إلى التعرّف على مشاريع Firebase لمعرفة المزيد عن مشاريع Firebase.
تسجيل تطبيقك في Firebase
لاستخدام Firebase في تطبيق Apple، عليك تسجيل تطبيقك في مشروعك على Firebase. يُطلق على تسجيل تطبيقك غالبًا اسم "إضافة" تطبيقك إلى مشروعك.
انتقِل إلى Firebase وحدة التحكّم.
في وسط صفحة نظرة عامة على المشروع، انقر على رمز iOS+ لبدء سير عمل الإعداد.
إذا سبق لك إضافة تطبيق إلى مشروعك على Firebase، انقر على إضافة تطبيق لعرض خيارات المنصّة.
أدخِل معرّف حزمة تطبيقك في حقل معرّف الحزمة.
(اختياري) أدخِل معلومات التطبيق الأخرى: الاسم المختصر للتطبيق والمعرّف في App Store.
انقر على تسجيل التطبيق.
إضافة ملف إعدادات Firebase
انقر على تنزيل ملف GoogleService-Info.plist للحصول على ملف إعداد Firebase لتطبيقك (
GoogleService-Info.plist
).انقل ملف الإعداد إلى جذر مشروع Xcode. إذا طُلب منك ذلك، اختَر إضافة ملف الإعداد إلى جميع الأهداف.
إذا كان لديك عدة معرّفات حِزم في مشروعك، يجب ربط كل معرّف حزمة بتطبيق مسجّل في وحدة تحكّم Firebase لكي يتضمّن كل تطبيق ملف GoogleService-Info.plist
خاصًا به.
إضافة حِزم تطوير البرامج (SDK) من Firebase إلى تطبيقك
استخدِم أداة Swift Package Manager لتثبيت الموارد الاعتمادية في Firebase وإدارتها.
- في Xcode، بعد فتح مشروع تطبيقك، انتقِل إلى ملف > إضافة حِزم.
- عندما يُطلب منك ذلك، أضِف مستودع حزمة تطوير البرامج (SDK) لمنصة Firebase على أجهزة Apple:
- اختَر مكتبة Firebase Cloud Messaging.
- أضِف العلامة
-ObjC
إلى قسم علامات الربط الأخرى في إعدادات الإصدار الخاص بالكائن المستهدَف. - للحصول على أفضل تجربة مع Firebase Cloud Messaging، ننصحك بتفعيل Google Analytics في مشروعك على Firebase وإضافة حزمة تطوير البرامج (SDK) لخدمة "إحصاءات Google" على Firebase إلى تطبيقك. يمكنك اختيار المكتبة بدون جمع معرّف المعلِن أو مع جمع معرّف المعلِن. اطّلِع على الأسئلة الشائعة حول أحدث تنظيم للوحدات في Google Analytics لحزمة تطوير البرامج (SDK) لمنصة Firebase.
- بعد الانتهاء، سيبدأ Xcode تلقائيًا في حلّ التبعيات وتنزيلها في الخلفية.
https://github.com/firebase/firebase-ios-sdk.git
تحميل مفتاح مصادقة APNs
حمِّل مفتاح مصادقة APNs إلى Firebase. إذا لم يكن لديك مفتاح مصادقة APNs، احرص على إنشاء مفتاح في مركز أعضاء مطوّري Apple.
-
داخل مشروعك في وحدة تحكّم Firebase، انقر على رمز الترس، ثم على إعدادات المشروع، ثم على علامة التبويب Cloud Messaging.
-
في مفتاح مصادقة APNs ضمن إعداد تطبيق iOS، انقر على زر تحميل.
-
انتقِل إلى الموقع الذي حفظت فيه المفتاح، واختَره، ثم انقر على فتح. أضِف معرّف المفتاح (المتوفّر في Apple Developer Member Center) وانقر على تحميل.
إعداد Firebase في تطبيقك
عليك إضافة رمز إعداد Firebase إلى تطبيقك. استورِد وحدة Firebase وأعِدّ مثيلاً مشتركًا كما هو موضّح:
- استورِد الوحدة
FirebaseCore
فيUIApplicationDelegate
، بالإضافة إلى أي وحدات Firebase أخرى يستخدمها مفوّض تطبيقك. على سبيل المثال، لاستخدام Cloud Firestore و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; // ...
- اضبط مثيلاً مشتركًا
FirebaseApp
في طريقةapplication(_:didFinishLaunchingWithOptions:)
لمندوب تطبيقك: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];
- إذا كنت تستخدم 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() } } } }
التسجيل لتلقّي إشعارات عن بُعد
إما عند بدء التشغيل أو في النقطة المطلوبة في مسار تطبيقك، سجِّل تطبيقك لتلقّي الإشعارات عن بُعد. استخدِم CallregisterForRemoteNotifications
كما هو موضّح أدناه:
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];
delegate
الخاصة بفئة FIRMessaging. على سبيل المثال، في تطبيق iOS، يمكنك تعيينها في الطريقة applicationWillFinishLaunchingWithOptions:
أو applicationDidFinishLaunchingWithOptions:
الخاصة بمندوب التطبيق.
الاشتراك في موضوع في تطبيق العميل
يمكن لتطبيقات العميل الاشتراك في أي موضوع حالي، أو يمكنها إنشاء موضوع جديد. عندما يشترك تطبيق عميل في اسم موضوع جديد (غير متوفّر حاليًا لمشروعك على Firebase)، يتم إنشاء موضوع جديد بهذا الاسم في FCM ويمكن لأي عميل الاشتراك فيه بعد ذلك.
للاشتراك في موضوع، استدعِ طريقة الاشتراك من سلسلة التعليمات البرمجية الرئيسية لتطبيقك (FCM ليس آمنًا للاستخدام في سلاسل التعليمات البرمجية المتعددة). إذا تعذّر طلب الاشتراك في البداية، ستعيد FCM المحاولة تلقائيًا. في الحالات التي يتعذّر فيها إكمال الاشتراك، يُصدر الاشتراك خطأ يمكنك رصده في معالج الإكمال كما هو موضّح أدناه:
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"); }];
يُجري هذا الاستدعاء طلبًا غير متزامن إلى الخلفية FCM ويشترك العميل في الموضوع المحدّد. قبل الاتصال بـ subscribeToTopic:topic
، تأكَّد من أنّ نسخة تطبيق العميل قد تلقّت رمز تسجيل من خلال معاودة الاتصال didReceiveRegistrationToken
.
في كل مرة يبدأ فيها التطبيق، تتأكّد السمة FCM من أنّه تم الاشتراك في جميع المواضيع المطلوبة. لإلغاء الاشتراك، اتّصِل بالرقم unsubscribeFromTopic:topic
، وسيتم إلغاء الاشتراك في الموضوع في الخلفية.FCM
تلقّي رسائل المواضيع والتعامل معها
تسلِّم FCM رسائل المواضيع بالطريقة نفسها التي تسلِّم بها الرسائل الأخرى الواردة من الخادم.
نفِّذ application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
كما هو موضّح:
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); }
إنشاء طلبات الإرسال
بعد إنشاء موضوع، سواء من خلال اشتراك مثيلات تطبيق العميل في الموضوع من جهة العميل أو من خلال واجهة برمجة التطبيقات للخادم، يمكنك إرسال رسائل إلى الموضوع. إذا كانت هذه هي المرة الأولى التي تنشئ فيها طلبات إرسال لـ 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);
});
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:
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);
});
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:
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
الخطوات التالية
- يمكنك استخدام الخادم للاشتراك في مثيلات تطبيق العميل في المواضيع وتنفيذ مهام إدارة أخرى. يمكنك الاطّلاع على إدارة الاشتراكات في المواضيع على الخادم.