如何基于用户属性、受众及应用版本发送FCM通知?含Firebase Admin API实现
Hey there! Let me walk you through how to send Firebase Cloud Messaging (FCM) notifications targeted by user properties, audiences, and app versions—both using the Firebase Console and the Firebase Admin API.
The console makes this pretty straightforward, since you already noted it supports these targeting options directly. Here's how to use each:
Targeting by User Properties
- Head to the Cloud Messaging section in the Firebase Console and click "Send your first message".
- In the "Target" step, select User segments.
- Click "Add condition" and build your rule using user properties you've set (e.g.,
user_properties.subscribed == trueoruser_properties.country == "FR"). You can combine multiple conditions withAND/ORlogic.
Targeting by Audiences
- If you've already created an audience in Firebase Analytics (e.g., "Weekly Active Users" or a custom group based on user behavior/properties), just select Audiences in the "Target" step.
- Pick the specific audience you want to notify, and the console will automatically target all devices associated with users in that group.
Targeting by App Version
- Again, go to User segments in the "Target" step.
- Add a condition using the
app_versionproperty. You can use exact matches (e.g.,app_version == "2.1.0") or range comparisons (e.g.,app_version >= "2.0.0" && app_version < "3.0.0"). Just make sure your app versions follow a consistent format (like semantic versioning) for reliable matching.
For automated or server-side notifications, the Admin API lets you replicate these targeting options with code. I'll use Node.js examples here since it's one of the most common SDKs, but the logic translates to other supported languages too.
First, make sure you've initialized the Admin SDK with your service account credentials.
Targeting by User Properties
Use conditional messages to target users with specific properties. Here's an example:
const admin = require('firebase-admin'); admin.initializeApp(); // Send a notification to users in Germany who have opted into promotions const message = { notification: { title: 'Exclusive German Deals!', body: 'Don’t miss out on our limited-time offers for Germany.' }, condition: 'user_properties.country == "DE" && user_properties.promotions_opt_in == true' }; admin.messaging().send(message) .then((response) => console.log('Message sent successfully:', response)) .catch((error) => console.error('Error sending message:', error));
Targeting by Audiences
Audiences you create in Firebase Analytics are defined by a set of conditions. To target them via the API:
- Go to your audience in the Firebase Console, view its details, and copy the underlying condition string (e.g.,
user_properties.level >= 10 && event:first_open > 2024-01-01). - Use that condition string in your Admin API message, just like the user property example above.
Alternatively, if you've linked your audience to an FCM topic, you can send directly to that topic:
// Send to a topic linked to your audience const message = { notification: { title: 'Audience Update', body: 'Important news for our loyal users.' }, topic: 'your-audience-topic-id' }; admin.messaging().send(message) .then((response) => console.log('Message sent to audience:', response)) .catch((error) => console.error('Error:', error));
Targeting by App Version
Use the app_version property in a conditional message to target specific app versions:
// Notify users on versions older than 2.2.0 to update const message = { notification: { title: 'Update Your App!', body: 'Version 2.2.0 is here with bug fixes and new features.' }, condition: 'app_version < "2.2.0"' }; admin.messaging().send(message) .then((response) => console.log('Update reminder sent:', response)) .catch((error) => console.error('Error:', error));
Key Notes
- User Properties: You need to set these on the client side first (e.g., using
FirebaseMessaging.getInstance().setUserProperty("country", "DE")on Android, or equivalent methods on iOS/Web). - App Version: FCM automatically collects this from the app, so no extra setup is needed—just ensure version strings are consistent across your app builds.
- Condition Syntax: Stick to FCM's supported operators (
==,!=,<,>,<=,>=,&&,||,!) and wrap string values in double quotes.
内容的提问来源于stack exchange,提问作者samson

