NodeRT / NodeRT

Winrt APIs-node.js modules generator
Other
663 stars 68 forks source link

Creating toast notification #159

Open ErraticFox opened 3 years ago

ErraticFox commented 3 years ago

Hello, I've successfully manually built my NodeRT package, which was windows.ui.notifications. Though, even with TypeScript support and the Microsoft JavaScript example, I'm not able to create a toast notification.

Here's the example Microsoft provides:

var notifications = Windows.UI.Notifications;

// Get the toast notification manager for the current app.
var notificationManager = notifications.ToastNotificationManager

// The getTemplateContent method returns a Windows.Data.Xml.Dom.XmlDocument object
// that contains the toast notification XML content.
var template = notifications.toastTemplateType.toastImageAndText01
var toastXml = notificationManager.getTemplateContent(notifications.ToastTemplateType[template])

// You can use the methods from the XML document to specify the required elements for the toast.
var images = toastXml.getElementsByTagName("image")
images[0].setAttribute("src", "images/toastImageAndText.png")

var textNodes = toastXml.getElementsByTagName("text")
textNodes.forEach(function (value, index) {
    var textNumber = index + 1
    var text = ""
    for (var j = 0;j < 10;j++) {
        text += "Text input " + /*@static_cast(String)*/textNumber + " "
    }
    value.appendChild(toastXml.createTextNode(text))
})

// Create a toast notification from the XML, then create a ToastNotifier object
// to send the toast.
var toast = new notifications.ToastNotification(toastXml)

notificationManager.createToastNotifier().show(toast)

which gives me the error

var template = notifications.toastTemplateType.toastImageAndText01
                                               ^
TypeError: Cannot read property 'toastImageAndText01' of undefined

so I tried changing it around to ToastTemplateType (as well as typescript telling me to) and then I get a Bad arguments: no suitable overload found. I've console.logged a lot of the elements in hopes of a solution and I've hit multiple different errors trying multiple different things, but I've not yet found a way to trigger the toast notification.

ErraticFox commented 3 years ago

Update: The current code I'm using right now seems to have progress

import { ToastNotification, ToastNotificationManager, ToastTemplateType } from 'windows.ui.notifications'

const notificationManager = ToastNotificationManager

const template = ToastTemplateType.toastImageAndText01
const toastXml = notificationManager.getTemplateContent(template)

const toast = new ToastNotification(toastXml)

notificationManager.createToastNotifier('7E46BD40-39C6-4813-B414-019AD1122333').show(toast)

I am using a random GUID for now. But this now gives me the error

$ ts-node index.ts 
Error: Cannot find module 'windows.data.xml.dom'
Require stack:
- C:\Users\errat\Desktop\NodeRT Output\windows.ui.notifications\lib\main.js

Though I went ahead and installed this in my project and the build process was successful, but I'm stumped now and I don't know where to go from here.

UPDATE 2: I finally got a notification to appear. I noticed the error was coming from the manually built module I created, so I switched back to the one I am using from NPM and it worked fine.

My only question is, how can I manually build the modules from NodeRT UI and have them include the other packages like windows.data.xml.dom?