I am using firebase database
i want to send push notification to the each fcmtoken that saved.
i want to send the all device, i am having the unique deviceId and fcmtoken
i want to write the firestore function for this
i tried this one
import firebase_admin
from firebase_admin import credentials,messaging
from firebase_admin import firestore
# Use the application default credentials
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
'projectId':'project',
})
db = firestore.client()
import json
def hello_firestore(data, context):
""" Triggered by a change to a Firestore document.
Args:
data (dict): The event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
print('\nNew value:')
newfld = data["value"]["fields"]
print(json.dumps(newfld))
newfld1 = newfld["latestMessage"]
print(json.dumps(newfld1))
msg = newfld1["mapValue"]["fields"]["message"]["stringValue"]
print(msg)
user_id = newfld1["mapValue"]["fields"]["user"]["mapValue"]["fields"]["user_Id"]["stringValue"]
print(user_id)
newfld2 = newfld["memberIds"]
print(json.dumps(newfld2))
receiverIds = newfld["memberIds"]["arrayValue"]["values"]
print(json.dumps(receiverIds))
receiverIdslst = list()
for data in receiverIds:
receiverIdslst.append(data["integerValue"])
user_details = db.collection(u'user_info').get()
for doc in user_details:
print(doc)
data = u'{}'.format(doc.to_dict())
print(data)
doc1 = eval(data)
print(doc1)
token = doc1['fcmToken']
deviceuniqueId=doc1['deviceId']
message = messaging.Message(
notification=messaging.Notification(
title="Push title",
body="Push body"),
token=token)
messaging.send(message)
print("Success")
but showing exception
Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/firebase_admin/messaging.py", line 352, in send json=data File "/env/local/lib/python3.7/site-packages/firebase_admin/_http_client.py", line 129, in body resp = self.request(method, url, **kwargs) File "/env/local/lib/python3.7/site-packages/firebase_admin/_http_client.py", line 117, in request resp.raise_for_status() File "/env/local/lib/python3.7/site-packages/requests/models.py", line 941, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://fcm.googleapis.com/v1/projects/pwe-mobile/messages:send During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v1.py", line 402, in run_background_function _function_handler.invoke_user_function(event_object) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v1.py", line 222, in invoke_user_function return call_user_function(request_or_event) File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker_v1.py", line 219, in call_user_function event_context.Context(**request_or_event.context)) File "/user_code/main.py", line 77, in hello_firestore messaging.send(message) File "/env/local/lib/python3.7/site-packages/firebase_admin/messaging.py", line 116, in send return _get_messaging_service(app).send(message, dry_run) File "/env/local/lib/python3.7/site-packages/firebase_admin/messaging.py", line 355, in send raise self._handle_fcm_error(error) firebase_admin._messaging_utils.UnregisteredError: Requested entity was not found.
How can i send push notification to the fcm token?