How to set up fierbase push notification?

Hello,
I am new in frappe framework.
I try to set up the firebase push notification. install the firebase-admin dependency and set the project firebase configuration but that not works. also Not getting any reference document.
How can I set up this ?
Thank you.

2 Likes

Hey, I’m looking for the same. Did you receive any lead on this??

yes I found the solution which is below.

def on_save(doc,method):
if doc.project:
for ds in frappe.get_all(‘DocShare’,filters={‘share_doctype’:‘Project’,‘share_name’:doc.project}):
ds_doc=frappe.get_doc(‘DocShare’,ds.name)
first_name , last_name, fcm_token = frappe.db.get_value(“User”,ds_doc.get(“user”),[‘first_name’,‘last_name’,‘fcm_token’])
gateway_url = ‘https://fcm.googleapis.com/fcm/send
params = {
“to” : fcm_token,
“collapse_key” : “type_a”,
“notification”:{
“body” : “New task is created.”+first_name +“–”+last_name+“–”+doc.project+“–”+doc.subject,
“title”: first_name +last_name +" user send the notification.",
“project” : doc.project,
“task_subject” : doc.subject
},
“data” : {
“body” : “Send Notification”,
“title”: “Postman fcm notification”
}
}
headers = {
“Content-Type”:“application/json”,
“Authorization”:“authentication key set”
}
use_post = True
send_request(gateway_url,params,headers,use_post)

def send_request(gateway_url, params, headers, use_post):
if use_post:
frappe.msgprint(_(frappe.as_json(params)))
response = requests.post(gateway_url, headers=headers, json=params)
else:
response = requests.get(gateway_url, headers=headers, params=params)
response.raise_for_status()
return response.status_code

1 Like

Thank you for the guide. I happen to use another method, using firebase-admin.
Refer: Build app server send requests  |  Firebase Cloud Messaging

can u help by giving step by instructions on ho to acheive this.
i have made a web view app and the app loads a erpnext url n so its like a website in a web view app with a responsive layout…

i want to have all notifications come as push notifications for this app and when i click should open the app…

pls help

You can see my code for reference, this is based on firebase_admin_sdk

import frappe
from firebase_admin import messaging, get_app
from frappe.utils import cint
from . import init_firebase_sdk

def is_firebase_initialized():
    try:
        app = get_app()
        return True
    except ValueError:
        return False

def check_config(firebase_admin_settings=None, check_notification_settings=False):
    if not is_firebase_initialized():
        done = init_firebase_sdk()
        if not done:
            frappe.log_error("Firebase admin SDK error", "Firebase admin SDK not initialized")
            return False, "Firebase admin SDK not initialized"

    if not firebase_admin_settings:
        firebase_admin_settings = frappe.get_doc("Firebase Admin Settings","Firebase Admin Settings")
    msg = None
    if not cint(firebase_admin_settings.get("enabled")):
        msg = "Please enable firebase admin SDK"
        frappe.log_error("Firebase admin SDK error", )
        return False, msg
    if check_notification_settings:
        if not cint(firebase_admin_settings.get("enable_notifications")):
            msg = "Please enable notifications in Firebase Admin Settings"
            frappe.log_error("Firebase admin SDK error", msg)
            return False, msg
    return True, msg


def send_push_notification(title, body, tokens=None, topic=None, condition=None, data=None, method="TOKEN"):
    status, msg  = check_config( check_notification_settings=True)
    if not status:
        return status, msg

    message = messaging.Message(
        notification=messaging.Notification(
            title=title,
            body=body,
        ),
        data=data,
        token=tokens if method == "TOKEN" else None,
        topic=topic if method == "TOPIC" else None,
        condition=condition if method == "CONDITION" else None
    )
    # Send the message
    try:
        response = messaging.send(message)
        return True, response
    except Exception as e:
        return False, str(e)