This repository provides integration with Firebase Cloud Messaging (FCM) for sending push notifications from a Frappe/ERPNext instance. The app uses FCM's REST API to send notifications to client devices, leveraging Firebase service account credentials.
- Push Notifications: Send push notifications to Android and iOS devices using Firebase Cloud Messaging.
- Access Token Management: Automatically fetches and caches access tokens to reduce API calls.
- Customizable Notification Settings: Configurable FCM Notification Settings for managing Firebase service account credentials.
- Ensure your Frappe/ERPNext environment is running.
cd /path/to/frappe-bench/apps
git clone https://github.com/fadilsiddique/frappe_fcm_notification.gitcd /path/to/frappe-bench
bench install-app frappe_fcm_notificationbench migrate
bench restart- In your Firebase Console, go to Project Settings > Service accounts.
- Click Generate new private key. This downloads a JSON file containing your service account credentials and firebase admin-sdk credentials.
The app expects FCM Notification Settings values to be filled in with data from admin-sdk json file, the file name should look like project-name-firebase-adminsdk-8uf66-eagb4660e2.json.
Follow these steps to configure FCM Notification Settings in your Frappe/ERPNext instance:
-
Go to Frappe > Settings > FCM Notification Settings in your Frappe instance.
-
Populate the following fields using the values from your
service_token.jsonfile:Project ID:"project_id"from JSONPrivate Key ID:"private_key_id"from JSONPrivate Key:"private_key"from JSON (replace\\nwith actual newline characters)Client Email:"client_email"from JSONClient ID:"client_id"from JSONAuth URI:"auth_uri"from JSONToken URI:"token_uri"from JSONAuth Provider X509 Cert URL:"auth_provider_x509_cert_url"from JSONClient X509 Cert URL:"client_x509_cert_url"from JSON
-
Save the settings.
Once the settings are configured, you can send notifications through the following API endpoints:
-
Send FCM Notification
Endpoint:
/api/method/frappe_fcm_notification.fcm_notification.notification_queueParameters:
device_token: The device token of the recipient.title: The title of the notification.body: The body text of the notification.
Example:
{ "device_token": "device_token_value", "title": "Hello", "body": "This is a test notification." }Response:
{ "status": "success", "response": { "name": "projects/your-project-id/messages/message-id" } }
- Errors are logged in Error Logs in Frappe, allowing you to view and debug any issues that occur during notification sending.
- You can limit the length of logged access tokens to prevent security issues (e.g., only logging the first 50 characters).
@frappe.whitelist()
def get_fcm_credentials():
credentials_doc = frappe.get_single("FCM Notification Settings")
return {
"type": "service_account",
"project_id": credentials_doc.get("project_id"),
"private_key_id": credentials_doc.get("private_key_id"),
"private_key": credentials_doc.get_password("private_key").replace("\\n", "\n"),
"client_email": credentials_doc.get("client_email"),
"client_id": credentials_doc.get("client_id"),
"auth_uri": credentials_doc.get("auth_uri"),
"token_uri": credentials_doc.get("token_uri"),
"auth_provider_x509_cert_url": credentials_doc.get("auth_provider_x509_cert_url"),
"client_x509_cert_url": credentials_doc.get("client_x509_cert_url")
}@frappe.whitelist()
def send_fcm_notification(device_token, title, body):
access_token = get_cached_access_token()["access_token"]
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json; UTF-8",
}
payload = {
"message": {
"token": device_token,
"notification": {
"title": title,
"body": body
},
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"title": title,
"body": body
}
}
}
fcm_endpoint = f'https://fcm.googleapis.com/v1/projects/{get_fcm_credentials()["project_id"]}/messages:send'
response = requests.post(fcm_endpoint, headers=headers, json=payload)
if response.status_code == 200:
return {"status": "success", "response": response.json()}
else:
frappe.log_error(response.text, "FCM Notification Error")
return {"status": "failed", "error": response.text}- Invalid Device Token: Ensure that the device token provided is valid and registered with FCM.
- Authentication Issues: Verify the
service_token.jsoncredentials and check that the token URI and other settings in FCM Notification Settings match. - Access Token Expiration: Access tokens are cached for 55 minutes; however, if tokens expire sooner, revalidate the token cache logic.
This project is licensed under the MIT License.
Feel free to submit pull requests and issues. Contributions are welcome!