Electron에 대한 C++ 알림 추가 기능 만들기

이 가이드에서는 Electron 앱에서 Windows App SDK 알림 API를 사용하는 C++ 네이티브 추가 기능을 만드는 방법을 보여 줍니다. 이것은 더 복잡한 시나리오를 살펴보기 전에 네이티브 추가 기능을 이해하기 위한 좋은 시작점입니다.

필수 조건

1단계: C++ 네이티브 추가 기능 만들기

winapp CLI를 사용하여 C++ 추가 기능 템플릿을 생성합니다.

npx winapp node create-addon --template cpp --name nativeWindowsAddon

nativeWindowsAddon/ 폴더를 만듭니다.

  • addon.cc - Windows API를 호출하는 C++ 코드
  • binding.gyp - 네이티브 컴파일을 위한 빌드 구성

또한 이 명령은 스크립트를 build-addon에 추가하여 package.json에 포함합니다.

추가 기능을 빌드합니다.

npm run build-addon

2단계: 알림 코드 추가

nativeWindowsAddon/addon.cc 열고 Windows App SDK 알림 API를 사용하도록 업데이트합니다. 생성된 템플릿에는 필요한 Windows SDK 및 Windows App SDK 헤더가 포함됩니다.

알림 API에 대한 포함을 추가합니다.

#include <winrt/Microsoft.Windows.AppNotifications.h>
#include <winrt/Microsoft.Windows.AppNotifications.Builder.h>

Windows 알림을 표시하는 함수를 추가합니다.

Napi::Value ShowNotification(const Napi::CallbackInfo& info) {
    Napi::Env env = info.Env();

    try {
        std::string title = info[0].As<Napi::String>().Utf8Value();
        std::string message = info[1].As<Napi::String>().Utf8Value();

        winrt::Microsoft::Windows::AppNotifications::Builder::AppNotificationBuilder builder;
        builder.AddText(winrt::to_hstring(title));
        builder.AddText(winrt::to_hstring(message));

        auto notification = builder.BuildNotification();
        winrt::Microsoft::Windows::AppNotifications::AppNotificationManager::Default().Show(notification);

        return Napi::Boolean::New(env, true);
    } catch (const winrt::hresult_error& e) {
        Napi::Error::New(env, winrt::to_string(e.message())).ThrowAsJavaScriptException();
        return env.Null();
    }
}

메서드에 함수를 등록합니다 Init .

exports.Set("showNotification", Napi::Function::New(env, ShowNotification));

3단계: 추가 기능 빌드

npm run build-addon

4단계: 알림 테스트

src/index.js 추가 기능을 열고 로드합니다.

const addon = require('../nativeWindowsAddon/build/Release/nativeWindowsAddon.node');

테스트 호출을 추가합니다.

addon.showNotification('Hello from Electron!', 'This notification uses the Windows App SDK.');

앱을 실행합니다.

npm start

Windows 알림이 표시됩니다.

5단계: 디버그 ID 업데이트

appxmanifest.xml를 변경할 때마다 명령어를 실행합니다.

npx winapp node add-electron-debug-identity

다음 단계