تتيح لك Firebase Admin SDK وFCM v1
HTTP API لطلبات الرسائل ضبط
جميع الحقول المتاحة في
عنصر message
. ويشمل ذلك:
- مجموعة مشترَكة من الحقول التي سيتم تفسيرها من خلال جميع مثيلات التطبيق التي تتلقّى الرسالة
- مجموعات الحقول الخاصة بالمنصة، مثل
AndroidConfig
وWebpushConfig
، والتي لا يتم تفسيرها إلا من خلال مثيلات التطبيق التي تعمل على المنصة المحددة
تمنحك عمليات الحظر الخاصة بالمنصات المرونة لتخصيص الرسائل على المنصات المختلفة لضمان التعامل معها بشكل صحيح عند تلقّيها. سيأخذ خادم FCM الخلفي جميع المَعلمات المحدّدة في الاعتبار ويخصّص الرسالة لكل نظام أساسي.
حالات استخدام الحقول الشائعة
استخدِم الحقول الشائعة في الحالات التالية:
- إرسال الحقول إلى أي منصة
- إرسال الرسائل إلى المواضيع
يمكن لجميع مثيلات التطبيق، بغض النظر عن النظام الأساسي، تفسير الحقول الشائعة التالية:
حالات استخدام الحقول الخاصة بالمنصة
استخدِم الحقول الخاصة بالمنصة عندما تريد:
- إرسال الحقول إلى منصات معيّنة فقط
- إرسال الحقول الخاصة بالمنصة بالإضافة إلى الحقول الشائعة
عندما تريد إرسال قيم إلى منصات معيّنة فقط، استخدِم الحقول الخاصة بالمنصة. على سبيل المثال، لإرسال إشعار إلى منصتَي Apple والويب فقط وليس إلى Android، عليك استخدام مجموعتَين منفصلتَين من الحقول، واحدة لمنصة Apple والأخرى لمنصة الويب.
عند إرسال رسائل تتضمّن خيارات تسليم محدّدة، استخدِم الحقول الخاصة بالنظام الأساسي لضبطها. يمكنك تحديد قيم مختلفة لكل نظام أساسي إذا أردت ذلك. ومع ذلك، حتى عندما تريد ضبط القيمة نفسها بشكل أساسي على جميع المنصات، عليك استخدام حقول خاصة بكل منصة. ويرجع ذلك إلى أنّ كل نظام أساسي قد يفسّر القيمة بشكل مختلف قليلاً، فعلى سبيل المثال، يتم ضبط مدة البقاء على Android كوقت انتهاء صلاحية بالثواني، بينما يتم ضبطها على Apple كتاريخ انتهاء صلاحية.
رسالة الإشعار مع خيارات التسليم الخاصة بالمنصة
يرسل طلب الإرسال التالي لواجهة برمجة التطبيقات HTTP الإصدار 1 عنوان إشعار ومحتوى شائعَين إلى جميع المنصات، ولكنه يرسل أيضًا بعض عمليات الإلغاء الخاصة بالمنصة. على وجه التحديد، يجب أن يتضمّن الطلب ما يلي:
- تضبط مدة بقاء طويلة لمنصتَي Android والويب، مع ضبط أولوية رسالة APNs (منصات Apple) على إعداد منخفض
- تضبط هذه السمة المفاتيح المناسبة لتحديد نتيجة نقرة المستخدم على الإشعار على أجهزة Android وApple، وهما
click_action
وcategory
على التوالي.
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Match update",
"body":"Arsenal goal in added time, score is now 3-0"
},
"android":{
"ttl":"86400s",
"notification"{
"click_action":"OPEN_ACTIVITY_1"
}
},
"apns": {
"headers": {
"apns-priority": "5",
},
"payload": {
"aps": {
"category": "NEW_MESSAGE_CATEGORY"
}
}
},
"webpush":{
"headers":{
"TTL":"86400"
}
}
}
}
لمزيد من المعلومات، يمكنك الاطّلاع على صفحة مرجع HTTP الإصدار 1 للحصول على تفاصيل أكثر حول المفاتيح المتاحة في الأقسام الخاصة بالمنصة في نص الرسالة. لمزيد من المعلومات حول إنشاء طلبات إرسال تتضمّن نص الرسالة، راجِع إرسال رسالة باستخدام الإصدار 1 من واجهة برمجة التطبيقات HTTP لخدمة FCM.
رسالة إشعار تتضمّن خيارات الألوان والرموز
في المثال التالي، يرسل طلب الإرسال عنوان إشعار ومحتوى شائعَين إلى جميع المنصات، ولكنه يرسل أيضًا بعض عمليات الإلغاء الخاصة بالمنصة إلى أجهزة Android.
بالنسبة إلى Android، يضبط الطلب رمزًا ولونًا خاصَّين لعرضهما على أجهزة Android. كما هو موضّح في مرجع AndroidNotification، يتم تحديد اللون بتنسيق #rrggbb، ويجب أن تكون الصورة مصدرًا محليًا لرمز قابل للرسم في تطبيق Android.
في ما يلي مثال على التأثير المرئي على جهاز المستخدم:
Node.js
const topicName = 'industry-tech';
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.'
},
android: {
notification: {
icon: 'stock_ticker_update',
color: '#7e55c3'
}
},
topic: topicName,
};
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
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())
.setAndroidConfig(AndroidConfig.builder()
.setTtl(3600 * 1000)
.setNotification(AndroidNotification.builder()
.setIcon("stock_ticker_update")
.setColor("#f45342")
.build())
.build())
.setApnsConfig(ApnsConfig.builder()
.setAps(Aps.builder()
.setBadge(42)
.build())
.build())
.setTopic("industry-tech")
.build();
Python
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.',
),
android=messaging.AndroidConfig(
ttl=datetime.timedelta(seconds=3600),
priority='normal',
notification=messaging.AndroidNotification(
icon='stock_ticker_update',
color='#f45342'
),
),
apns=messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(badge=42),
),
),
topic='industry-tech',
)
Go
oneHour := time.Duration(1) * time.Hour
badge := 42
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.",
},
Android: &messaging.AndroidConfig{
TTL: &oneHour,
Notification: &messaging.AndroidNotification{
Icon: "stock_ticker_update",
Color: "#f45342",
},
},
APNS: &messaging.APNSConfig{
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
Badge: &badge,
},
},
},
Topic: "industry-tech",
}
#C
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.",
},
Android = new AndroidConfig()
{
TimeToLive = TimeSpan.FromHours(1),
Notification = new AndroidNotification()
{
Icon = "stock_ticker_update",
Color = "#f45342",
},
},
Apns = new ApnsConfig()
{
Aps = new Aps()
{
Badge = 42,
},
},
Topic = "industry-tech",
};
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":"industry-tech",
"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."
},
"android":{
"notification":{
"icon":"stock_ticker_update",
"color":"#7e55c3"
}
}
}
}
لمزيد من المعلومات، يمكنك الاطّلاع على صفحة مرجع HTTP الإصدار 1 للحصول على تفاصيل أكثر حول المفاتيح المتاحة في الأقسام الخاصة بالمنصة في نص الرسالة.
رسالة إشعار تتضمّن صورة مخصّصة
يرسل طلب الإرسال التالي عنوان إشعار شائعًا إلى جميع الأنظمة الأساسية، ولكنّه يرسل أيضًا صورة. في ما يلي مثال على التأثير المرئي على جهاز المستخدم:
Node.js
const topicName = 'industry-tech';
const message = {
notification: {
title: 'Sparky says hello!'
},
android: {
notification: {
imageUrl: 'https://foo.bar.pizza-monster.png'
}
},
apns: {
payload: {
aps: {
'mutable-content': 1
}
},
fcm_options: {
image: 'https://foo.bar.pizza-monster.png'
}
},
webpush: {
headers: {
image: 'https://foo.bar.pizza-monster.png'
}
},
topic: topicName,
};
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);
});
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":"industry-tech",
"notification":{
"title":"Sparky says hello!",
},
"android":{
"notification":{
"image":"https://foo.bar/pizza-monster.png"
}
},
"apns":{
"payload":{
"aps":{
"mutable-content":1
}
},
"fcm_options": {
"image":"https://foo.bar/pizza-monster.png"
}
},
"webpush":{
"headers":{
"image":"https://foo.bar/pizza-monster.png"
}
}
}
}
لمزيد من المعلومات، يمكنك الاطّلاع على صفحة مرجع HTTP الإصدار 1 للحصول على تفاصيل أكثر حول المفاتيح المتاحة في الأقسام الخاصة بالمنصة في نص الرسالة.
رسالة الإشعار التي تتضمّن إجراء نقر مرتبطًا
يرسل طلب الإرسال التالي عنوان إشعار شائعًا إلى جميع المنصات، ولكنه يرسل أيضًا إجراءً ينفّذه التطبيق استجابةً لتفاعل المستخدم مع الإشعار. في ما يلي مثال على التأثير المرئي على جهاز المستخدم:
Node.js
const topicName = 'industry-tech';
const message = {
notification: {
title: 'Breaking News....'
},
android: {
notification: {
clickAction: 'news_intent'
}
},
apns: {
payload: {
aps: {
'category': 'INVITE_CATEGORY'
}
}
},
webpush: {
fcmOptions: {
link: 'breakingnews.html'
}
},
topic: topicName,
};
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);
});
REST
POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1