javascriptnode-webkit

NW.js open a new window and close it on event


I'm migrating my NW.js v0.12.3 to the new NW.js v0.17.3. In my old app, I used to open a window to show an incoming call notification. And if the user answered the call or hangs up I closed the window based in a special event listener.

This is my code which works with no problems in v0.12.3:

var notificationWin;

window.onload = function () {

    var messageHandler = function(event) {
         if(event.data.key == 'incomingCall'){
            win.requestAttention(1);
            
            notificationWin = gui.Window.open('notification.html', {  
                frame: false,
                toolbar: false,        
                focus: true,
                icon: "app/imgs/traywinaz.png",
                title:"Incoming Call"
            });

            notificationWin.on ('loaded', function(){
                    ....
            });
            
      }else if(event.data.key == 'callRejected' || event.data.key == 'callAnswered' || event.data.key == 'callCanceled' ){
            notificationWin.close();
      }
    }

    window.addEventListener('message', messageHandler, false);
}

But in the new version I cant close the window in the call rejected or answered events. I can't get the notification window to close it.

My new code looks like this:

var messageHandler = function(event) {
    if(event.data.key == 'incomingCall'){
        win.requestAttention(1);

        nw.Window.open('app/notification.html', {  
            frame: false,       
            focus: true,
            icon: "app/imgs/traywinaz.png",
            id: "callNotification"
        },  function(new_notification) {

            new_notification.setAlwaysOnTop(true);  
            new_notification.setVisibleOnAllWorkspaces(true);

            new_notification.on ('loaded', function(){
                ....
            });
        });
        
    }else if(event.data.key == 'callRejected' || event.data.key == 'callAnswered' || event.data.key == 'callCanceled' ){
      try{
        notificationWin =  nw.Window.get('app/notification.html');
        notificationWin.close();
      }catch(e){  
        console.log(e);
      }
    }
};

I can only get the window inside the callback so I also tried to do something like this inside the callback:

notificationWin = new_notification;

but this closes my main window.

Any ideas of what I am doing wrong or how can I achieve this?

The documentation http://docs.nwjs.io/en/latest/References/Window/#windowgetwindow_object says Window.get([window_object]) but no idea how to get the [window_object] parameter in the new version.


Solution

  • Coming back to this, I resolved it by saving the reference inside de callback : "notificationWin = new_notification;" and then just calling notificationWin.close();

    The first time I tried this it closed my main window but now works : )