How to enable push notification on local setup?

How to enable push notification on local setup

For frappe cloud setup there is relay server setting, but for local setup ?

anyone have enabled push notifications

@nilpatel42

Steps to Enable Push Notifications on Local Setup:

  1. Set Up a WebSocket Server:
  • You can use the socketio library, which Frappe already uses for real-time communication. In a local setup, you’ll need to make sure that the WebSocket server is running and accessible.
  • You might need to modify the Frappe socketio.js configuration to point to your local WebSocket server.
  1. Configure Frappe for Local WebSocket:
  • In the site_config.json of your Frappe site, add or update the websocket_url entry to point to your local server. For example:

json

Copy code

{
    "websocket_url": "http://localhost:3000"
}
  • Make sure that the WebSocket server (socketio) is running and listening on the specified port.
  1. Modify Frappe Configuration:
  • In your frappe-bench folder, you might need to adjust some settings related to redis and socketio in the Procfile. Ensure that the services are correctly configured to run locally.
  1. Using Push Notifications in Custom Apps:
  • If you’re using custom apps, make sure your socketio events are correctly configured and emitted from the server-side Python code.
  • On the client side (JavaScript), listen for those events using the frappe.socketio functions.
  1. Browser Support:
  • Ensure that the browsers you are testing on support WebSockets and notifications. Some older or restrictive environments might block them.
  1. Security Considerations:
  • Since this is a local setup, you may need to adjust the CORS policy and ensure that WebSockets are not blocked by your firewall or antivirus.

Example of Frappe WebSocket Integration:

  • On the server side:

python

Copy code

import frappe

@frappe.whitelist(allow_guest=True)
def notify_user(user, message):
    frappe.publish_realtime(event='msgprint', message=message, user=user)
  • On the client side:

javascript

Copy code

frappe.realtime.on('msgprint', function(message) {
    frappe.msgprint(message);
});

@nilpatel42

Another Way Is

Steps to Enable Push Notifications on Local Setup

  1. Set Up Firebase Cloud Messaging (FCM):
  • You need to use a service like Firebase Cloud Messaging (FCM) to handle push notifications.
  1. Create a Firebase Project:
  • Go to the Firebase Console.
  • Create a new project and configure it for your application.
  1. Get FCM Server Key:
  • In your Firebase project settings, navigate to the Cloud Messaging tab and obtain the server key.
  1. Install FCM Integration App:
  • You can use an existing FCM integration app for Frappe/ERPNext. One such app is erpnext_fcm.
bench get-app https://github.com/tridz-dev/erpnext_fcm
bench install-app erpnext_fcm
  1. Configure FCM in ERPNext:
  • Go to FCM Notification Settings in ERPNext.
  • Enter your FCM server key obtained from Firebase.
  1. Link User Devices:
  • Use the User Device Doctype to link device IDs to each user.
  1. Trigger Notifications:
  • Create notifications in Frappe/ERPNext and trigger them based on events. The notifications will be sent to the respective users via FCM if they have subscribed.

Example Code for Sending Notifications

Here’s an example of how you can send a notification from a server-side script:

Python

import frappe
from frappe.core.doctype.notification_log.notification_log import enqueue_create_notification

def send_push_notification(user, title, message):
    notification_doc = {
        'type': 'Alert',
        'document_type': 'User',
        'document_name': user,
        'subject': title,
        'from_user': frappe.session.user,
        'email_content': message
    }
    enqueue_create_notification(notification_doc)

# Example usage
send_push_notification('user@example.com', 'Test Notification', 'This is a test push notification.')

Thank You @Abhishek_Chougule

I will try First method and my socketio is running correctly but what changes need to be done in procfile for setup ?

@nilpatel42

  1. Procfile Configuration:

    • Ensure these lines are present in your Procfile:
      redis_socketio: redis-server config/redis_socketio.conf
      socketio: /usr/bin/node apps/frappe/socketio.js
      
  2. Adjust Paths if Needed:

    • Modify the path to node if it differs (e.g., /usr/local/bin/node).
  3. Restart Bench:

    • Apply changes by running:
      bench restart
      
  4. Check Logs:

    • Debug any issues by checking:
      tail -f logs/socketio.error.log
      tail -f logs/redis_socketio.error.log
      

I tried, but not working, I have to mention one thing i have one chat app which running on 3000 port so is there any problem with this ?

Hi @nilpatel42:

Are you talking about HRMS?

no, I am talking about Chatwoot which running on 3000 port