4

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)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Programmer12
  • 89
  • 1
  • 8
  • FCM messaging originates from the backend that you use to send it. You can use any topic name you want. The app just needs to be subscribed to it, and the backend just needs to target it. – Doug Stevenson Dec 20 '20 at 07:50
  • Hello @DougStevenson thanks for your reply. How can I make a topic please and subscribe the app to it. (sorry I am new to this) – Programmer12 Dec 20 '20 at 08:01

3 Answers3

3

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.

  • That's great thank you. It was just what I was looking for. However, I am getting an error raised when I use this. The error: ``` message = messaging.Message( NameError: name 'messaging' is not defined```. Any help is greatly appreciated.@Jonathan Nolasco Barrientos – Programmer12 Dec 20 '20 at 22:29
  • For sure, you need to import messaging at first: ``from firebase_admin import messaging``, check it out this example https://github.com/firebase/firebase-admin-python/blob/eefc31b67bc8ad50a734a7bb0a52f56716e0e4d7/snippets/messaging/cloud_messaging.py#L24-L40 – Jonathan Nolasco Barrientos Dec 22 '20 at 04:30
3

The above solutions are depreciated and outdated.

Let me include the latest implementation of firebase-admin SDK for python.

import firebase_admin
from firebase_admin import credentials, messaging

cred = credentials.Certificate(
    "<path-to-your-credential-json>")
firebase_admin.initialize_app(cred)

topic = 'notification'

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

print(response)

*Note of few configurations:

  1. Obtain your credential.json in your firebase console under: "Project settings" -> "Service accounts" -> "Generate new private key"
  2. Make sure you subscribe to the correct topic name for both of your server and client application. Every client applications that subscribed to the same topic regardless of which devices will received the corresponding notifications.

Have a good day~

1

To subscribe an Android client to a topic, do as shown in the documentation on subscribing to a topic:

FirebaseMessaging.getInstance().subscribeToTopic("weather")

Then you can send a message to that topic from a trusted environment, such as your development machine, a server you control, or Cloud Functions. For an example of this, see How do you send a Firebase Notification to all devices via CURL?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807