參考https://github.com/olalonde/node-notify的实现

模块的C++代码 node_gtknotify.cc

#include <v8.h>

#include <node.h>

#include <string>

#include <gtkmm-3.0/gtkmm.h>

#include <libnotifymm.h>

using namespace v8;

class GtkNotify : node::ObjectWrap{

public:

GtkNotify(){}

~GtkNotify(){}

std::string title;

std::string icon;

static Persistent<FunctionTemplate> persistent_function_template;

static void Init(Handle<Object> target){

HandleScope scope;

Local<FunctionTemplate> local_function_template = FunctionTemplate::New(New);

GtkNotify::persistent_function_template = Persistent<FunctionTemplate>::New(local_function_template);

GtkNotify::persistent_function_template->InstanceTemplate()->SetInternalFieldCount(1);

GtkNotify::persistent_function_template->SetClassName(String::NewSymbol("Notification"));

GtkNotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New("title"), GetTitle, SetTitle);

GtkNotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New("icon"), GetIcon, SetIcon);

NODE_SET_PROTOTYPE_METHOD(GtkNotify::persistent_function_template, "send", Send);

target->Set(String::NewSymbol("notification"), GtkNotify::persistent_function_template->GetFunction());

}

static Handle<Value> New(const Arguments& args){

HandleScope scope;

GtkNotify* instance = new GtkNotify();

instance->title = "Node.js";

instance->icon = "terminal";

instance->Wrap(args.This());

return args.This();

}

static Handle<Value> Send(const Arguments& args){

HandleScope scope;

GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(args.This());

String::Utf8Value v8str(args[0]);

//弹出消息框

Notify::init("Basic");

Notify::Notification n(instance->title.c_str(), *v8str, instance->icon.c_str());

n.show();

return Boolean::New(true);

}

static Handle<Value> GetTitle(Local<String> property, const AccessorInfo& info){

GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(info.Holder());

return String::New(instance->title.c_str());

}

static Handle<Value> GetIcon(Local<String> property, const AccessorInfo& info){

GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(info.Holder());

return String::New(instance->icon.c_str());

}

static void SetTitle(Local<String> property, Local<Value> value, const AccessorInfo& info) {

GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(info.Holder());

String::Utf8Value v8str(value);

instance->title = *v8str;

}

static void SetIcon(Local<String> property, Local<Value> value, const AccessorInfo& info) {

GtkNotify* instance = node::ObjectWrap::Unwrap<GtkNotify>(info.Holder());

String::Utf8Value v8str(value);

instance->icon = *v8str;

}

};

Persistent<FunctionTemplate> GtkNotify::persistent_function_template;

extern "C"{

static void init(Handle<Object> target){

GtkNotify::Init(target);

}

NODE_MODULE(node_gtknotify, init);

}

node-gyp配置文件 binding.gyp

{

"targets": [

{

"target_name": "node_gtknotify",

"sources": [ "src/node_gtknotify.cc" ]

}

]

}

文件夹结构

执行命令

node-gyp configure

node-gyp build

假设没有安装对应的库/路径找不到。中间会出现头文件找不到的错误;

笨拙的解决方法。在build以下的Makefile中加入

CXXFLAGS += -I/usr/include/glibmm-2.4 -I/usr/lib/x86_64-linux-gnu/glibmm-2.4/include -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/sigc++-2.0 -I/usr/lib/x86_64-linux-gnu/sigc++-2.0/include
-I/usr/include/giomm-2.4 -I/usr/include/gdkmm-3.0 -I/usr/lib/x86_64-linux-gnu/gdkmm-3.0/include -I/usr/include/pangomm-1.4 -I/usr/lib/x86_64-linux-gnu/pangomm-1.4/include -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0
-I/usr/include/cairomm-1.0 -I/usr/include/freetype2 -I/usr/include/gtkmm-3.0 -I/usr/lib/x86_64-linux-gnu/gtkmm-3.0/include -I/usr/include/atkmm-1.6 -I/usr/include/atk-1.0 -I/usr/include/libnotifymm-1.0

上述的库是依据错误提示进行安装的

Javascript測试代码

var notify = require("./build/Release/node_gtknotify");

var notification = new notify.notification();

notification.title = "Notification title";

notification.icon = "emblem-default"; // see /usr/share/icons/gnome/16x16

notification.send("hello,world");

注意,执行时可能有错误提示:**符号找不到,这是由于没有加入对应的共享链接库

解决方法:在build/node_gtknotify.target.mk中加入

LIBS := -lglibmm-2.4 -lnotify -lnotifymm-1.0

