參考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. win2008服务器信任问题

    右键计算机,管理,在第一个页面里面有个安全信息,里面的右边有一个配置IE ESC ,点击他后会出现一个窗口,在那里面选择禁用即可!

  2. JSON.stringify与jQuery.parseJSON

    1.JSON.stringify,这个函数的作用主要是为了系列化对象的.(或者说是将原来的对象转换为字符串的,如json对象): 首先定义一个json对象,var jsonObject = { &qu ...

  3. ubuntu上安装ffmpeg

      安装包和主要步骤: 1. 首先安装系统基础环境 RHEL & CentOS 系列:yum install -y automake autoconf libtool gcc gcc-c++ ...

  4. YUY格式

    YUV格式有两大类:planar和packed. 对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V. 对于packed的YUV格式,每个像素点的Y ...

  5. 求LCA最近公共祖先的在线ST算法_C++

    ST算法是求最近公共祖先的一种 在线 算法,基于RMQ算法,本代码用双链树存树 预处理的时间复杂度是 O(nlog2n)   查询时间是 O(1) 的 另附上离线算法 Tarjan 的链接: http ...

  6. 割点与桥,强连通分量,点双,边双[poj_1236]学校网络

    割点与桥 题目描述 给定一张无向图G(V,E),你需要找出所有的割点与桥. 输入 第一行给出两个正整数V,E. 接下来E行每行两个正整数x,y,表示有一条连接x,y的边. 输出 输出共2行,第一行输出 ...

  7. bind 简单配置dns

    一. 安装apt-get install bind9 apt-get install bind9-host dnsutils apt-get install bind9-doc 二.修改本机配置我们要 ...

  8. LVM更换硬盘

    #检测坏道 smartctl -a /dev/sdd #硬盘检测 e2fsck -f /dev/mapper/vg_root-lv_data #重新定义空间大小,将原来的大小上减去要移走的硬盘 res ...

  9. SQLSERVER2008以上版本的数据恢复

    这哥们真不错... http://blog.csdn.net/dba_huangzj/article/details/8491327

  10. MPchartAnadroid的对比柱状图

    1.导入三方jar包(可参照) MPAndroidChart(GitHub上优秀得图表功能库) MPAndroidChart常见设置属性(一)——应用层 2.布局activity_main.xml(这 ...