Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Python, problem sending push notifications to phone with Google Firebase without Android App

The code is here:

https://stackoverflow.com/questions/75870903/python-problem-sending-push-notifications-to-phone-with...

The code prints a message that the file has been sent successfully, but there is no notification on my phone. How can I solve this problem? This notification and file are not reaching my phone? I don't understand where this notification is going.

When the code runs, a file comes to the https://console.firebase.google.com/ >>storage section, but a link and explanation do not come to the notification window of my phone. Does it require a separate setting?

Or is it not possible to send a notification with an icon to the notification window of the android phone with firebase without an android application relationship? If so which GCP (Google Cloud Platform) application should we use to send push notifications?

0 2 1,676
2 REPLIES 2

Hi @murattasci06,

Welcome to Google Cloud Community!

To send a push notification to your phone using Firebase Cloud Messaging (FCM), you will need to have an Android app registered with FCM. If you don't have an app, you can create a simple Android app that receives push notifications by following the Firebase documentation. Alternatively, you can try using a third-party service like OneSignal or Pusher to send push notifications to your device without an app. Uploading a file to Firebase Storage does not automatically trigger a push notification. If you want to send a notification to your phone when a new file is uploaded, you can use Cloud Functions to trigger a function when a new file is added to the storage bucket, and use the FCM API to send a notification to your device.

You may also want to check these documentations:

Thanks

Hİ,  @christianpaula

I tried to implement your triggered method for which android application is not developed, but I am ...

the code (triggered) :

import firebase_admin
from firebase_admin import credentials
from firebase_admin import messaging
from google.cloud import storage
from google.auth import compute_engine
import os

cred = credentials.Certificate('push-notifier.json')

# Initialize Firebase Admin SDK
if not firebase_admin._apps:
default_app = firebase_admin.initialize_app(cred, {
'storageBucket': 'push-notifier-144.appspot.com'
})
else:
default_app = firebase_admin.get_app()

def send_notification_on_upload(data, context):
"""Cloud Function triggered by a new file upload to a Cloud Storage bucket.
Sends a push notification to devices subscribed to the 'price_tracker' topic.

Args:
data (dict): The dictionary with data specific to this type of event.
The `data` field contains a description of the event in a JSON string.
context (google.cloud.functions.Context): Metadata of triggering event.
"""

# Get the file name and bucket name from the event data
file_name = data['name']
bucket_name = data['bucket']

# Create Google Cloud Storage client and get the bucket object
client = storage.Client()
bucket = client.bucket(bucket_name)

# Get the file object from the bucket
blob = bucket.blob(file_name)

# Create the URL of the uploaded file
file_url = f'https://storage.googleapis.com/{bucket_name}/{file_name}'

# Create the push notification message
message = messaging.Message(
notification=messaging.Notification(
title='Price Tracker Analysis has been finished.',
body='Click here to view the analysis results.'
),
data={
'url': file_url
},
topic='price_tracker'
)

# Delete previous files in the bucket
for blob in bucket.list_blobs():
if blob.name != file_name:
blob.delete()

# Send the push notification message
response = messaging.send(message)

print('Successfully sent message:', response)

def get_topic_tokens(topic_name):
"""Gets the registration tokens for devices subscribed to a topic.

Args:
topic_name (str): The name of the topic.

Returns:
list: The registration tokens of devices subscribed to the topic.
"""
# Get the tokens of the devices subscribed to the topic
http_client = messaging.ApiCallHandler.default_http_client()
response = http_client.make_get_request('/v1/projects/{project_id}/topics/{topic_name}/subscriptions', topic_name=topic_name)
subscriptions = response.get('subscriptions', [])
tokens = [subscription.get('pushConfig', {}).get('attributes', {}).get('token') for subscription in subscriptions]
return tokens

def upload_file_to_storage():
# Get credentials
credentials = service_account.Credentials.from_service_account_file('push-notifier.json')

# Initialize storage client
client = storage.Client(credentials=credentials)

# Get bucket
bucket_name = 'push-notifier-144.appspot.com'
bucket = client.bucket(bucket_name)

# Get file path and name
file_path = 'Backend-TOGG.pdf'
file_name = os.path.basename(file_path)

# Upload the file to Cloud Storage
blob = bucket.blob(file_name)
blob.upload_from_filename(file_path)

print(f'{file_name} has been uploaded to Cloud Storage.')
return file_name

if __name__ == '__main__':
# Upload the file to Cloud Storage
file_name = upload_file_to_storage()

# Trigger the notification
data = {
'name': file_name,
'bucket': 'push-notifier-144.appspot.com'
}
send_notification_on_upload(data, None)

It uploads the file to the cloud. It says that the notification has been sent, but the phone still does not receive a notification. What is missing or wrong here? (Backend-TOGG.pdf has been uploaded to Cloud Storage. Successfully sent message: projects/push-notifier-144/messages/6877140705068881297)

Top Labels in this Space
Top Solution Authors