We build. You grow.

Get best community software here

Start a social network, a fan-site, an education project with oxwall - free opensource community software

JS Event Hooking for Notifications | Forum

ViXiV Technologies
Service Worker Notifications must be triggered from JavaScript, which means I must hook into and listen to certain JS events.


Originally I was looking through all the PHP events to see what was available, but realized quickly this must all be done from JS.


I have Notifications working for Desktop and Mobile, but now need to hook into events to be able to send them properly.


Naturally, we'll probably AT LEAST want Notifications on receiving a New Message. Going through all the JS that gets served I've found this, but can anyone make any sense of it and how I could access or use from another script:

Anyone understand this JS?


MAILBOX_ConversationItem = Backbone.Model.extend({

    selected: false,


    initialize: function(){


        var conversationUnread = OWM.Mailbox.unreadMessageList.findWhere({convId: this.get('conversationId')});

        if ( conversationUnread ){

            this.set({selected: true});

        }


        var that = this;

        OW.bind('mailbox.new_message_notification', function(data){

            if (data.message.convId == that.get('conversationId')){

                that.set({selected: true});

            }

        });

    }

});



I need to listen for this event in my own JS script, but can't access OW (undefined):

        OW.bind('mailbox.new_message_notification', function(data){

            if (data.message.convId == that.get('conversationId')){

                that.set({selected: true});

            }

        });


The only other option is editing the mailbox static JS script when the plugin installs, but I believe this against the plugin rules...


This is for Mobile Only so I have to hook into onPing to handle Desktop, because there doesn't seem to be an equivalent event to listen to for Desktop for when a new message is received, unless someone knows better? 

The Forum post is edited by ViXiV Technologies Aug 27
ViXiV Technologies
From IRC:

(kevireilly) ViXiVTech: seems feasible that you may be trying to bind to `OW` before it has been defined since your binding is in the head and whatever OW is appears to be included before </body>


ViXiV Technologies
Found my answer and Notifications working on New Messages!
Jake - JB TECH Oxwall Support
Care to elaborate the solution? I was thinking you could call over OW and such via PHP to set for your own JS script and go from there.
ViXiV Technologies
I'm currently still looking for the BEST solution, but one hacky way to do it would be like this, but this only provides notifications while the user is active on the page and so its not a true service worker push notification.


Service workers are basically just little JS daemons running in the clients browser, but that means you have to interface with them from the client's browser (index) JS and can't do so from PHP as far as I'm aware.You register a service worker in the clients browser allowing for push / web notifications.
Initially for testing I was using Web Notifications, but will be switching to PUSH.Then in the main index (or added to):


$(function() {

        OW.bind('mailbox.new_message_notification', function(data){

            console.log(data);

            displayNotification('$site', \"New Message\", \"New Message\"); // quick test message


        });

});


Basically this is saying on document ready, bind to this JS function to event 'mailbox.new_message_notification'
You don't even have to register a service worker for this, you can literally put the notification function in the main index as well, but kind of defeats the purpose of having a service worker be able to push notifications to a user that isn't live on the site.........

The Forum post is edited by ViXiV Technologies Aug 28
ViXiV Technologies
Also working on dynamic caching in service worker, so if I seem a little absent that's why.
ViXiV Technologies
I'll probably end up using a PHP push library to send the PUSH notification to Firebase Cloud Messaging FCM (GCM Replacement) which will send to service worker.
Paul
Paul Sep 11
Maybe the Push Notification Plugin could be interesting for you. They are using OneSignal, which is easy to use and can handle a lot of browsers
ViXiV Technologies
Found the PHP event for messages and found that you can sort of hijack other plugins event handler and add code without messing up the other plugin, ie the Messaging plugin in this instance, as long as you leave their code intact along with your additions. Any plugins event handler can be added to yours for hooking into their events as long as you leave their code alone. I can't really look at pay plugins for development as I believe that would be illegal to reverse engineer, but I've figured out everything I need. Advanced and Pro should be released by Monday, but not sure if I'll be including the push notifications in advanced.