I'm building an app using Firebase Notifications where a user can enter their zip code and then receive notifications for specific events happening in their locality.
How do I only send notifications to a specific user? I've tried including Zip Code in the Advanced Options sections of Firebase Notifications, but it still sends the notification to all users.
Seems like Topics is what you are looking for. When the user enters their zipcode you can subscribe them to a topic eg: /topics/zip then you can send to the appropriate zipcode from the console. Subscribing will create the topic if it does not already exist.
Note: Topics can take up to 24 hours to show up in the console after they are created.
It seems you are trying to Send Notifications to a User Segment in the Firebase Console. However, it seems that the specific detail you're targeting is not an option that is available in the console.
As of right now, the only available User Segments you can send to are (these can be modified in the Target Section when Sending a Notification):
- (User) Audience
- (Device) Language
- English (I think it may vary depending on the language you're using)
- (App) Version
- Contains
- Does not contain
- Exact Match
- Regular Expression
With the above being clear, I think the only way for you to send to those specific users in a specific Zip Code, you should be implementing the push from your server app, wherein you collect the users profiles with the corresponding Zip Code, and then push the messages to their registration tokens.
I think the links mentioned in this answer will help you:
Firebase Cloud Messaging has server-side APIs that you can call to send messages. See https://firebase.google.com/docs/cloud-messaging/server.
Sending a message can be as simple as using curl
to call a HTTP end-point. See https://firebase.google.com/docs/cloud-messaging/server#implementing-http-connection-server-protocol
curl -X POST --header "Authorization: key=<API_ACCESS_KEY>" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"to\":\"<YOUR_DEVICE_ID_TOKEN>\",\"notification\":{\"body\":\"Yellow\"} \"priority":\"10"}"