Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send notifications #26

Open
danielphan2003 opened this issue Sep 28, 2024 · 7 comments
Open

How to send notifications #26

danielphan2003 opened this issue Sep 28, 2024 · 7 comments
Labels
question Further information is requested

Comments

@danielphan2003
Copy link

danielphan2003 commented Sep 28, 2024

Similar to Utils.notify

@Aylur
Copy link
Owner

Aylur commented Sep 28, 2024

There is currently no builtin utility for sending notifications yet, you can use libnotify in the meantime

@adminy

This comment was marked as off-topic.

@Aylur

This comment was marked as off-topic.

@adminy

This comment was marked as off-topic.

@Aylur

This comment was marked as off-topic.

@Aylur Aylur changed the title Document how to add notifications How to send notifications Sep 30, 2024
@Aylur Aylur added the question Further information is requested label Sep 30, 2024
@PoSayDone
Copy link

PoSayDone commented Nov 7, 2024

I'm trying to use libnotify but no success, am I doing something wrong?

Using libnotify in my ScreenRecorderService GObject. (But acting the same even in App main function)

After sending notification Ags just stops responding to anything

const ScreenRecorderService = GObject.registerClass(
	{
		Properties: {
			...
		},
	},
	class Recorder extends GObject.Object {
		#recordings = GLib.getenv("HOME") + "/Videos/Screenrecords";
		#screenshots = GLib.getenv("HOME") + "/Pictures/Screenshots";
		#file = "";
		#interval: AstalIO.Time | null = null;

		#recording = false;
		#timer = 0;

		get recording() {
			return this.#recording;
		}
		get timer() {
			return this.#timer;
		}

		async start() {
			...
		}

		async stop() {
			if (!this.recording) return;

			Notify.init("Ags");

			try {
				await execAsync(
					`bash -c "$XDG_CONFIG_HOME/ags/bin/screenrecord-end.sh" & disown`,
				);

				this.#recording = false;
				this.notify("recording");

				if (this.#interval) this.#interval.cancel();
				this.#timer = 0;
				this.notify("timer");

				try {
					const notification = new Notify.Notification({
						appName: "Ags",
						summary: "Screenrecord",
						body: this.#file,
						iconName: icons.fallback.video,
					});

					notification.add_action(
						"show_in_files",
						"Show in Files",
						() => exec(`bash -c xdg-open ${this.#recordings}`),
					);
					notification.add_action("view", "View", () =>
						exec(`bash -c xdg-open ${this.#file}`),
					);

					if (!notification.show()) {
						console.log("Error in notify");
					}
				} catch (e) {
					console.log(e);
				}
			} catch (e) {
				console.error("Error executing screenrecord-end script:", e);
			} finally {
				// Ensure Notify is uninitialized when done with it
				Notify.uninit();
			}
		}
	},
);

@Aylur
Copy link
Owner

Aylur commented Nov 9, 2024

libnotify is synchronous, so if you send from the same process that runs the daemon, it will block the eventblock

currently the most simple way to send a notification imo is just execAsync+notify-send

const res = await execAsync([
    "notify-send", "Screenrecord", this.file,
    "-a", "Screenrecord",
    "-i", "video-x-generic-symbolic",
    "--hint=string:image:video-x-generic-symbolic",
    "-A", "file=Show in Files",
    "-A", "view=View",
])

switch (res) {
    case "file":
        return execAsync([
            GLib.getenv("FILE_MANAGER") || "xdg-open", Recording.DIR,
        ])
    case "view":
        return execAsync([
            "xdg-open", this.file,
        ])
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants