Notification API using frappe.publish_realtime

Dear Community,

I tried to use push notification use Notification API supported by Browser using frappe.publish_realtime ,below code snippet helped me.

It may be useful for community too.

In .py:

@frappe.whitelist()	
def display_announcement(note,announcement):
    msgvar = """Notification.requestPermission(function (permission) 
    {
        if (permission === "granted")
     {  
		 var notification = new Notification("%s"); 
		 notification.onclick = function(event){
			 event.preventDefault();
			 frappe.set_route('Form','Announcements','%s')
		 }
         }
          });""" % (note,announcement)
    user_list = frappe.get_all('User',filters={'enabled':1})
    for user in user_list:  
        frappe.publish_realtime(event='eval_js',message=msgvar,user=user['name'])

In .js:

frappe.ui.form.on("Announcements",{
	validate:function(frm){
		if(frm.doc.announce){
			frappe.call({
				method:'hunter_douglas.custom.display_announcement',
				args:{
				'note':frm.doc.note,
				'announcement':frm.doc.name
					},
				});
			}
		}
	});

Regards,
Abdulla

9 Likes