Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushing Notification to all users in Firebase

I am trying to send push notifications using python to all users. However, I am aware that there is no way to do this using apps and you have to use topics (as far as I am aware). Is there a way that I can create a topic out of the app? Thanks Edit: I am completely new to firebase (so sorry if I am difficult)

like image 590
Programmer12 Avatar asked Oct 24 '25 19:10

Programmer12


1 Answers

First of all you need to understand a topic does not need to create (it will be create automatically), you only need to define the topic name for example if you are creating app to receive push notification when the weather change, so the topic name could be "weather".

Now you need have 2 components: mobile & backend

1. Mobile: in your mobile app you only need integrate the Firebase SDK and subscribe to the topic "weather" how do you do that?

Firebase.messaging.subscribeToTopic("weather")

Don't forget checking documentation.

2. Backend: in your server you will need to implement the sender script based on FCM SDK. If you are a beginner I'd recommend you use Postman to send push notifications and then integrate FCM in your backend app.

You can send this payload trough Postman (don't forget set your API KEY in headers)

https://fcm.googleapis.com/fcm/send

{
  "to": "/topics/weather",
  "notification": {
    "title": "The weather changed",
    "body": "27 °C"
  }
}

If that works, you can add FCM SDK to your backend:

$ sudo pip install firebase-admin
default_app = firebase_admin.initialize_app()

Finally you can send notifications as documentation says:

from firebase_admin import messaging

topic = 'weather'

message = messaging.Message(
    notification={
        'title': 'The weather changed',
        'body': '27 °C',
    },
    topic=topic,
)
response = messaging.send(message)

More details here: https://github.com/firebase/firebase-admin-python/blob/eefc31b67bc8ad50a734a7bb0a52f56716e0e4d7/snippets/messaging/cloud_messaging.py#L24-L40

You need to be patient with the documentation, I hope I've helped.

like image 69
Jonathan Nolasco Barrientos Avatar answered Oct 26 '25 10:10

Jonathan Nolasco Barrientos