تعرف على النظام الجديد لإرسال الإشعارات في فايربيز (HTTP v1) ولماذا هو الأفضل للمطورين
إرسال الإشعارات اليوم صار أكثر أمانًا وتنظيمًا بفضل انتقال Firebase Cloud Messaging إلى HTTP v1 API بدل Legacy API. في هذا الدليل الحصري سأشرح لك خطوة-بخطوة كيفية إرسال إشعار من النوع topic باستخدام سكربت Node.js (مثال كامل مرفق)، وكيفية عمل نفس العملية من Postman، بالإضافة لنقاط مهمة متعلقة بـ FCM Flutter واشتراك التطبيق في الـ topic
لماذا HTTP v1؟
النسخة الجديدة (HTTP v1) تستخدم OAuth 2.0 Access Token بدل Server Key الثابت. الفائدة:
أمان أعلى (توكن مؤقت وليس مفتاح ثابت).
تكامل مع Google Cloud IAM.
بنية رسالة أكثر مرونة (تدعم apns/android/data بالإضافة إلى notification).
لكن التغيير يتطلب توليد توكن عبر Service Account JSON ووضع بنية message صحيحة (مثلاً لا تستخدم to كما كان في Legacy بل message.topic أو message.token).
المتطلبات الأساسية للتعامل مع الاشعارات
حساب Firebase ومشروع مفعل.
Service Account JSON (حمّلته من Firebase Console → Project Settings → Service accounts → Generate new private key).
تفعيل Firebase Cloud Messaging API من Google Cloud Console (APIs & Services → Library).
جهاز لتشغيل Node.js أو Postman.
تطبيق Flutter يشتري topic (لجزء FCM Flutter).
إرسال Topic Notification
import { GoogleAuth } from 'google-auth-library';
import fetch from 'node-fetch';
import fs from 'fs';
async function sendTopicNotification() {
const credentials = JSON.parse(fs.readFileSync('path json file', 'utf8'));
const auth = new GoogleAuth({
credentials,
scopes: ['https://www.googleapis.com/auth/firebase.messaging'],
});
const client = await auth.getClient();
const token = await client.getAccessToken();
const projectId = credentials.project_id;
const body = {
message: {
topic: "SRM",
notification: {
title: "تحديث جديد 💸",
body: "قم بتحديث التطبيق الآن للاستمتاع بالمميزات الجديدة"
},
data: {
url: "https://www.geecoders.com",
id_app: "com.geecoders.geecoders"
},
apns: {
payload: {
aps: {
sound: "Tri-tone",
"mutable-content": 1
}
}
}
}
};
const response = await fetch(`https://fcm.googleapis.com/v1/projects/${projectId}/messages:send`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token.token}`,
'Content-Type': 'application/json; UTF-8'
},
body: JSON.stringify(body)
});
const result = await response.json();
console.log("Response from FCM:", result);
console.log("projectId ===:", projectId);
console.log("token ===:", token.token);
}
sendTopicNotification().catch(console.error);
npm init -ynpm install google-auth-library node-fetch
كيف تعمل نفس العملية من Postman
https://fcm.googleapis.com/v1/projects/geecoders/messages:send
- Headers:- `Authorization: Bearer <ACCESS_TOKEN>` (التوكن الذي حصلت عليه)- `Content-Type: application/json` (أو `application/json; UTF-8`)
{
"message": {
"topic": "SRM",
"notification": {
"title": "تحديث جديد 💸",
"body": "قم بتحديث التطبيق الآن للاستمتاع بالمميزات الجديدة"
},
"data": {
"url": "https://www.geecoders.com",
"id_app": "com.geecoders.geecoders"
},
"apns": {
"payload": {
"aps": {
"sound": "Tri-tone",
"mutable-content": 1
}
}
}
}
}