执行效果

Nodejs扩展,实现消息弹窗的更多相关文章

  1. Sagit.Framework For IOS 开发框架入门教程5:消息弹窗STMsgBox

    前言: 昨天刚写了一篇IT连创业的文章:IT连创业系列:产品设计之答题模块,(欢迎大伙关注!) 感觉好久没写IOS的文章了,今天趁机,来补一篇,Sagit的教程. Sagit 开源地址:https:/ ...

  2. 【WPF】对话框/消息弹窗

    非模式对话框 需求:弹窗是非模式对话框,即可以多个弹窗弹出,且弹窗后面的窗体可以被操作,不会被锁定. 自定义的窗体Window实现以下步骤: 在C#代码中弹出窗体时,使用 window.Show() ...

  3. 搭建前端监控系统(五)Nodejs怎么搭建消息队列

    怎样定位前端线上问题,一直以来,都是很头疼的问题,因为它发生于用户的一系列操作之后.错误的原因可能源于机型,网络环境,接口请求,复杂的操作行为等等,在我们想要去解决的时候很难复现出来,自然也就无法解决 ...

  4. UWP笔记-消息弹窗自动淡出

    为了让用户有个更好的UI交互,可以增加自动淡出的消息弹窗,例如:网易云音乐UWP,切换播放模式时,出现的类似消息提示. 右键项目,添加用户控件 UserControlDemo.xaml: <Us ...

  5. 扩展:Flash消息

    扩展:Flash消息 flash存值之后只能取一次 from flask import Flask,render_template,flash,get_flashed_messages,session ...

  6. BOOST 线程完全攻略 - 扩展 - 线程消息通讯

      // controlled_module_ex.hpp : controlled_module类的扩展 // 增强线程之间消息通讯 // 增加线程安全启动和安全关闭功能 // 增加定时器功能 #p ...

  7. js封装好的模仿qq消息弹窗代码

    在我们的日常开发中,或者生活中.常常须要用到弹出窗.这里我们就用js模拟一下qq消息一样的弹出窗. 直接贴代码: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  8. Nodejs 接收RabbitMQ消息

    参考官方地址:https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html 关于C#消息发送端,请参考<c# RabbitMQ ...

  9. nodejs EventEmitter 发送消息

    var util = require('util'); var evem = require('events').EventEmitter; function myem(){ evem.call(th ...

随机推荐

  1. 机器学习-- Logistic回归 Logistic Regression

    转载自:http://blog.csdn.net/linuxcumt/article/details/8572746 1.假设随Tumor Size变化,预测病人的肿瘤是恶性(malignant)还是 ...

  2. CI的多级目录的功能

    https://segmentfault.com/q/1010000008317555?_ea=1621531

  3. Angular(二)

    <!DOCTYPE html> <html lang="en" ng-app='myApp'> <head> <meta charset= ...

  4. 转:Android ListView 异步加载图片

    http://www.iteye.com/topic/1118828 http://www.iteye.com/topic/1127914 这样做无疑是非常可取的方法,但是加载图片时仍然会感觉到轻微的 ...

  5. linux和shell关系

    坚持知识分享,该文章由Alopex编著, 转载请注明源地址: http://www.cnblogs.com/alopex/   索引: 什么是shell shell的分类 shell脚本的执行方式   ...

  6. EF4学习链接

    原文发布时间为:2011-09-23 -- 来源于本人的百度文章 [由搬家工具导入] 1.Fluent API 的方式定义与数据库映射 2.利用特性实现与数据库的映射 3.EF的一些公约的介绍 4.E ...

  7. (计数器)NOIP模拟赛(神奇的数位DP题。。)

    没有原题传送门.. 手打原题QAQ [问题描述]     一本书的页数为N,页码从1开始编起,请你求出全部页码中,用了多少个0,1,2,…,9.其中—个页码不含多余的0,如N=1234时第5页不是00 ...

  8. greasemonkey

    Greasemonkey Hacks/Getting Started < Greasemonkey Hacks Greasemonkey Hacks Foreword Credits Prefa ...

  9. Linux c 目录操作函数scandir

    头文件#include <dirent.h>  函数定义:int scandir(const char *dir,struct dirent **namelist,int (*filter ...

  10. JS打印——第三方控件打印

    LODOP 官方地址:http://www.lodop.net/ 一个很好的打印控件,可以是实现纸张设置.横打竖打.打印预览.打印维护多种功能.官网的示例非常详细.能很好支持多种浏览器的打印. 在使用 ...