إرسال الإخطارات الأصلية منصة Cross باستخدام Node.js. مركز الإخطار لـ MacOS ، notify-osd / libnotify-bin لـ Linux ، محامصات لنظام التشغيل Windows 8/10 ، أو بالونات Taskbar لإصدارات Windows السابقة. يتم استخدام الهدير إذا لم يتم استيفاء أي من هذه المتطلبات. يعمل بشكل جيد مع الإلكترون.
عرض إشعارًا أصليًا على MacOS و Windows و Linux:
const notifier = require ( 'node-notifier' ) ;
// String
notifier . notify ( 'Message' ) ;
// Object
notifier . notify ( {
title : 'My notification' ,
message : 'Hello, there!'
} ) ; notify-osd أو libnotify-bin (يجب أن يكون لدى Ubuntu هذا افتراضيًا)انظر الوثائق وتدفق الرسم البياني للاختيار المراسل.
npm install --save node-notifierانتقل CLI إلى مشروع منفصل: https://github.com/mikaelbr/node-notifier-cli
الاستخدام القياسي ، مع عوامل التراجع عبر المنصات على النحو المحدد في مخطط تدفق المراسل. ستعمل جميع الخيارات أدناه بطريقة أو بأخرى على معظم المنصات.
const notifier = require ( 'node-notifier' ) ;
const path = require ( 'path' ) ;
notifier . notify (
{
title : 'My awesome title' ,
message : 'Hello from node, Mr. User!' ,
icon : path . join ( __dirname , 'coulson.jpg' ) , // Absolute path (doesn't work on balloons)
sound : true , // Only Notification Center or Windows Toasters
wait : true // Wait with callback, until user action is taken against notification, does not apply to Windows Toasters as they always wait or notify-send as it does not support the wait option
} ,
function ( err , response , metadata ) {
// Response is response from notification
// Metadata contains activationType, activationAt, deliveredAt
}
) ;
notifier . on ( 'click' , function ( notifierObject , options , event ) {
// Triggers if `wait: true` and user clicks notification
} ) ;
notifier . on ( 'timeout' , function ( notifierObject , options ) {
// Triggers if `wait: true` and notification closes
} ) ;إذا كنت ترغب في تحكم فائق الحبيبات ، فيمكنك تخصيص كل مراسل بشكل فردي ، مما يتيح لك ضبط خيارات محددة للأنظمة المختلفة.
انظر أدناه للحصول على الوثائق على كل مراسل.
مثال:
const NotificationCenter = require ( 'node-notifier/notifiers/notificationcenter' ) ;
new NotificationCenter ( options ) . notify ( ) ;
const NotifySend = require ( 'node-notifier/notifiers/notifysend' ) ;
new NotifySend ( options ) . notify ( ) ;
const WindowsToaster = require ( 'node-notifier/notifiers/toaster' ) ;
new WindowsToaster ( options ) . notify ( ) ;
const Growl = require ( 'node-notifier/notifiers/growl' ) ;
new Growl ( options ) . notify ( ) ;
const WindowsBalloon = require ( 'node-notifier/notifiers/balloon' ) ;
new WindowsBalloon ( options ) . notify ( ) ;أو ، إذا كنت تستخدم عدة مراسلين (أو كنت كسولًا):
// NOTE: Technically, this takes longer to require
const nn = require ( 'node-notifier' ) ;
new nn . NotificationCenter ( options ) . notify ( ) ;
new nn . NotifySend ( options ) . notify ( ) ;
new nn . WindowsToaster ( options ) . notify ( options ) ;
new nn . WindowsBalloon ( options ) . notify ( options ) ;
new nn . Growl ( options ) . notify ( options ) ; NotificationCenter نفس الاستخدام وإعداد المعلمة كطرف terminal-notifier .
يتطلب مركز الإخطار الأصلي الإصدار 10.8 أو أعلى. إذا كان لديك نسخة سابقة ، فستكون Growl هي الاحتياط. إذا لم يتم تثبيت GRETL ، فسيتم إرجاع خطأ في رد الاتصال.
نظرًا لأن node-notifier يلتف حول terminal-notifier ، يمكنك فعل أي شيء يمكن أن يفعله terminal-notifier ، فقط عن طريق تمرير الخصائص إلى طريقة notify .
على سبيل المثال:
terminal-notifier يقول -message ، يمكنك القيام {message: 'Foo'}terminal-notifier تقول -list ALL ، يمكنك القيام {list: 'ALL'} .الإخطار هو التركيز الأساسي لهذه الوحدة ، لذا فإن القائمة والتفعيل تعمل ، لكنها غير موثقة.
const NotificationCenter = require ( 'node-notifier' ) . NotificationCenter ;
var notifier = new NotificationCenter ( {
withFallback : false , // Use Growl Fallback if <= 10.8
customPath : undefined // Relative/Absolute path to binary if you want to use your own fork of terminal-notifier
} ) ;
notifier . notify (
{
title : undefined ,
subtitle : undefined ,
message : undefined ,
sound : false , // Case Sensitive string for location of sound file, or use one of macOS' native sounds (see below)
icon : 'Terminal Icon' , // Absolute Path to Triggering Icon
contentImage : undefined , // Absolute Path to Attached Image (Content Image)
open : undefined , // URL to open on Click
wait : false , // Wait for User Action against Notification or times out. Same as timeout = 5 seconds
// New in latest version. See `example/macInput.js` for usage
timeout : 5 , // Takes precedence over wait if both are defined.
closeLabel : undefined , // String. Label for cancel button
actions : undefined , // String | Array<String>. Action label or list of labels in case of dropdown
dropdownLabel : undefined , // String. Label to be used if multiple actions
reply : false // Boolean. If notification should take input. Value passed as third argument in callback and event emitter.
} ,
function ( error , response , metadata ) {
console . log ( response , metadata ) ;
}
) ; ملاحظة: خيار wait هو اختزال timeout: 5 . هذا فقط يضع مهلة لمدة 5 ثوان. لا يجعل الإخطار لزجة!
اعتبارًا من الإصدار 6.0 ، هناك مجموعة timeout افتراضية من 10 لضمان إغلاق التطبيق بشكل صحيح. من أجل إزالة timeout والحصول على إشعار إغلاق على الفور (لا يدعم الإجراءات) ، قم بتعيين timeout على false . إذا كنت تستخدم action فمن المستحسن تعيين timeout على قيمة عالية لضمان أن يكون لدى المستخدم الوقت للرد.
الاستثناء: إذا تم تعريف reply ، فمن المستحسن تعيين timeout على قيمة عالية ، أو لا شيء على الإطلاق.
لإخطارات MacOS: icon ، contentImage ، وجميع أشكال reply / actions تتطلب MacOS 10.9.
يمكن أن يكون الصوت أحد هذه: Basso ، Blow ، Bottle ، Frog ، Funk ، Glass ، Hero ، Morse ، Ping ، Pop ، Purr ، Sosumi ، Submarine ، Tink .
إذا كان sound true ببساطة ، يتم استخدام Bottle .
انظر أيضا:
توضيح المسار المخصص
يأخذ customPath قيمة المسار النسبي أو المطلق إلى ثنائي من شوكة/إصدار مخصص من terminal-notifier .
مثال ./vendor/mac.noindex/terminal-notifier.app/Contents/MacOS/terminal-notifier
توضيح الأضواء
terminal-notifier.app يتواجد في مجلد mac.noindex لمنع الأضواء من فهرسة التطبيق.
WindowsToasterملاحظة: هناك بعض القيود على الصور في إخطارات Windows 8 الأصلية:
هذه القيود ناتجة عن نظام إشعار الخبز المحمص. نصيحة جيدة هي استخدام شيء مثل path.join أو path.delimiter للحفاظ على مساراتك عبر النظام الأساسي.
من Mikaelbr/Gulp-Notify#90 (تعليق)
يمكنك جعلها تعمل عن طريق الذهاب إلى النظام> الإخطارات والإجراءات. يحتاج تطبيق "Toast" إلى تمكين لافتات. (يمكنك تنشيط لافتات من خلال النقر على تطبيق "Toast" ووضع "لافتات الإشعار" على ON)
تحديث Windows 10 Fall Creators (الإصدار 1709) ملاحظة:
يتم استخدام Snoretoast للحصول على نخب النوافذ الأصلية!
السلوك الافتراضي هو أن يكون تطبيق Toster Applicaton الأساسي appID . هذا يعمل كما هو متوقع ، ولكنه يظهر SnoreToast كنص في الإخطار.
من خلال تحديث Fall Creators ، ستعمل الإشعارات على Windows 10 فقط كما هو متوقع إذا تم تحديد appID صالح. يجب أن يكون appID الخاص بك هو نفس القيمة التي تم تسجيلها أثناء تثبيت تطبيقك.
يمكنك العثور على معرف التطبيق الخاص بك عن طريق البحث في السجل appID الذي حددته عند تثبيت تطبيقك. على سبيل المثال: إذا كنت تستخدم Framework Squirrel ، فسيكون appID الخاص بك مثل com.squirrel.your.app .
const WindowsToaster = require ( 'node-notifier' ) . WindowsToaster ;
var notifier = new WindowsToaster ( {
withFallback : false , // Fallback to Growl or Balloons?
customPath : undefined // Relative/Absolute path if you want to use your fork of SnoreToast.exe
} ) ;
notifier . notify (
{
title : undefined , // String. Required
message : undefined , // String. Required if remove is not defined
icon : undefined , // String. Absolute path to Icon
sound : false , // Bool | String (as defined by http://msdn.microsoft.com/en-us/library/windows/apps/hh761492.aspx)
id : undefined , // Number. ID to use for closing notification.
appID : undefined , // String. App.ID and app Name. Defaults to no value, causing SnoreToast text to be visible.
remove : undefined , // Number. Refer to previously created notification to close.
install : undefined // String (path, application, app id). Creates a shortcut <path> in the start menu which point to the executable <application>, appID used for the notifications.
} ,
function ( error , response ) {
console . log ( response ) ;
}
) ;Growl const Growl = require ( 'node-notifier' ) . Growl ;
var notifier = new Growl ( {
name : 'Growl Name Used' , // Defaults as 'Node'
host : 'localhost' ,
port : 23053
} ) ;
notifier . notify ( {
title : 'Foo' ,
message : 'Hello World' ,
icon : fs . readFileSync ( __dirname + '/coulson.jpg' ) ,
wait : false , // Wait for User Action against Notification
// and other growl options like sticky etc.
sticky : false ,
label : undefined ,
priority : undefined
} ) ;شاهد المزيد من المعلومات حول استخدام Growly.
WindowsBalloon بالنسبة للإصدارات السابقة من Windows ، يتم استخدام بالونات شريط المهام (ما لم يتم تنشيط الاحتياطي وتشغيل هدير). يستخدم Balloons Sotfier مشروعًا رائعًا يسمى notifu .
const WindowsBalloon = require ( 'node-notifier' ) . WindowsBalloon ;
var notifier = new WindowsBalloon ( {
withFallback : false , // Try Windows Toast and Growl first?
customPath : undefined // Relative/Absolute path if you want to use your fork of notifu
} ) ;
notifier . notify (
{
title : undefined ,
message : undefined ,
sound : false , // true | false.
time : 5000 , // How long to show balloon in ms
wait : false , // Wait for User Action against Notification
type : 'info' // The notification type : info | warn | error
} ,
function ( error , response ) {
console . log ( response ) ;
}
) ; انظر الاستخدام الكامل على الصفحة الرئيسية للمشروع: notifu .
NotifySend ملاحظة: لا يدعم notify-send علامة wait .
const NotifySend = require ( 'node-notifier' ) . NotifySend ;
var notifier = new NotifySend ( ) ;
notifier . notify ( {
title : 'Foo' ,
message : 'Hello World' ,
icon : __dirname + '/coulson.jpg' ,
wait : false , // Defaults no expire time set. If true expire time of 5 seconds is used
timeout : 10 , // Alias for expire-time, time etc. Time before notify-send expires. Defaults to 10 seconds.
// .. and other notify-send flags:
'app-name' : 'node-notifier' ,
urgency : undefined ,
category : undefined ,
hint : undefined
} ) ; راجع الأعلام والخيارات على صفحة MAN notify-send(1)
أصبح node-notifier ممكنًا من خلال برنامج مفتوح المصدر. شكر خاص جدا لجميع الوحدات التي تستخدمها node-notifier .
terminal-notifierSnoretoastnotifugrowlyانظر هذه القضية من قبل Araxeus.
SnoreToast انظر الملاحظة على "تحديث Windows 10 Fall Creators" في قسم Windows. إجابة قصيرة: قم بتحديث appID الخاص بك.
إذا كنت لا ترى إشعارات داخل WSL2 ، فقد تضطر إلى تغيير إذن ملفات البائعين exe (Snoretoast). انظر القضية لمزيد من المعلومات
عند استخدام node-notifier داخل جلسة TMUX ، يمكن أن يتسبب في تعليق في النظام. يمكن حل هذا باتباع الخطوات الموضحة في هذا التعليق
هناك المزيد من المعلومات هنا #61 (تعليق).
حتى إذا قمت بتحديد أيقونة في كائن التكوين لـ node-notifier ، فسترى أيقونة طرفية صغيرة في الإخطار (انظر المثال في الجزء العلوي من هذا المستند).
هذه هي الطريقة التي تعمل بها الإخطارات على MacOS. إنهم يعرضون دائمًا أيقونة تطبيق الوالدين الذي يبدأ الإخطار. بالنسبة إلى node-notifier ، فإن terminal-notifier هي البادئ ، ولديه أيقونة المحطة المحددة على أنها أيقونة.
لتحديد الرمز المخصص الخاص بك ، تحتاج إلى terminal-notifier غير المتقدمة وإنشاء الإصدار المخصص الخاص بك مع الرمز الخاص بك.
انظر العدد رقم 71 لمزيد من المعلومات #71.
إذا كان تعبئة تطبيق الإلكترون الخاص بك باعتباره asar ، فستجد node-notifier فشل في التحميل.
نظرًا للطريقة التي يعمل بها Asar ، لا يمكنك تنفيذ ثنائي من داخل asar . كحل بسيط ، عند تعبئة التطبيق في ASAR ، يرجى التأكد من أنك --unpack vendor/ مجلد node-notifier ، وبالتالي فإن الوحدة لا تزال لديها إمكانية الوصول إلى ثنائيات الإخطار.
يمكنك القيام بذلك مع الأمر التالي:
asar pack . app.asar --unpack " ./node_modules/node-notifier/vendor/** " أو إذا كنت تستخدم electron-builder دون استخدام ASAR مباشرة ، قم بإلحاق كائن build على package.json على النحو التالي:
...
build: {
asarUnpack: [
' ./node_modules/node-notifier/**/* ' ,
]
},
...للمشكلات باستخدام وحدة PKG. تحقق من هذه المشكلة: #220 (تعليق)
عند استخدام node-notifier داخل webpack ، يجب عليك إضافة المقتطف أدناه إلى webpack.config.js .
هذا ضروري لأن node-notifier يقوم بتحميل الملاحظات من ثنائي ، بحيث يحتاج إلى مسار ملف نسبي. عندما يقوم WebPack بتجميع الوحدات النمطية ، فإنه يقمع أدلة الملفات ، مما تسبب في node-notifier للخطأ على منصات معينة.
لإصلاح ذلك ، يمكنك تكوين WebPack للحفاظ على أدلة الملفات النسبية. قم بذلك عن طريق إلحاق الكود التالي بـ webpack.config.js :
node: {
__filename : true ,
__dirname : true
} تم ترخيص هذه الحزمة باستخدام ترخيص MIT.
يحتوي Snoretoast و Notifu على تراخيص في إصداراتهما المماثلة التي لا تتطابق مع ترخيص MIT ، LGPL-3 و BSD 3-poin لتكون محددة. نحن لسنا محامين ، لكننا بذلوا قصارى جهدنا للتوافق مع الشروط في تلك التراخيص أثناء إطلاق هذه الحزمة باستخدام الترخيص الذي اخترناه.