应用未在后台运行时未触发电子“打开网址”事件

时间:2018-12-10 13:08:10

标签: javascript node.js electron protocols url-scheme

当我在macOS上运行我的电子应用程序时,我想读取deeplinkingUrl附带的所有参数。当我的应用程序通过Windows上的协议调用时,我可以从process.argv中读取deeplinkingUrl。但是在macOS上是不可能的,因为process.argv给出-psn...,并且在第一次启动应用程序时仅给出一次该参数。

我想通过在我的应用程序不在macOS上的后台运行时从浏览器调用协议来触发open-url。但是此事件仅在应用程序运行时触发。当应用未运行时,应用会显示,但不会根据deeplinkingUrl附带的操作进行导航。

我也在package.json中写了那些内容:

"protocols": {
  "name": "My App",
  "schemes": [
    "myapp"
  ]
}

来自electron.js的人:

let clickToCallObj = {};
let deeplinkingUrl;
let mainWindow;

app.on('open-url', (event, url) => {
    event.preventDefault();

    const isDefaultProtocolClientSet = app.isDefaultProtocolClient('myapp');

    deeplinkingUrl = url;

    clickToCallObj.operation = utils.getOperation(deeplinkingUrl); //e.g. myapp:/call/12345 -> operation: call

    if (mainWindow && isDefaultProtocolClientSet) {
        mainWindow.isMinimized() ? mainWindow.restore() : null;
        mainWindow.focus();

        sendClickToCallEvent(clickToCallObj, deeplinkingUrl); //takes the action with coming operation and deeplinkingUrl
    }
});

那么,当我不在后台运行的macOS上调用我的应用程序时,如何获得deeplinkingUrl?

1 个答案:

答案 0 :(得分:0)

  1. app.on('will-finish-launching', )事件应该在app.on('ready', )
  2. 之前捕获
  3. isInvokedWhileAppIsNotRunning参数已设置为true,并在应用程序加载完成后受到控制。
  4. Based on electron documentation, open-url event has used in will-finish-launching event regarding their occurrence order.

    // Protocol handler for osx while application is not running in the background.
    app.on ('will-finish-launching' , () => {
        app.on('open-url', (event, url) => {
    
    
            event.preventDefault();
    
            deeplinkingUrl = url;
    
            const isDefaultProtocolClientSet    = app.isDefaultProtocolClient(getProtocolName());
            clickToCallObj.operation            = utils.getOperation(deeplinkingUrl);
    
            if (isDefaultProtocolClientSet) {
                clickToCallObj.isInvokedWhileAppIsNotRunning = true;
            }
        });
    });
    
    // Protocol handler for osx while the app is running in the background
    app.on('open-url', (event, url) => {
        event.preventDefault();
    
        const isDefaultProtocolClientSet = app.isDefaultProtocolClient(getProtocolName());
    
        clickToCallObj.operation = utils.getOperation(url);
    
        if (mainWindow && isDefaultProtocolClientSet) {
            mainWindow.isMinimized() ? mainWindow.restore() : null;
            mainWindow.focus();
    
            sendClickToCallEvent(clickToCallObj, url);
        }
    });