لتوجيه رسالة إلى أجهزة متعددة، استخدم مراسلة الموضوع . تتيح لك هذه الميزة إرسال رسالة إلى أجهزة متعددة اشتركت في موضوع معين.
يركز هذا البرنامج التعليمي على إرسال رسائل الموضوع من خادم التطبيق الخاص بك باستخدام Admin SDK أو REST API لـ FCM، واستلامها والتعامل معها في تطبيق Android. سنغطي معالجة الرسائل لكل من التطبيقات الخلفية والمقدمة. يتم تناول كافة الخطوات لتحقيق ذلك، بدءًا من الإعداد وحتى التحقق.
قم بإعداد SDK
قد يغطي هذا القسم الخطوات التي أكملتها بالفعل إذا قمت بإعداد تطبيق عميل Android لـ FCM أو اتبعت خطوات إرسال رسالتك الأولى .
قبل ان تبدأ
تثبيت أو تحديث Android Studio إلى أحدث إصدار له.
تأكد من أن مشروعك يلبي هذه المتطلبات:
- يستهدف مستوى API 19 (KitKat) أو أعلى
- يستخدم أندرويد 4.4 أو أعلى
- يستخدم Jetpack (AndroidX) ، والذي يتضمن تلبية متطلبات الإصدار التالية:
-
com.android.tools.build:gradle
v7.3.0 أو الأحدث -
compileSdkVersion
28 أو الأحدث
-
قم بإعداد جهاز فعلي أو استخدم محاكيًا لتشغيل تطبيقك.
لاحظ أن حزم Firebase SDK التي تعتمد على خدمات Google Play تتطلب تثبيت خدمات Google Play على الجهاز أو المحاكي.قم بتسجيل الدخول إلى Firebase باستخدام حساب Google الخاص بك.
إذا لم يكن لديك مشروع Android بالفعل وتريد فقط تجربة أحد منتجات Firebase، فيمكنك تنزيل أحد نماذج البدء السريع الخاصة بنا.
إنشاء مشروع Firebase
قبل أن تتمكن من إضافة Firebase إلى تطبيق Android الخاص بك، تحتاج إلى إنشاء مشروع Firebase للاتصال بتطبيق Android الخاص بك. قم بزيارة فهم مشاريع Firebase لمعرفة المزيد حول مشاريع Firebase.
سجل تطبيقك مع Firebase
لاستخدام Firebase في تطبيق Android، يلزمك تسجيل تطبيقك في مشروع Firebase الخاص بك. غالبًا ما يُطلق على تسجيل تطبيقك اسم "إضافة" تطبيقك إلى مشروعك.
انتقل إلى وحدة تحكم Firebase .
في وسط صفحة النظرة العامة على المشروع، انقر فوق أيقونة Android (
) أو إضافة تطبيق لبدء سير عمل الإعداد.أدخل اسم حزمة تطبيقك في حقل اسم حزمة Android .
يحدد اسم الحزمة تطبيقك بشكل فريد على الجهاز وفي متجر Google Play.
يُشار غالبًا إلى اسم الحزمة على أنه معرف التطبيق .
ابحث عن اسم حزمة تطبيقك في ملف Gradle الخاص بالوحدة النمطية (على مستوى التطبيق)، عادةً
app/build.gradle
(مثال لاسم الحزمة:com.yourcompany.yourproject
).انتبه إلى أن قيمة اسم الحزمة حساسة لحالة الأحرف، ولا يمكن تغييرها لتطبيق Firebase Android هذا بعد تسجيله في مشروع Firebase الخاص بك.
(اختياري) أدخل معلومات التطبيق الأخرى: لقب التطبيق وشهادة توقيع التصحيح SHA-1 .
لقب التطبيق : معرف داخلي ملائم لا يظهر إلا لك في وحدة تحكم Firebase
شهادة توقيع تصحيح الأخطاء SHA-1 : يلزم وجود تجزئة SHA-1 بواسطة مصادقة Firebase (عند استخدام تسجيل الدخول بحساب Google أو تسجيل الدخول برقم الهاتف ) وروابط Firebase الديناميكية .
انقر فوق تسجيل التطبيق .
أضف ملف تكوين Firebase
قم بتنزيل ملف تكوين Firebase Android (
) ثم قم بإضافته إلى تطبيقك:google-services.json انقر فوق تنزيل google-services.json للحصول على ملف تكوين Firebase Android.
انقل ملف التكوين الخاص بك إلى الدليل الجذر للوحدة (على مستوى التطبيق) لتطبيقك.
يحتوي ملف تكوين Firebase على معرفات فريدة ولكنها غير سرية لمشروعك. لمعرفة المزيد حول ملف التكوين هذا، قم بزيارة فهم مشاريع Firebase .
يمكنك تنزيل ملف تهيئة Firebase مرة أخرى في أي وقت.
تأكد من عدم إلحاق اسم ملف التكوين بأحرف إضافية، مثل
(2)
.
لجعل القيم الموجودة في ملف التكوين
الخاص بك قابلة للوصول إلى Firebase SDK، تحتاج إلى المكون الإضافي Google Services Gradle (google-services.json google-services
).في ملف Gradle على مستوى الجذر (على مستوى المشروع) (
<project>/build.gradle.kts
أو<project>/build.gradle
)، أضف المكون الإضافي لخدمات Google باعتباره تبعية:Kotlin
plugins { id("com.android.application") version "7.3.0" apply false // ... // Add the dependency for the Google services Gradle plugin id("com.google.gms.google-services") version "4.4.0" apply false }
Groovy
plugins { id 'com.android.application' version '7.3.0' apply false // ... // Add the dependency for the Google services Gradle plugin id 'com.google.gms.google-services' version '4.4.0' apply false }
في ملف Gradle الخاص بوحدتك (على مستوى التطبيق) (عادةً
<project>/<app-module>/build.gradle.kts
أو<project>/<app-module>/build.gradle
)، أضف المكون الإضافي لخدمات Google:Kotlin
plugins { id("com.android.application") // Add the Google services Gradle plugin id("com.google.gms.google-services") // ... }
Groovy
plugins { id 'com.android.application' // Add the Google services Gradle plugin id 'com.google.gms.google-services' // ... }
أضف حزم Firebase SDK إلى تطبيقك
في ملف Gradle الخاص بالوحدة النمطية (على مستوى التطبيق) (عادةً
<project>/<app-module>/build.gradle.kts
أو<project>/<app-module>/build.gradle
)، أضف التبعية لـ Firebase Cloud مكتبة الرسائل للاندرويد. نوصي باستخدام Firebase Android BoM للتحكم في إصدار المكتبة.للحصول على تجربة مثالية مع Firebase Cloud Messaging، نوصي بتمكين Google Analytics في مشروع Firebase الخاص بك وإضافة Firebase SDK لـ Google Analytics إلى تطبيقك.
dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:32.6.0")) // Add the dependencies for the Firebase Cloud Messaging and Analytics libraries // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-messaging") implementation("com.google.firebase:firebase-analytics") }
باستخدام Firebase Android BoM ، سيستخدم تطبيقك دائمًا إصدارات متوافقة من مكتبات Firebase Android.
هل تبحث عن وحدة مكتبة خاصة بـ Kotlin؟ بدءًا من أكتوبر 2023 (Firebase BoM 32.5.0) ، يمكن لمطوري Kotlin وJava الاعتماد على وحدة المكتبة الرئيسية (لمزيد من التفاصيل، راجع الأسئلة الشائعة حول هذه المبادرة ).(بديل) أضف تبعيات مكتبة Firebase دون استخدام BoM
إذا اخترت عدم استخدام Firebase BoM، فيجب عليك تحديد كل إصدار من مكتبة Firebase في سطر التبعية الخاص به.
لاحظ أنه إذا كنت تستخدم مكتبات Firebase متعددة في تطبيقك، فإننا نوصي بشدة باستخدام BoM لإدارة إصدارات المكتبة، مما يضمن توافق جميع الإصدارات.
dependencies { // Add the dependencies for the Firebase Cloud Messaging and Analytics libraries // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-messaging:23.3.1") implementation("com.google.firebase:firebase-analytics:21.5.0") }
مزامنة مشروع Android الخاص بك مع ملفات Gradle.
تحتاج إصدارات Gradle التي تستخدم الإصدار 4.2 من المكون الإضافي Android Gradle (AGP) أو الإصدارات الأقدم إلى تمكين دعم Java 8. بخلاف ذلك، ستفشل مشاريع Android هذه في البناء عند إضافة Firebase SDK.
لإصلاح فشل البناء هذا، يمكنك اتباع أحد الخيارين:
- أضف
compileOptions
المدرجة من رسالة الخطأ إلى ملفbuild.gradle.kts
أو ملفbuild.gradle
على مستوى التطبيق . - قم بزيادة
minSdk
لمشروع Android الخاص بك إلى 26 أو أعلى.
تعرف على المزيد حول فشل البناء هذا في هذه الأسئلة الشائعة .
- أضف
اشترك في تطبيق العميل في موضوع ما
يمكن لتطبيقات العميل الاشتراك في أي موضوع موجود، أو يمكنها إنشاء موضوع جديد. عندما يشترك تطبيق عميل في اسم موضوع جديد (اسم غير موجود بالفعل لمشروع Firebase الخاص بك)، يتم إنشاء موضوع جديد بهذا الاسم في FCM ويمكن لأي عميل الاشتراك فيه لاحقًا.
للاشتراك في موضوع ما، يستدعي تطبيق العميل Firebase Cloud Messaging subscribeToTopic()
باسم موضوع FCM. تقوم هذه الطريقة بإرجاع Task
، والتي يمكن أن يستخدمها مستمع الإكمال لتحديد ما إذا كان الاشتراك ناجحًا:
Kotlin+KTX
Firebase.messaging.subscribeToTopic("weather") .addOnCompleteListener { task -> var msg = "Subscribed" if (!task.isSuccessful) { msg = "Subscribe failed" } Log.d(TAG, msg) Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show() }
Java
FirebaseMessaging.getInstance().subscribeToTopic("weather") .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { String msg = "Subscribed"; if (!task.isSuccessful()) { msg = "Subscribe failed"; } Log.d(TAG, msg); Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } });
لإلغاء الاشتراك، يستدعي تطبيق العميل Firebase Cloud Messaging unsubscribeFromTopic()
باسم الموضوع.
تلقي والتعامل مع رسائل الموضوع
تقوم FCM بتسليم رسائل الموضوع بنفس طريقة تسليم الرسائل الأخرى.
لتلقي الرسائل، استخدم خدمة تمتد FirebaseMessagingService
. يجب أن تتجاوز خدمتك عمليات الاسترجاعات onMessageReceived
و onDeletedMessages
.
قد تكون النافذة الزمنية لمعالجة الرسالة أقل من 20 ثانية اعتمادًا على التأخيرات التي تحدث قبل الاتصال onMessageReceived
، بما في ذلك تأخيرات نظام التشغيل، أو وقت بدء تشغيل التطبيق، أو حظر سلسلة الرسائل الرئيسية بواسطة عمليات أخرى، أو استغراق مكالمات onMessageReceived
السابقة وقتًا طويلاً. بعد ذلك الوقت، قد تتداخل سلوكيات نظام التشغيل المختلفة، مثل إيقاف العمليات في Android أو حدود التنفيذ في الخلفية في Android O، مع قدرتك على إكمال عملك.
يتم توفير onMessageReceived
لمعظم أنواع الرسائل، مع الاستثناءات التالية:
يتم تسليم رسائل الإشعارات عندما يكون تطبيقك في الخلفية . في هذه الحالة، يتم تسليم الإشعار إلى علبة النظام الخاصة بالجهاز. يقوم المستخدم بالنقر على إشعار لفتح مشغل التطبيق بشكل افتراضي.
رسائل تحتوي على كل من الإشعارات وحمولة البيانات، عند استلامها في الخلفية . في هذه الحالة، يتم تسليم الإشعار إلى علبة نظام الجهاز، ويتم تسليم حمولة البيانات في الإضافات الخاصة بنشاط المشغل الخاص بك.
في ملخص:
حالة التطبيق | إشعار | بيانات | كلاهما |
---|---|---|---|
المقدمة | onMessageReceived | onMessageReceived | onMessageReceived |
خلفية | علبة النظام | onMessageReceived | الإخطار: علبة النظام البيانات: في إضافات القصد. |
تحرير بيان التطبيق
لاستخدام FirebaseMessagingService
، تحتاج إلى إضافة ما يلي في بيان التطبيق الخاص بك:
<service android:name=".java.MyFirebaseMessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service>
يوصى أيضًا بتعيين القيم الافتراضية لتخصيص مظهر الإشعارات. يمكنك تحديد رمز افتراضي مخصص ولون افتراضي مخصص يتم تطبيقهما عندما لا يتم تعيين قيم مكافئة في حمولة الإشعارات.
أضف هذه السطور داخل علامة application
لتعيين الرمز الافتراضي المخصص واللون المخصص:
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. See README(https://goo.gl/l4GJaQ) for more. --> <meta-data android:name="com.google.firebase.messaging.default_notification_icon" android:resource="@drawable/ic_stat_ic_notification" /> <!-- Set color used with incoming notification messages. This is used when no color is set for the incoming notification message. See README(https://goo.gl/6BKBk7) for more. --> <meta-data android:name="com.google.firebase.messaging.default_notification_color" android:resource="@color/colorAccent" />
يعرض Android الرمز الافتراضي المخصص لـ
- جميع رسائل الإشعارات المرسلة من مؤلف الإشعارات .
- أي رسالة إعلام لا تقوم بتعيين الرمز بشكل صريح في حمولة الإعلام.
يستخدم Android اللون الافتراضي المخصص لـ
- جميع رسائل الإشعارات المرسلة من مؤلف الإشعارات .
- أي رسالة إعلام لا تقوم بتعيين اللون بشكل صريح في حمولة الإعلام.
إذا لم يتم تعيين أي رمز افتراضي مخصص ولم يتم تعيين أي رمز في حمولة الإشعارات، فسيعرض Android رمز التطبيق باللون الأبيض.
تجاوز onMessageReceived
من خلال تجاوز الطريقة FirebaseMessagingService.onMessageReceived
، يمكنك تنفيذ إجراءات بناءً على كائن RemoteMessage المستلم والحصول على بيانات الرسالة:
Kotlin+KTX
override fun onMessageReceived(remoteMessage: RemoteMessage) { // TODO(developer): Handle FCM messages here. // Not getting messages here? See why this may be: https://goo.gl/39bRNJ Log.d(TAG, "From: ${remoteMessage.from}") // Check if message contains a data payload. if (remoteMessage.data.isNotEmpty()) { Log.d(TAG, "Message data payload: ${remoteMessage.data}") // Check if data needs to be processed by long running job if (needsToBeScheduled()) { // For long-running tasks (10 seconds or more) use WorkManager. scheduleJob() } else { // Handle message within 10 seconds handleNow() } } // Check if message contains a notification payload. remoteMessage.notification?.let { Log.d(TAG, "Message Notification Body: ${it.body}") } // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. See sendNotification method below. }
Java
@Override public void onMessageReceived(RemoteMessage remoteMessage) { // TODO(developer): Handle FCM messages here. // Not getting messages here? See why this may be: https://goo.gl/39bRNJ Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); if (/* Check if data needs to be processed by long running job */ true) { // For long-running tasks (10 seconds or more) use WorkManager. scheduleJob(); } else { // Handle message within 10 seconds handleNow(); } } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } // Also if you intend on generating your own notifications as a result of a received FCM // message, here is where that should be initiated. See sendNotification method below. }
تجاوز onDeletedMessages
في بعض الحالات، قد لا يقوم FCM بتسليم رسالة. يحدث هذا عندما يكون هناك عدد كبير جدًا من الرسائل (> 100) معلقة لتطبيقك على جهاز معين في وقت اتصاله أو إذا لم يكن الجهاز متصلاً بـ FCM لأكثر من شهر واحد. في هذه الحالات، قد تتلقى رد اتصال إلى FirebaseMessagingService.onDeletedMessages()
عندما يتلقى مثيل التطبيق رد الاتصال هذا، يجب أن يقوم بإجراء مزامنة كاملة مع خادم التطبيق الخاص بك. إذا لم تكن قد أرسلت رسالة إلى التطبيق على هذا الجهاز خلال الأسابيع الأربعة الماضية، فلن يتصل FCM بـ onDeletedMessages()
.التعامل مع رسائل الإعلام في تطبيق الخلفية
عندما يكون تطبيقك في الخلفية، يقوم Android بتوجيه رسائل الإشعارات إلى علبة النظام. يقوم المستخدم بالنقر على الإشعار لفتح مشغل التطبيق بشكل افتراضي.
يتضمن ذلك الرسائل التي تحتوي على الإشعارات وحمولة البيانات (وجميع الرسائل المرسلة من وحدة تحكم الإشعارات). في هذه الحالات، يتم تسليم الإشعار إلى علبة نظام الجهاز، ويتم تسليم حمولة البيانات في الإضافات الخاصة بنشاط المشغل الخاص بك.
للحصول على معلومات حول تسليم الرسائل إلى تطبيقك، راجع لوحة معلومات تقارير FCM ، التي تسجل عدد الرسائل المرسلة والمفتوحة على أجهزة Apple وAndroid، إلى جانب بيانات "مرات الظهور" (الإشعارات التي يراها المستخدمون) لتطبيقات Android.
بناء طلبات الإرسال
بعد إنشاء موضوع، إما عن طريق اشتراك مثيلات تطبيق العميل في الموضوع من جانب العميل أو عبر واجهة برمجة تطبيقات الخادم ، يمكنك إرسال رسائل إلى الموضوع. إذا كانت هذه هي المرة الأولى التي تقوم فيها بإنشاء طلبات إرسال لـ 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 -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 -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
الخطوات التالية
- يمكنك استخدام الخادم الخاص بك للاشتراك في مثيلات تطبيق العميل في المواضيع وتنفيذ مهام الإدارة الأخرى. راجع إدارة اشتراكات المواضيع على الخادم .