Sending Topic Notifications via Firebase HTTP API
You currently have a functional PHP script that pushes push notifications to specific device tokens stored in an array. This is the standard approach for targeting individual users who are subscribed to a particular channel or have opted into a specific campaign. However, your goal is to broadcast a message to everyone who has installed the app by addressing a topic named alerts instead of listing individual IDs.
To achieve this, you must modify the request payload sent to the Firebase Cloud Messaging (FCM) HTTP API endpoint. The core logic remains identical: you are still using the same Google API key and maintaining the POST request to https://fcm.googleapis.com/fcm/send. The structural change is minimal but critical within the JSON data being transmitted.
In your current implementation, the $fields array defines a key called registration_ids, which accepts an array of tokens. To send a notification to a topic, you must remove the registration_ids key entirely and introduce a new key named to. The value for this key is not an array; it is a single string representing the topic name.
Here is how the payload structure changes:
$msg = array(
'body' => "abc",
'title' => "Hello from Api",
'vibrate' => 1,
'sound' => 1,
);
// Remove 'registration_ids' and add 'to' with the topic name as a string
$fields = array(
'to' => '/topics/alerts', // The topic name goes here
'notification' => $msg
);
This adjustment tells Firebase to match the message against any device that has subscribed to the /topics/alerts segment. You do not need to know the individual FCM tokens of your users for this specific action; Firebase handles the routing logic based on the topic subscription layer within its backend infrastructure.
Implementation Details and Trade-offs
When transitioning from individual token targeting to topic targeting, consider the implications for data storage and performance. In your original script, you were iterating through an array of tokens (TOKENS). If you attempt to send a message to millions of users individually, you would hit rate limits on the FCM HTTP API very quickly. Topics are designed specifically to solve this scalability issue by allowing a single request to reach potentially all subscribers without enumerating them.
However, there is a trade-off regarding the size of the payload. The to field has a maximum limit of 4096 characters for topic names in many configurations, though standard topic names are usually much shorter. You must ensure that the topic name you define in your app (e.g., alerts) matches exactly with what is registered on the client side during initialization.
If you are using a mobile SDK like Firebase Android or iOS SDKs, the subscription happens automatically when you initialize the app and pass the topic name to the FirebaseMessaging instance. For example, in an Android app, you might call messaging.subscribeToTopics(["alerts"]). Without this client-side registration, sending a message to /topics/alerts from your server will result in no devices receiving it, regardless of the payload configuration.
Security and Best Practices
Your script currently defines the API key using define( 'API_ACCESS_KEY', 'My_API_KEY' ). While this works for local development or simple scripts, embedding secrets directly in production code is a security risk if that file is exposed. In a production environment, consider moving the API key to an environment variable or a secure configuration file rather than hardcoding it.
Additionally, notice the line curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );. Disabling peer verification makes your connection vulnerable to man-in-the-middle attacks. Since you are sending sensitive data (like API keys) and relying on Firebase's infrastructure, you should always verify the SSL certificate by removing that specific option or setting it to true in a production build.
By switching from registration_ids to to, you leverage Firebase's built-in segmentation capabilities. This allows you to manage user engagement more granularly; for instance, you can create separate topics like news, updates, and alerts and target them independently without needing to update every user's subscription list on the server side.