الفرق بين المراجعتين ل"Laravel/notifications"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
 
(18 مراجعة متوسطة بواسطة مستخدمين اثنين آخرين غير معروضة)
سطر 1: سطر 1:
<noinclude>{{DISPLAYTITLE:الإشعارات}}</noinclude>
+
<noinclude>{{DISPLAYTITLE:الإشعارات (Notifications) في Laravel}}</noinclude>
 
+
==مقدمة==
=== مقدمة ===
 
 
بالإضافة لدعم إرسال البريد الإلكتروني، يوفّر Laravel دعمًا لإرسال إشعارات بين قنوات توصيل عديدة من ضمنها البريد الإكتروني، والرسائل القصيرة (عبر Nexmo)، و Slack. يمكن أيضًا حفظ الإشعارات في قاعدة البيانات لإظهارها في واجهة الويب.
 
بالإضافة لدعم إرسال البريد الإلكتروني، يوفّر Laravel دعمًا لإرسال إشعارات بين قنوات توصيل عديدة من ضمنها البريد الإكتروني، والرسائل القصيرة (عبر Nexmo)، و Slack. يمكن أيضًا حفظ الإشعارات في قاعدة البيانات لإظهارها في واجهة الويب.
  
 
تكون الإشعارات في العادة على شكل رسائل قصيرة، وغنية بالمعلومات، تنبّه المستخدم لشيءٍ حدث في التطبيق. مثلًا، إن كنت تكتب تطبيق فواتير، يمكنك إرسال تنبيه "تمّ خلاص الفاتورة" للمستخدم عبر بريد إلكتروني أو رسالة قصيرة.
 
تكون الإشعارات في العادة على شكل رسائل قصيرة، وغنية بالمعلومات، تنبّه المستخدم لشيءٍ حدث في التطبيق. مثلًا، إن كنت تكتب تطبيق فواتير، يمكنك إرسال تنبيه "تمّ خلاص الفاتورة" للمستخدم عبر بريد إلكتروني أو رسالة قصيرة.
 
+
==إنشاء الإشعارات==
=== صناعة الإشعارات ===
+
في Laravel، يُمثَّل كل تنبيه بصنف (موجودٌ عادةً في المجلد <code>app/Notifications</code>). لا تقلق إذا لم تجد المجلد، إذ سيُنشَأ عند تنفيذ الأمر <code>make:notification</code>:<syntaxhighlight lang="php">
في Laravel، يُمثَّل كل تنبيه بصنف (موجودٌ عادةً في المجلد <code>app/Notifications</code>). لا تقلق إذا لم تجد المجلد، إذ سيُنشَأ عند تنفيذ الأمر <code>make:notification</code>:
 
<syntaxhighlight lang="php">
 
 
Php artisan make:notification InvoicePaid  
 
Php artisan make:notification InvoicePaid  
</syntaxhighlight>
+
</syntaxhighlight>سيُنشِئ هذا الأمر صنفًا جديدًا في المجلد <code>app/Notifications</code>. يحتوي كل صنف إشعارات على التابع <code>via</code> وعدد من التوابع لكتابة نص التنبيه (مثل <code>toMail</code> أو  <code>toDatabase</code>) التي تحوّل التنبيه لرسالة ملائمة للقناة التي ستُرسَل عليها.
 
+
==إرسال الإشعارات==
سيُنشِئ هذا الأمر صنفًا جديدًا في المجلد <code>app/Notifications</code>. يحتوي كل صنف إشعارات على التابع <code>via</code> وعدد من التوابع لكتابة نص التنبيه (مثل <code>toMail</code> أو  <code>toDatabase</code>) التي تحوّل التنبيه لرسالة ملائمة للقناة التي ستُرسَل عليها.
+
===استعمال الخاصية Notifiable===
 
 
=== إرسال الإشعارات ===
 
 
 
==== استعمال الخاصية Notifiable ====
 
 
يمكن إرسال الإشعارات بطريقتين: باستعمال التبع <code>notify</code> من صنف الخاصية <code>Notifiable</code> أو باستعمال الواجهة الثابتة <code>Notification</code>. لنبدأ أولًا باستعمال الخاصية:<syntaxhighlight lang="php">
 
يمكن إرسال الإشعارات بطريقتين: باستعمال التبع <code>notify</code> من صنف الخاصية <code>Notifiable</code> أو باستعمال الواجهة الثابتة <code>Notification</code>. لنبدأ أولًا باستعمال الخاصية:<syntaxhighlight lang="php">
 
<?php
 
<?php
سطر 30: سطر 23:
  
  
</syntaxhighlight>
+
</syntaxhighlight>تُستعمل هذه الخاصية تلقائيا في النموذج <code>App/User</code> وتحتوي تابعًا واحدًا يُستعمل لإرسال الإشعارات وهو <code>notify</code>. يتلقى هذا التابع نسخة من كائن التنبيه:<syntaxhighlight lang="php">
 
 
تُستعمل هذه الخاصية تلقائيا في النموذج <code>App/User</code> وتحتوي تابعًا واحدًا يُستعمل لإرسال الإشعارات وهو <code>notify</code>. يتلقى هذا التابع نسخة من كائن التنبيه:<syntaxhighlight lang="php">
 
 
use App\Notifications\InvoicePaid;
 
use App\Notifications\InvoicePaid;
  
سطر 38: سطر 29:
  
  
</syntaxhighlight>
+
</syntaxhighlight>'''ملاحظة''': يمكن استعمال الخاصية <code>illuminiate\Notifications\Notifiable</code> في أي نموذج. لستَ محدودًا باستعماله في النموذج <code>User</code>.
 
+
===استعمال الواجهة الساكنة Notification===
<u>ملاحظة</u>: يمكن استعمال الخاصية <code>illuminiate\Notifications\Notifiable</code> في أي نموذج. لستَ محدودًا باستعماله في النموذج <code>User</code>.
+
بدلًا من ذلك، يمكن إرسال الإشعارات عبر الواجهة الساكنة <code>Notification</code>. يكون هذا مفيدًا خاصة عندما تريد إرسال تنبيه لعدة عناصر قابلة لتلقي إشعارات (notifiable) مثل مجموعة مستخدمين. لاستخدام لواجهة الثابتة لإرسال الإشعارات، مرّر جميع العناصر القابلة لتلقّي إشعارات مع نسخة لكائن التنبيه إلى التابع <code>send</code>:<syntaxhighlight lang="php">
 
 
==== استعمال الواجهة الثابتة Notification ====
 
بدلًا من ذلك، يمكن إرسال الإشعارات عبر الواجهة الثابتة <code>Notification</code>. يكون هذا مفيدًا خاصة عندما تريد إرسال تنبيه لعدة عناصر قابلة لتلقي إشعارات (notifiable) مثل مجموعة مستخدمين. لاستخدام لواجهة الثابتة لإرسال الإشعارات، مرّر جميع العناصر القابلة لتلقّي إشعارات مع نسخة لكائن التنبيه إلى التابع <code>send</code>:
 
<syntaxhighlight lang="php">
 
 
Notification::send($users, new InvoicePaid($invoice));
 
Notification::send($users, new InvoicePaid($invoice));
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
===تحديد قنوات التوصيل===
==== تحديد قنوات التوصيل ====
 
 
يحتوي كل صنف إشعارات على التابع <code>via</code> الذي يحدد أي قناة ستُستخدم لإرسال التنبيه. من البداية، يمكن إرسال التنبيه على القنوات <code>mail</code> و <code>database</code> و <code>broadcast</code> و <code>nexmo</code> و <code>slack</code>.
 
يحتوي كل صنف إشعارات على التابع <code>via</code> الذي يحدد أي قناة ستُستخدم لإرسال التنبيه. من البداية، يمكن إرسال التنبيه على القنوات <code>mail</code> و <code>database</code> و <code>broadcast</code> و <code>nexmo</code> و <code>slack</code>.
  
<u>ملاحظة</u>: إذا أردت استخدام قنوات أخرى مثل <code>Telegram</code> أو <code>Pusher</code>، ابحث في موقع قنوات الإشعارات المدعوم  من المجموعة.
+
ملاحظة<u>:</u> إذا أردت استخدام قنوات أخرى مثل <code>Telegram</code> أو <code>Pusher</code>، ابحث في موقع قنوات الإشعارات المدعوم  من المجموعة.
  
 
يتلقّى التابع <code>via</code> نسخة الكائن <code>notifiable$</code>، وهو نسخة من الصنف الذي سيتلقّى الإشعارات. يمكن استعمال <code>notifiable$</code> لتحديد القناة الإرسال:<syntaxhighlight lang="php">
 
يتلقّى التابع <code>via</code> نسخة الكائن <code>notifiable$</code>، وهو نسخة من الصنف الذي سيتلقّى الإشعارات. يمكن استعمال <code>notifiable$</code> لتحديد القناة الإرسال:<syntaxhighlight lang="php">
سطر 68: سطر 54:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
===إضافة إشعارات لطابور الانتظار===
==== إضافة إشعارات لقائمة الإنتظار ====
+
<u>تنبيه</u>: قبل استخدام قوائم الانتظار، يجب ضبطها [https://laravel.com/docs/5.6/queues وإطلاق عامل في قائمة الإنتظار].
<u>تنبيه</u>: قبل استخدام قوائم الإنتظار، يجب ضبطها [https://laravel.com/docs/5.6/queues وإطلاق عامل في قائمة الإنتظار].  
 
  
 
قد يستغرق إرسال إشعارات بعض الوقت، خاصة إذا احتاجت قناة الإرسال إلى واجهة API خارجية. لتسريع وقت إجابة التطبيق، أعط إشعاراتك إمكانية الإضافة لقائمة انتظار بإضافة الواجهة الثابتة <code>ShouldQueue</code> أو خاصية الصنف <code>Queueable</code> لصنف التنبيه. تُحمّل الخاصية والواجهة تلقائيًا في الأصناف المُنشَأة بالأمر <code>make:notification</code> لذا يمكنك استعمالها مباشرة في الصنف:<syntaxhighlight lang="php">
 
قد يستغرق إرسال إشعارات بعض الوقت، خاصة إذا احتاجت قناة الإرسال إلى واجهة API خارجية. لتسريع وقت إجابة التطبيق، أعط إشعاراتك إمكانية الإضافة لقائمة انتظار بإضافة الواجهة الثابتة <code>ShouldQueue</code> أو خاصية الصنف <code>Queueable</code> لصنف التنبيه. تُحمّل الخاصية والواجهة تلقائيًا في الأصناف المُنشَأة بالأمر <code>make:notification</code> لذا يمكنك استعمالها مباشرة في الصنف:<syntaxhighlight lang="php">
سطر 86: سطر 71:
  
  
</syntaxhighlight>
+
</syntaxhighlight>حالما تضاف الواجهة <code>ShouldQueue</code> لصنف التنبيه، يمكنك إرسال الإشعارات كالمعتاد. سيكتشف Laravel الواجهة ويضيف تلقائيًا إرسال الإشعارات إلى قائمة انتظار:<syntaxhighlight lang="php">
 
 
حالما تضاف الواجهة <code>ShouldQueue</code> لصنف التنبيه، يمكنك إرسال الإشعارات كالمعتاد. سيكتشف Laravel الواجهة ويضيف تلقائيًا إرسال الإشعارات إلى قائمة انتظار:<syntaxhighlight lang="php">
 
 
$user->notify(new InvoicePaid($invoice));
 
$user->notify(new InvoicePaid($invoice));
</syntaxhighlight>
+
</syntaxhighlight>إذا أردت تأخير إيصال التنبيه، يمكنك إضافة التابع <code>delay</code> حين صناعة التنبيه:<syntaxhighlight lang="php">
 
 
إذا أردت تأخير إيصال التنبيه، يمكنك إضافة التابع <code>delay</code> حين صناعة التنبيه:<syntaxhighlight lang="php">
 
 
$when = now()->addMinutes(10);
 
$when = now()->addMinutes(10);
  
سطر 99: سطر 80:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
===إشعارات حسب الطلب===
==== إشعارات حسب الطلب ====
 
 
قد تحتاج في بعض الأحيان لإرسال تنبيه لشخص ليس مسّجلًا كمستخدم. باستخدام التابع <code>Notifications::route</code>، يمكنك تحديد توجيه تنبيه مخصّص قبل إرسال التوجيه:<syntaxhighlight lang="php">
 
قد تحتاج في بعض الأحيان لإرسال تنبيه لشخص ليس مسّجلًا كمستخدم. باستخدام التابع <code>Notifications::route</code>، يمكنك تحديد توجيه تنبيه مخصّص قبل إرسال التوجيه:<syntaxhighlight lang="php">
 
Notification::route('mail', 'taylor@example.com')
 
Notification::route('mail', 'taylor@example.com')
سطر 108: سطر 88:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
==إشعارات البريد الإلكتروني==
=== إشعارات البريد الإلكتروني ===
+
===تنسيق رسائل البريد===
 
 
==== تنسيق رسائل البريد ====
 
 
إذا كان التنبيه يدعم الإرسال بالبريد، يجب تعريف التابع <code>toMail</code> في صنف التنبيه. يقبل هذا التابع كائن <code>notifiable$</code> ويعيد نسخة الكائن <code>Illuminate\Notifications\Messages\MailMessage</code>. يمكن أن تحتوي رسائل سطورًا من النصوص بالإضافة مِحث لاتخاذ إجراء. لنلقِ نظرةً على مثال تابع <code>toMail</code>:<syntaxhighlight lang="php">
 
إذا كان التنبيه يدعم الإرسال بالبريد، يجب تعريف التابع <code>toMail</code> في صنف التنبيه. يقبل هذا التابع كائن <code>notifiable$</code> ويعيد نسخة الكائن <code>Illuminate\Notifications\Messages\MailMessage</code>. يمكن أن تحتوي رسائل سطورًا من النصوص بالإضافة مِحث لاتخاذ إجراء. لنلقِ نظرةً على مثال تابع <code>toMail</code>:<syntaxhighlight lang="php">
 
/**
 
/**
سطر 131: سطر 109:
  
  
</syntaxhighlight>
+
</syntaxhighlight>'''ملاحظة''': لاحظ أنّنا استعملنا <code>this->invoice->id$</code> في التابع <code>toMail</code>. يمكنك تمرير أي بيانات تحتاجها لتوليد الرسالة في التابع الباني للتنبيه.
<u>ملاحظة</u>: لاحظ أنّنا استعملنا <code>this->invoice->id$</code> في التابع <code>toMail</code>. يمكنك تمرير أي بيانات تحتاجها لتوليد الرسالة في التابع الباني للتنبيه.
 
 
 
في هذا المثال، سجّلنا تحيةً، ثم سطرًا من النص، ثم محثًا لاتخاذ إجراءٍ ما، ثمّ سطرًا آخر من النص. هذه التوابع التي يوفّرها الكائن MailMessage تجعل تنسيق بريدٍ قصيرٍ أمرًا سريعًا وبسيطًا. بعد ذلك، ستُحوّل قناة البريد المكوّنات لقالب HTML جميل ومتجاوب يحتوي على نص بسيط. في ما يلي مثال على بريد مولّد بالقناة mail:
 
[[ملف:notification-example.png|مركز|تصغير]]
 
 
 
<u>ملاحظة</u>: عند بعث إشعارات بالبريد الإلكتروني، تأكد من ضبط قيمة <code>name</code> في ملف الضبط  <code>config/app.php</code>. ستُستعمل هذه القيمة في رأس وتذييل الرسالة.
 
  
===== خيارات تنسيق إضافية =====
+
في هذا المثال، سجّلنا تحيةً، ثم سطرًا من النص، ثم محثًا لاتخاذ إجراءٍ ما، ثمّ سطرًا آخر من النص. هذه التوابع التي يوفّرها الكائن MailMessage تجعل تنسيق بريدٍ قصيرٍ أمرًا سريعًا وبسيطًا. بعد ذلك، ستُحوّل قناة البريد المكوّنات لقالب HTML جميل ومتجاوب يحتوي على نص بسيط. في ما يلي مثال على بريد مولّد بالقناة mail:[[ملف:notification-example.png|مركز|تصغير|وصلة=https://wiki.hsoub.com/%D9%85%D9%84%D9%81:notification-example.png]]'''ملاحظة''': عند بعث إشعارات بالبريد الإلكتروني، تأكد من ضبط قيمة <code>name</code> في ملف الضبط  <code>config/app.php</code>. ستُستعمل هذه القيمة في رأس وتذييل الرسالة.
بدل تعريف الأسطر في صنف التنبيه، يمكن استعمال التابع view لتحديد قالب خاص يُستعمَل لإظهار بريد التنبيه:
+
====خيارات تنسيق إضافية====
<syntaxhighlight lang="php">
+
بدل تعريف الأسطر في صنف التنبيه، يمكن استعمال التابع <code>view</code> لتحديد قالب خاص يُستعمَل لإظهار بريد التنبيه:<syntaxhighlight lang="php">
 
/**
 
/**
  
سطر 155: سطر 127:
 
   );
 
   );
  
</syntaxhighlight>
+
</syntaxhighlight>يمكن أيضًا إعادة كائن قابل للإرسال من التابع <code>toMail</code>:<syntaxhighlight lang="php">
 
 
يمكن أيضًا إعادة كائن قابل للإرسال من التابع <code>toMail</code>:<syntaxhighlight lang="php">
 
 
/**
 
/**
  
سطر 181: سطر 151:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
====رسائل الأخطاء====
===== رسائل الأخطاء =====
 
 
بعض الإشعارات تُعلم المستخدم بوقوع خطأ. مثل خطأ في دفع فاتورة. يمكنك ذكر أن رسالة التنبيه بخصوص خطأ باستعمال التابع <code>error</code> عند صنع الرسالة. عند استعمال التابع <code>error</code> على رسالة، يكون زرّ نداء العمل أحمرَ اللون بدل الأزرق:<syntaxhighlight lang="php">
 
بعض الإشعارات تُعلم المستخدم بوقوع خطأ. مثل خطأ في دفع فاتورة. يمكنك ذكر أن رسالة التنبيه بخصوص خطأ باستعمال التابع <code>error</code> عند صنع الرسالة. عند استعمال التابع <code>error</code> على رسالة، يكون زرّ نداء العمل أحمرَ اللون بدل الأزرق:<syntaxhighlight lang="php">
 
/**
 
/**
سطر 201: سطر 170:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
===تخصيص المتلقي===
==== تخصيص المتلقي ====
+
عند إرسال التنبيه عبر القناة <code>mail</code>، سيبحث نظام الإشعارات تلقائيًا عن خاصية <code>email</code> في الكائن القابل لتلقي تنبيه. يمكنك تخصيص أي عنوان إلكتروني يُستعمَل لإرسال الإشعارات بتعريف التابع  <code>routeNotifcationForMail</code>:<syntaxhighlight lang="php">
عند إرسال التنبيه عبر القناة <code>mail</code>، سيبحث نظام الإشعارات تلقائيًا عن خاصية <code>email</code> في الكائن القابل لتلقي تنبيه. يمكنك تخصيص أي عنوان إلكتروني يُستعمَل لإرسال الإشعارات بتعريف التابع  <code>routeNotifcationForMail</code>:
 
 
 
<syntaxhighlight lang="php">
 
 
<?php
 
<?php
  
سطر 229: سطر 195:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
===تخصيص الموضوع===
==== تخصيص الموضوع ====
+
في العادة، يكون موضوع الرسالة هو اسم صنف التنبيه منسّق بطريقة "كتابة العنوان". لذا إذا كان اسم صنف الإشعارات <code>InvoicePaid</code>، فسيكون الموضوع Invoice Paid. إذا أردت تحديد موضوع معيّن للرسالة، فاستعمل التابع <code>subject</code> عند بناء الرسالة:<syntaxhighlight lang="php">
في العادة، يكون موضوع الرسالة هو اسم صنف التنبيه منسّق بطريقة "كتابة العنوان". لذا إذا كان اسم صنف الإشعارات <code>InvoicePaid</code>، فسيكون الموضوع Invoice Paid. إذا أردت تحديد موضوع معيّن للرسالة، فاستعمل التابع <code>subject</code> عند بناء الرسالة:
 
<syntaxhighlight lang="php">
 
 
/**
 
/**
  
سطر 249: سطر 213:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
===تخصيص القالب===
==== تخصيص القالب ====
 
 
يمكن تغيير قالب HTML وقالب النص البسيط المستخدَم من قِبل الرسالة بنشر مصدر حزمة التنبيه.<syntaxhighlight lang="php">
 
يمكن تغيير قالب HTML وقالب النص البسيط المستخدَم من قِبل الرسالة بنشر مصدر حزمة التنبيه.<syntaxhighlight lang="php">
 
php artisan vendor:publish --tag=laravel-notifications
 
php artisan vendor:publish --tag=laravel-notifications
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
==إشعارات بريد Markdown==
=== إشعارات بريد Markdown ===
 
 
تسمح إشعارات <code>Markdown</code> بالإستفادة من القوالب المصنوعة مسبقًا في البريد الإلكتروني في حين توفّر في نفس الوقت حريّة كتابة أطول وأكثر تخصيصًا حيث تُكتب بلغة <code>Markdown</code>. يُظهر Laravel قوالب HTML بطريقة جميلة ومتجاوبة للرسائل في حين يولّد تلقائيًا نسخة مقابلة من النص البسيط.
 
تسمح إشعارات <code>Markdown</code> بالإستفادة من القوالب المصنوعة مسبقًا في البريد الإلكتروني في حين توفّر في نفس الوقت حريّة كتابة أطول وأكثر تخصيصًا حيث تُكتب بلغة <code>Markdown</code>. يُظهر Laravel قوالب HTML بطريقة جميلة ومتجاوبة للرسائل في حين يولّد تلقائيًا نسخة مقابلة من النص البسيط.
 
+
===توليد الرسالة===
==== توليد الرسالة ====
+
لتوليد تنبيه بقالب Markdown معيّن، استعمل الخيار <code>markdown--</code> مع الأمر <code>make:notification</code>:<syntaxhighlight lang="php">
لتوليد تنبيه بقالب Markdown معيّن، استعمل الخيار <code>markdown--</code> مع الأمر <code>make:notification</code>:
 
<syntaxhighlight lang="php">
 
 
php artisan make:notification InvoicePaid --markdown=mail.invoice.paid  
 
php artisan make:notification InvoicePaid --markdown=mail.invoice.paid  
</syntaxhighlight>
+
</syntaxhighlight>كبقية إشعارات البريد، يجب على الإشعارات التي تستعمل Markdown أن تُعرِّف التابع <code>toMail</code> في صنف الإشعارات. لكن بدل استعمال <code>line</code> و <code>action</code>، استعمل التابع <code>markdown</code> لتحديد اسم القالب الذي يجب استعماله:<syntaxhighlight lang="php">
كبقية إشعارات البريد، يجب على الإشعارات التي تستعمل Markdown أن تُعرِّف التابع <code>toMail</code> في صنف الإشعارات. لكن بدل استعمال <code>line</code> و <code>action</code>، استعمل التابع <code>markdown</code> لتحديد اسم القالب الذي يجب استعماله:<syntaxhighlight lang="php">
 
 
/**
 
/**
  
سطر 280: سطر 239:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
===كتابة الرسالة===
==== كتابة الرسالة ====
 
 
تستعمل إشعارات Markdown مزيجًا من مكونات Blade و نصوص Markdown مما يسمح ببناء الإشعارات بسهولة إلى جانب استعمال مكونات Laravel المصنوعة مسبقًا:<syntaxhighlight lang="php">
 
تستعمل إشعارات Markdown مزيجًا من مكونات Blade و نصوص Markdown مما يسمح ببناء الإشعارات بسهولة إلى جانب استعمال مكونات Laravel المصنوعة مسبقًا:<syntaxhighlight lang="php">
 
@component('mail::message')
 
@component('mail::message')
سطر 297: سطر 255:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
====المكوّن Button====
===== المكوّن Button =====
 
 
يُظهر المكوّن <code>button</code> زًرَّ ربط في وسط الصفحة. يقبل المكوّن معاملَين: مسار url ولونًا اختياريًا <code>color</code>. الألوان المدعومة هي <code>blue</code> و <code>green</code> و <code>red</code>. يمكنك إضافة أي عدد تريده من الأزرار:<syntaxhighlight lang="php">
 
يُظهر المكوّن <code>button</code> زًرَّ ربط في وسط الصفحة. يقبل المكوّن معاملَين: مسار url ولونًا اختياريًا <code>color</code>. الألوان المدعومة هي <code>blue</code> و <code>green</code> و <code>red</code>. يمكنك إضافة أي عدد تريده من الأزرار:<syntaxhighlight lang="php">
 
@component('mail::button', ['url' => $url, 'color' => 'green']) View Order @endcomponent
 
@component('mail::button', ['url' => $url, 'color' => 'green']) View Order @endcomponent
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
====المكوّن Panel====
===== المكوّن Panel =====
 
 
يُظهرالمكوّن <code>Panel</code> النص المُمرّر  كمجموعة بخلفية مختلفة قليلًا عن بقية الرسالة مما يسمح بجذب الانتباه لهذا النص:<syntaxhighlight lang="php">
 
يُظهرالمكوّن <code>Panel</code> النص المُمرّر  كمجموعة بخلفية مختلفة قليلًا عن بقية الرسالة مما يسمح بجذب الانتباه لهذا النص:<syntaxhighlight lang="php">
 
@component('mail::panel') This is the panel content. @endcomponent  
 
@component('mail::panel') This is the panel content. @endcomponent  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
====المكوّن Table====
===== المكوّن Table =====
+
يسمح المكوّن <code>table</code> بتحويل جدول Markdown لجدول HTML. يقبل المكوّن جدول Markdown كمحتوى ويدعم المحاذاة باستخدام نص محاذاة Markdown:<syntaxhighlight lang="text">
يسمح المكوّن <code>table</code> بتحويل جدول Markdown لجدول HTML. يقبل المكوّن جدول Markdown كمحتوى ويدعم المحاذاة باستخدام نص محاذاة Markdown:<syntaxhighlight>
 
  
 
@component('mail::table')
 
@component('mail::table')
سطر 320: سطر 275:
  
 
</syntaxhighlight>
 
</syntaxhighlight>
 
+
===تخصيص المكونات===
==== تخصيص المكونات ====
+
يمكنك تحميل كل المكونات من Markdown في التطبيق لتخصيصها. لتحويل المكونات، استعمل الأمر <code>vendor:publish</code> لنشر الأصول laravel-mail:<syntaxhighlight lang="php">
يمكنك تحميل كل المكونات من Markdown في التطبيق لتخصيصها. لتحويل المكونات، استعمل الأمر <code>vendor:publish</code> لنشر الأصول laravel-mail:
 
<syntaxhighlight lang="php">
 
 
php artisan vendor:publish --tag=laravel-mail  
 
php artisan vendor:publish --tag=laravel-mail  
 
</syntaxhighlight>سيُحمّل هذا الأمر مكونات Markdown في المجلد <code>resources/views/vendor/mail</code>. سيحتوي المجلد <code>mail</code> على مجلد <code>html</code> ومجلد <code>markdown</code>، كل منهما يحتوي على النسخة الملائمة من المكوّنات. تُستخدم المكونات في المجلد <code>html</code> لبناء نسخة <code>html</code> من الرسالة في حين تُستخدم المكونات من المجلد <code>Markdown</code> لبناء نسخة نص عادي. لك حرية تخصيص هذه المكونات كما تريد.
 
</syntaxhighlight>سيُحمّل هذا الأمر مكونات Markdown في المجلد <code>resources/views/vendor/mail</code>. سيحتوي المجلد <code>mail</code> على مجلد <code>html</code> ومجلد <code>markdown</code>، كل منهما يحتوي على النسخة الملائمة من المكوّنات. تُستخدم المكونات في المجلد <code>html</code> لبناء نسخة <code>html</code> من الرسالة في حين تُستخدم المكونات من المجلد <code>Markdown</code> لبناء نسخة نص عادي. لك حرية تخصيص هذه المكونات كما تريد.
 +
====تخصيص ال CSS====
 +
بعد تحميل المكونات، يحتوي المجلد <code>resources/views/vendor/mail/html/themes</code> على ملف <code>css</code> المبدئي <code>default.css</code>. يمكنك تخصيص تخطيط CSS في هذا الملف وسيُضمّن تلقائيًا في العرض HTML لرسائل Markdown. ملاحظة: إذا أردت بناء نمط جديد لمكون Markdown، اكتب ملف <code>CSS</code> جديد في المجلد <code>html/themes</code> وغيّر الخيار <code>theme</code> في ملف الضبط <code>mail</code>.
 +
==إشعارات قاعدة البيانات==
 +
===المتطلبات===
 +
تُخزّن قناة الإشعارات <code>database</code> معلومات الإشعارات في جدول في قاعدة البيانات. يحتوي هذا الجدول على معلومات مثل نوع التنبيه ومعلومات <code>JSON</code> خاصّة لوصف التنبيه.
  
===== تخصيص ال CSS =====
+
يمكن مساءلة الجدول لعرض الإشعارات في واجهة المستخدم لكن قبل أن تتمكن من القيام بذلك، ستحتاج لإنشاء جدول قاعدة بيانات لحمل الإشعارات. يمكن استعمال <code>notifications:table</code> لتوليد ترحيل بالمخطط المناسب:<syntaxhighlight lang="php">
بعد تحميل المكونات، يحتوي المجلد <code>resources/views/vendor/mail/html/themes</code> على ملف <code>css</code> المبدئي <code>default.css</code>. يمكنك تخصيص تخطيط CSS في هذا الملف وسيُضمّن تلقائيًا في العرض HTML لرسائل Markdown.
 
ملاحظة: إذا أردت بناء نمط جديد لمكون Markdown، اكتب ملف CSS جديد في المجلد html/themes وغيّر الخيار theme في ملف الضبط mail.
 
إشعارات قاعدة البيانات
 
المتطلبات
 
تُخزّن قناة الإشعارات database معلومات الإشعارات في جدول في قاعدة البيانات. يحتوي هذا الجدول على معلومات مثل نوع التنبيه ومعلومات JSON خاصّة لوصف التنبيه.
 
يمكن مساءلة الجدول لعرض الإشعارات في واجهة المستخدم لكن قبل أن تتمكن من القيام بذلك، ستحتاج لإنشاء جدول قاعدة بيانات لحمل الإشعارات. يمكن استعمال notifications:table لتوليد ترحيل بالمخطط المناسب:
 
 
php artisan notifications:table
 
php artisan notifications:table
 +
php artisan migrate
 +
</syntaxhighlight>
 +
===تنسيق إشعارات قواعد البيانات===
 +
إذا كان التنبيه يدعم التخزين في قاعدة البيانات، يجب تعريف أحد التابعين <code>toDatabase</code> أو <code>toArray</code> في صنف التنبيه. سيتلقى هذا التابع كائن <code>notifiable$</code> يُعيد مصفوفة PHP. تُشفّر المصفوفة المُرجعة كشيفرة <code>JSON</code> وتُخزَّن في الخانة <code>data</code> من الجدول <code>notifications</code>. فلنلق نظرة على التابع <code>toArray</code>:<syntaxhighlight lang="php">
 +
/**
 +
 +
* Get the array representation of the notification.
 +
*
 +
* @param  mixed  $notifiable
 +
* @return array
 +
*/
 +
public function toArray($notifiable) {
  
php artisan migrate
+
  return [
تنسيق إشعارات قواعد البيانات
+
      'invoice_id' => $this->invoice->id,
إذا كان التنبيه يدعم التخزين في قاعدة البيانات، يجب تعريف أحد التابعين toDatabase أو toArray في صنف التنبيه. سيتلقى هذا التابع كائن notifiable$ يُعيد مصفوفة PHP. تُشفّر المصفوفة المُرجعة كشيفرة JSON وتُخزَّن في الخانة data من الجدول notifications. فلنلق نظرة على التابع toArray:
+
      'amount' => $this->invoice->amount,
/**
+
  ];
* Get the array representation of the notification.
 
*
 
* @param  mixed  $notifiable
 
* @return array
 
*/
 
public function toArray($notifiable)
 
{
 
    return [
 
        'invoice_id' => $this->invoice->id,
 
        'amount' => $this->invoice->amount,
 
    ];
 
 
}
 
}
المقارنة بين toDatabase و toArray
+
 
يُستعمل التابع toArray أيضًا من خدمة البث (broadcast) للتثبت من أي بيانات يجب بثها لعميل JavaScript. إذا أردت استخدام مصفوفتين مختلفتين لتمثيل القناتين roadcast و database، فيجب عليك تعريف التابع toDatabase و toArray.
+
 
الوصول للإشعارات
+
</syntaxhighlight>
بعد تسجيل التنبيه في قاعدة البيانات، ستحتاج إلى طريقةٍ سهلةٍ للوصول إليها من الكائنات القابلة للتنبيه في التطبيق. الخاصية Illuminate\Notifications\Notifiable المضمّنة أصلًا في النموذج App\User تحمل علاقة Eloquent هي notifications التي تُعيد التنبيه للكائن. لإعادة الإشعارات، استخدم التابع كأي علاقة Eloquent. ستُصنَّف الإشعارات تلقائيًا حسب وقت الصنع created_at:
+
====المقارنة بين toDatabase و toArray====
 +
يُستعمل التابع <code>toArray</code> أيضًا من خدمة البث (broadcast) للتثبت من أي بيانات يجب بثها لعميل <code>JavaScript</code>. إذا أردت استخدام مصفوفتين مختلفتين لتمثيل القناتين <code>roadcast</code> و <code>database</code>، فيجب عليك تعريف التابع <code>toDatabase</code> و <code>toArray</code>.
 +
===الوصول للإشعارات===
 +
بعد تسجيل التنبيه في قاعدة البيانات، ستحتاج إلى طريقةٍ سهلةٍ للوصول إليها من الكائنات القابلة للتنبيه في التطبيق. الخاصية <code>Illuminate\Notifications\Notifiable</code> المضمّنة أصلًا في النموذج <code>App\User</code> تحمل علاقة <code>Eloquent</code> هي <code>notifications</code> التي تُعيد التنبيه للكائن. لإعادة الإشعارات، استخدم التابع كأي علاقة <code>Eloquent</code>. ستُصنَّف الإشعارات تلقائيًا حسب وقت الصنع <code>created_at</code>:<syntaxhighlight lang="php">
 
$user = App\User::find(1);
 
$user = App\User::find(1);
  
 
foreach ($user->notifications as $notification) {
 
foreach ($user->notifications as $notification) {
    echo $notification->type;
+
 
 +
  echo $notification->type;
 
}
 
}
إذا أردت إعادة الإشعارات غير المقروءة فقط، استعمل العلاقة unreadNotifications. ستُرتَّب الإشعارات هنا أيضًا حسب وقت الصنع created_at:
+
 
 +
 
 +
</syntaxhighlight>إذا أردت إعادة الإشعارات غير المقروءة فقط، استعمل العلاقة <code>unreadNotifications</code>. ستُرتَّب الإشعارات هنا أيضًا حسب وقت الصنع <code>created_at</code>:<syntaxhighlight lang="php">
 
$user = App\User::find(1);
 
$user = App\User::find(1);
  
 
foreach ($user->unreadNotifications as $notification) {
 
foreach ($user->unreadNotifications as $notification) {
    echo $notification->type;
+
 
 +
  echo $notification->type;
 
}
 
}
ملاحظة: للوصول إلى الإشعارات من عميل JavaScript، فيجب تعريف وحدة تحكم للتطبيق حيث تُعيد الإشعارات للكائنات القابلة للتنبيه ككائن المستخدم الحالي. ثم يمكنك إطلاق طلب HTTP لوحدة التحكم تلك من عميل JavaScript.
+
 
وضع إشارة مقروء على الإشعارات
+
 
في العادة، ستريد وضع علامة مقروء على التنبيه بعد أن يراه المستخدم. توفّر الخاصية Illuminate\Notifications\Notifiable التابع markAsRead الذي يحيّن الخانة read_at في قاعدة البيانات:
+
</syntaxhighlight><u>ملاحظة</u>: للوصول إلى الإشعارات من عميل JavaScript، فيجب تعريف وحدة تحكم للتطبيق حيث تُعيد الإشعارات للكائنات القابلة للتنبيه ككائن المستخدم الحالي. ثم يمكنك إطلاق طلب HTTP لوحدة التحكم تلك من عميل JavaScript.
 +
===وضع إشارة مقروء على الإشعارات===
 +
في العادة، ستريد وضع علامة مقروء على التنبيه بعد أن يراه المستخدم. توفّر الخاصية <code>Illuminate\Notifications\Notifiable</code> التابع <code>markAsRead</code> الذي يحيّن الخانة <code>read_at</code> في قاعدة البيانات:<syntaxhighlight lang="php">
 
$user = App\User::find(1);
 
$user = App\User::find(1);
  
 
foreach ($user->unreadNotifications as $notification) {
 
foreach ($user->unreadNotifications as $notification) {
    $notification->markAsRead();
+
 
 +
  $notification->markAsRead();
 
}
 
}
لكن بدل تكرار العمل على كل الإشعارات، يمكن استعمال التابع markAsRead مباشرة على مجموعة إشعارات:
+
 
 +
 
 +
</syntaxhighlight>لكن بدل تكرار العمل على كل الإشعارات، يمكن استعمال التابع <code>markAsRead</code> مباشرة على مجموعة إشعارات:<syntaxhighlight lang="php">
 
$user->unreadNotifications->markAsRead();
 
$user->unreadNotifications->markAsRead();
يمكن أيضًا طلب تحيين جماعي لوضع الإشعارات كمقروءة دون الحاجة للحصول عليها من قاعدة البيانات:
+
</syntaxhighlight>يمكن أيضًا طلب تحيين جماعي لوضع الإشعارات كمقروءة دون الحاجة للحصول عليها من قاعدة البيانات:<syntaxhighlight lang="php">
 
$user = App\User::find(1);
 
$user = App\User::find(1);
  
 
$user->unreadNotifications()->update(['read_at' => now()]);
 
$user->unreadNotifications()->update(['read_at' => now()]);
طبعًا، يمكن استعمال التابع delete لحذف التنبيه نهائيًا من قاعدة البيانات:
+
 
$user->notifications()->delete();
+
 
إشعارات البث
+
</syntaxhighlight>طبعًا، يمكن استعمال التابع <code>delete</code> لحذف التنبيه نهائيًا من قاعدة البيانات:<syntaxhighlight lang="php">
المتطلبات
+
$user->notifications()->delete();  
 +
</syntaxhighlight>
 +
==إشعارات البث==
 +
===المتطلبات===
 
قبل بث الإشعارات، يجب ضبط البث التعوّد على العمل بخدمات البث. يوفّر بث الأحداث طريقة للتفاعل مع أحداث Laravel المُطلق من جهة الخادم من عميل JavaScript.
 
قبل بث الإشعارات، يجب ضبط البث التعوّد على العمل بخدمات البث. يوفّر بث الأحداث طريقة للتفاعل مع أحداث Laravel المُطلق من جهة الخادم من عميل JavaScript.
تنسيق بث الإشعارات
+
===تنسيق بث الإشعارات===
تبث القناة broadcast الإشعارات باستعمال خدمة بث الأحداث في Laravel، مما يسمح لعميل JavaScript بالحصول على الإشعارات في وقتها الحقيقي. إذا كانت الإشعارات تدعم البث، يجب تعريف التابع toBroadcast في صنف التنبيه. يتلقى هذا التابع كائن notfifiable$ ويعيد نسخة الكائن BroadcastMessage، ترمَّز البيانات المعادة كشيفرة JSON وتُبثّ لعميل JavaScript. لنلق نظرة على مثال عن التابع toBroadcast:
+
تبث القناة <code>broadcast</code> الإشعارات باستعمال خدمة بث الأحداث في Laravel، مما يسمح لعميل JavaScript بالحصول على الإشعارات في وقتها الحقيقي. إذا كانت الإشعارات تدعم البث، يجب تعريف التابع <code>toBroadcast</code> في صنف التنبيه. يتلقى هذا التابع كائن <code>notfifiable$</code> ويعيد نسخة الكائن <code>BroadcastMessage</code>، ترمَّز البيانات المعادة كشيفرة <code>JSON</code> وتُبثّ لعميل JavaScript. لنلق نظرة على مثال عن التابع <code>toBroadcast</code>:<syntaxhighlight lang="php">
 
 
 
use Illuminate\Notifications\Messages\BroadcastMessage;
 
use Illuminate\Notifications\Messages\BroadcastMessage;
  
 
/**
 
/**
* الحصول على البث.
+
 
*
+
* الحصول على البث.
* @param  mixed  $notifiable
+
*
* @return BroadcastMessage
+
* @param  mixed  $notifiable
*/
+
* @return BroadcastMessage
public function toBroadcast($notifiable)
+
*/
{
+
public function toBroadcast($notifiable) {
    return new BroadcastMessage([
+
 
        'invoice_id' => $this->invoice->id,
+
  return new BroadcastMessage([
        'amount' => $this->invoice->amount,
+
      'invoice_id' => $this->invoice->id,
    ]);
+
      'amount' => $this->invoice->amount,
 +
  ]);
 
}
 
}
ضبط قائمة انتظار البث
+
 
توجد كل الإشعارات المعدة للبث في قائمة انتظار البث. إذا أردت ضبط اسم أو وصلة القائمة المستخدمة مع بث الإشعارات، استخدم التابعين onConnection و onQueue من BroadcastMessage:
+
 
return (new BroadcastMessage($data))
+
</syntaxhighlight>
                ->onConnection('sqs')
+
====ضبط طابور انتظار البث====
                ->onQueue('broadcasts');
+
توجد كل الإشعارات المعدة للبث في طابور انتظار البث. إذا أردت ضبط اسم أو وصلة الطابور المستخدم مع بث الإشعارات، استخدم التابعين <code>onConnection</code> و <code>onQueue</code> من<syntaxhighlight lang="php">
ملاحظة: بالإضافة للبيانات التي تُحدّدها، يحتوي بث الإشعارات أيضًا على خانة type تحتوي اسم صنف التنبيه.
+
BroadcastMessage: return (new BroadcastMessage($data))
الاستماع للإشعارات
+
 
تُبث الإشعارات على قناة خاصة منسقة بطريقة {notifiable}.{id}. لذا، إذا أرسلتَ تنبيهًا للنموذج App\User مع ID يساوي 1، سيُبث التنبيه على القناة الخاصة App.User.1. عند استعمال Laravel echo يمكنك الاستماع بسهولة لأي قناة باستعمال التابع المساعد notification:
+
              ->onConnection('sqs')
 +
              ->onQueue('broadcasts');
 +
 
 +
</syntaxhighlight>'''ملاحظة:''' بالإضافة للبيانات التي تُحدّدها، يحتوي بث الإشعارات أيضًا على خانة type تحتوي اسم صنف التنبيه.
 +
===الاستماع للإشعارات===
 +
تُبث الإشعارات على قناة خاصة منسقة بطريقة <code>{notifiable}.{id}</code>. لذا، إذا أرسلتَ تنبيهًا للنموذج <code>App\User</code> مع ID يساوي 1، سيُبث التنبيه على القناة الخاصة <code>App.User.1</code>. عند استعمال Laravel echo يمكنك الاستماع بسهولة لأي قناة باستعمال التابع المساعد <code>notification</code>:<syntaxhighlight lang="php">
 
Echo.private('App.User.' + userId)
 
Echo.private('App.User.' + userId)
    .notification((notification) => {
+
 
        console.log(notification.type);
+
  .notification((notification) => {
    });
+
      console.log(notification.type);
تخصيص قنوات التنبيه
+
  });
إذا أردت اختيار القناة التي تتلقى عليها بث الإشعارات، يمكنك تعريف التابع receivesBroadcastNotificationsOn للكائن المتلقي للإشعارات:
+
 
 +
</syntaxhighlight>
 +
====تخصيص قنوات الإشعار====
 +
إذا أردت اختيار القناة التي تتلقى عليها بث الإشعارات، يمكنك تعريف التابع <code>receivesBroadcastNotificationsOn</code> للكائن المتلقي للإشعارات:<syntaxhighlight lang="php">
 
<?php
 
<?php
  
 
namespace App;
 
namespace App;
  
use Illuminate\Notifications\Notifiable;
+
use Illuminate\Notifications\Notifiable; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Broadcasting\PrivateChannel;
 
use Illuminate\Foundation\Auth\User as Authenticatable;
 
  
class User extends Authenticatable
+
class User extends Authenticatable {
{
 
    use Notifiable;
 
  
    /**
+
  use Notifiable;
    * The channels the user receives notification broadcasts on.
+
  /**
    *
+
    * The channels the user receives notification broadcasts on.
    * @return string
+
    *
    */
+
    * @return string
    public function receivesBroadcastNotificationsOn()
+
    */
    {
+
  public function receivesBroadcastNotificationsOn()
        return 'users.'.$this->id;
+
  {
    }
+
      return 'users.'.$this->id;
 +
  }
 
}
 
}
إشعارات الرسائل القصيرة SMS
 
المتطلبات
 
خدمة إرسال إشعارات SMS مدعومة من قبل Nexmo. قبل أن تتمكن من إرسال الإشعارات، يجب أن تُثبت الحزمة nexmo/client وأن تضيف خيارات الضبط لملف الضبط config/services.php. يمكنك نسخ مثال الضبط التالي للبدء:
 
  
 +
 +
</syntaxhighlight>
 +
==إشعارات الرسائل القصيرة SMS==
 +
===المتطلبات===
 +
خدمة إرسال إشعارات SMS مدعومة من قبل <code>Nexmo</code>. قبل أن تتمكن من إرسال الإشعارات، يجب أن تُثبت الحزمة <code>nexmo/client</code> وأن تضيف خيارات الضبط لملف الضبط <code>config/services.php</code>. يمكنك نسخ مثال الضبط التالي للبدء:<syntaxhighlight lang="php">
 
'nexmo' => [
 
'nexmo' => [
    'key' => env('NEXMO_KEY'),
+
 
    'secret' => env('NEXMO_SECRET'),
+
  'key' => env('NEXMO_KEY'),
    'sms_from' => '15556666666',
+
  'secret' => env('NEXMO_SECRET'),
 +
  'sms_from' => '15556666666',
 
],
 
],
الخيار sms_from هو رقم الهاتف الذي ستُرسل منه رسالة التنبيه. يجب عليك إنشاء رقم هاتف لتطبيقك من لوحة تحكم Nexmo.
+
 
تنسيق إشعارات SMS
+
 
إذا كان التنبيه يدعم الإرسال عبر رسالة SMS، يجب تعريف التابع toNexmo في صنف التنبيه. سيتلقى التابع كائن notifiable$ ويعيد نسخة الكائن Illuminate\Notifications\Messages\NexmoMessage:
+
</syntaxhighlight>الخيار <code>sms_from</code> هو رقم الهاتف الذي ستُرسل منه رسالة التنبيه. يجب عليك إنشاء رقم هاتف لتطبيقك من لوحة تحكم Nexmo.
 +
===تنسيق إشعارات SMS===
 +
إذا كان التنبيه يدعم الإرسال عبر رسالة SMS، يجب تعريف التابع <code>toNexmo</code> في صنف التنبيه. سيتلقى التابع كائن <code>notifiable$</code> ويعيد نسخة الكائن <code>Illuminate\Notifications\Messages\NexmoMessage</code>:<syntaxhighlight lang="php">
 
/**
 
/**
* Get the Nexmo / SMS representation of the notification.
+
 
*
+
* Get the Nexmo / SMS representation of the notification.
* @param  mixed  $notifiable
+
*
* @return NexmoMessage
+
* @param  mixed  $notifiable
*/
+
* @return NexmoMessage
public function toNexmo($notifiable)
+
*/
{
+
public function toNexmo($notifiable) {
    return (new NexmoMessage)
+
 
                ->content('Your SMS message content');
+
  return (new NexmoMessage)
 +
              ->content('Your SMS message content');
 
}
 
}
محتوى Unicode
+
 
إذا الرسالة تحتوي رموز unicode، فيجب استدعاء التابع unicode عند بناء الرسالة NexmoMessage:
+
 
 +
</syntaxhighlight>
 +
====محتوى Unicode====
 +
إذا الرسالة تحتوي رموز unicode، فيجب استدعاء التابع <code>unicode</code> عند بناء الرسالة NexmoMessage:<syntaxhighlight lang="php">
 
/**
 
/**
* Get the Nexmo / SMS representation of the notification.
+
 
*
+
* Get the Nexmo / SMS representation of the notification.
* @param  mixed  $notifiable
+
*
* @return NexmoMessage
+
* @param  mixed  $notifiable
*/
+
* @return NexmoMessage
public function toNexmo($notifiable)
+
*/
{
+
public function toNexmo($notifiable) {
    return (new NexmoMessage)
+
 
                ->content('Your unicode message')
+
  return (new NexmoMessage)
                ->unicode();
+
              ->content('Your unicode message')
 +
              ->unicode();
 
}
 
}
تخصيص الرقم "from"
+
 
إن أردت إرسال تنبيه من رقم غير الرقم المُعرَف في config/services.php، استعمل التابع from في NexmoMessage:
+
 
 +
</syntaxhighlight>
 +
===تخصيص الرقم "from"===
 +
إن أردت إرسال تنبيه من رقم غير الرقم المُعرَف في <code>config/services.php</code>، استعمل التابع <code>from</code> في NexmoMessage:<syntaxhighlight lang="php">
 
/**
 
/**
* Get the Nexmo / SMS representation of the notification.
+
 
*
+
* Get the Nexmo / SMS representation of the notification.
* @param  mixed  $notifiable
+
*
* @return NexmoMessage
+
* @param  mixed  $notifiable
*/
+
* @return NexmoMessage
public function toNexmo($notifiable)
+
*/
{
+
public function toNexmo($notifiable) {
    return (new NexmoMessage)
+
 
                ->content('Your SMS message content')
+
  return (new NexmoMessage)
                ->from('15554443333');
+
              ->content('Your SMS message content')
 +
              ->from('15554443333');
 
}
 
}
توجيه إشعارات SMS
+
 
عند إرسال إشعارات عبر قناة nexmo، سيبحث نظام الإشعارات تلقائيًا عن الخاصية phone_number في الكائن المنبَه. إن أردت تخصيص رقم هاتف يُرسَل إليه التنبيه، عرِّف التابع routeNotificationForNexmo في الكائن:
+
 
 +
</syntaxhighlight>
 +
===توجيه إشعارات SMS===
 +
عند إرسال إشعارات عبر قناة <code>nexmo</code>، سيبحث نظام الإشعارات تلقائيًا عن الخاصية <code>phone_number</code> في الكائن المنبَه. إن أردت تخصيص رقم هاتف يُرسَل إليه التنبيه، عرِّف التابع <code>routeNotificationForNexmo</code> في الكائن:<syntaxhighlight lang="php">
 
<?php
 
<?php
  
 
namespace App;
 
namespace App;
  
use Illuminate\Notifications\Notifiable;
+
use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Foundation\Auth\User as Authenticatable;
 
  
class User extends Authenticatable
+
class User extends Authenticatable {
{
 
    use Notifiable;
 
  
    /**
+
  use Notifiable;
    * Route notifications for the Nexmo channel.
+
  /**
    *
+
    * Route notifications for the Nexmo channel.
    * @param  \Illuminate\Notifications\Notification  $notification
+
    *
    * @return string
+
    * @param  \Illuminate\Notifications\Notification  $notification
    */
+
    * @return string
    public function routeNotificationForNexmo($notification)
+
    */
    {
+
  public function routeNotificationForNexmo($notification)
        return $this->phone;
+
  {
    }
+
      return $this->phone;
 +
  }
 
}
 
}
إشعارات Slack
+
 
المتطلبات
+
 
قبل أن تتمكن من إرسال إشعارات عبر Slack، يجب عليك تثبيت المكتبة Guzzle HTTP عبر Composer:
+
</syntaxhighlight>
composer require guzzlehttp/guzzle
+
==إشعارات Slack==
ستحتاج أيضًا إلى ضبط "Incoming Webhook" لفريق Slack. سيُوفر هذا الضبط مسار URL يمكنك استعماله عند إرسال إشعارات Slack.
+
===المتطلبات===
تنسيق إشعارات slack
+
قبل أن تتمكن من إرسال إشعارات عبر Slack، يجب عليك تثبيت المكتبة Guzzle HTTP عبر <code>Composer</code>:<syntaxhighlight lang="php">
إن كان التنبيه يدعم الإرسال عبر Slack، يجب تعريف التابع toSlack في صنف التنبيه. سيتلقى التابع كائن notifiable$ ويعيد نسخة الكائن Illuminate\Notifications\Messages\SlackMessage. يمكن أن تحتوي إشعارات Slack على نصوص بالإضافة لمرفقات تنسق نصوصًا إضافية أو مصفوفة من الخانات. لنلق نظرة على مثال toSlack أساسي:
+
composer require guzzlehttp/guzzle  
 +
</syntaxhighlight>ستحتاج أيضًا إلى ضبط "Incoming Webhook" لفريق Slack. سيُوفر هذا الضبط مسار URL يمكنك استعماله عند إرسال إشعارات Slack.
 +
===تنسيق إشعارات slack===
 +
إن كان التنبيه يدعم الإرسال عبر Slack، يجب تعريف التابع <code>toSlack</code> في صنف التنبيه. سيتلقى التابع كائن <code>notifiable$</code> ويعيد نسخة الكائن <code>Illuminate\Notifications\Messages\SlackMessage</code>. يمكن أن تحتوي إشعارات Slack على نصوص بالإضافة لمرفقات تنسق نصوصًا إضافية أو مصفوفة من الخانات. لنلق نظرة على مثال toSlack أساسي:<syntaxhighlight lang="php">
 
/**
 
/**
* Get the Slack representation of the notification.
+
 
*
+
* Get the Slack representation of the notification.
* @param  mixed  $notifiable
+
*
* @return SlackMessage
+
* @param  mixed  $notifiable
*/
+
* @return SlackMessage
public function toSlack($notifiable)
+
*/
{
+
public function toSlack($notifiable) {
    return (new SlackMessage)
+
 
                ->content('One of your invoices has been paid!');
+
  return (new SlackMessage)
 +
              ->content('One of your invoices has been paid!');
 
}
 
}
في هذا المثال، أرسلنا فقط سطر نص مما سينتج تنبيها كالآتي:
 
  
  
تخصيص المرسل و المتلقي
+
</syntaxhighlight>في هذا المثال، أرسلنا فقط سطر نص مما سينتج تنبيها كالآتي:[[ملف:basic-slack-notification.png|مركز|تصغير|وصلة=https://wiki.hsoub.com/%D9%85%D9%84%D9%81:basic-slack-notification.png]]
يمكن استعمال التابعين from و to لتخصيص مرسل ومتلق للتنبيه. يقبل التابع from اسم مستخدم أو معرف emoji بينما يقبل التابع to قناة إرسال أو اسم مستخدم:
+
====تخصيص المرسل و المتلقي====
 +
يمكن استعمال التابعين <code>from</code> و <code>to</code> لتخصيص مرسل ومتلق للتنبيه. يقبل التابع <code>from</code> اسم مستخدم أو معرف emoji بينما يقبل التابع <code>to</code> قناة إرسال أو اسم مستخدم:<syntaxhighlight lang="php">
 
/**
 
/**
* Get the Slack representation of the notification.
+
 
*
+
* Get the Slack representation of the notification.
* @param  mixed  $notifiable
+
*
* @return SlackMessage
+
* @param  mixed  $notifiable
*/
+
* @return SlackMessage
public function toSlack($notifiable)
+
*/
{
+
public function toSlack($notifiable) {
    return (new SlackMessage)
+
 
                ->from('Ghost', ':ghost:')
+
  return (new SlackMessage)
                ->to('#other')
+
              ->from('Ghost', ':ghost:')
                ->content('This will be sent to #other');
+
              ->to('#other')
 +
              ->content('This will be sent to #other');
 
}
 
}
يمكنك أيضًا استعمال صورة بدل وجه emoji:
+
 
 +
 
 +
</syntaxhighlight>يمكنك أيضًا استعمال صورة بدل وجه emoji:<syntaxhighlight lang="php">
 
/**
 
/**
* Get the Slack representation of the notification.
+
 
*
+
* Get the Slack representation of the notification.
* @param  mixed  $notifiable
+
*
* @return SlackMessage
+
* @param  mixed  $notifiable
*/
+
* @return SlackMessage
public function toSlack($notifiable)
+
*/
{
+
public function toSlack($notifiable) {
    return (new SlackMessage)
+
 
                ->from('Laravel')
+
  return (new SlackMessage)
                ->image('https://laravel.com/favicon.png')
+
              ->from('Laravel')
                ->content('This will display the Laravel logo next to the message');
+
              ->image('https://laravel.com/favicon.png')
 +
              ->content('This will display the Laravel logo next to the message');
 
}
 
}
مرفقات Slack
+
 
يمكنك إضافة مرفقات لإشعارات Slack، توفر المرفقات خيارات تنسيق أكثر من النص العادي. في هذا المثال، سنرسل تنبيه خطأ عن استثناء حصل في التطبيق مع رابط لعرض معلومات إضافية عن الاستثناء:
+
 
 +
</syntaxhighlight>
 +
===مرفقات Slack===
 +
يمكنك إضافة مرفقات لإشعارات Slack، توفر المرفقات خيارات تنسيق أكثر من النص العادي. في هذا المثال، سنرسل تنبيه خطأ عن استثناء حصل في التطبيق مع رابط لعرض معلومات إضافية عن الاستثناء:<syntaxhighlight lang="php">
 
/**
 
/**
* Get the Slack representation of the notification.
 
*
 
* @param  mixed  $notifiable
 
* @return SlackMessage
 
*/
 
public function toSlack($notifiable)
 
{
 
    $url = url('/exceptions/'.$this->exception->id);
 
  
    return (new SlackMessage)
+
* Get the Slack representation of the notification.
                ->error()
+
*
                ->content('Whoops! Something went wrong.')
+
* @param  mixed  $notifiable
                ->attachment(function ($attachment) use ($url) {
+
* @return SlackMessage
                    $attachment->title('Exception: File Not Found', $url)
+
*/
                              ->content('File [background.jpg] was not found.');
+
public function toSlack($notifiable) {
                });
+
 
 +
  $url = url('/exceptions/'.$this->exception->id);
 +
  return (new SlackMessage)
 +
              ->error()
 +
              ->content('Whoops! Something went wrong.')
 +
              ->attachment(function ($attachment) use ($url) {
 +
                  $attachment->title('Exception: File Not Found', $url)
 +
                              ->content('File [background.jpg] was not found.');
 +
              });
 
}
 
}
سيُنتج المثال السابق رسالة Slack مشابهة للآتي
 
  
  
تسمح المرفقات أيضا بتحديد مصفوفة بيانات تُقدم للمستخدم. ستقدم البيانات في شكل جدول لتسهيل القراءة:
+
</syntaxhighlight>سيُنتج المثال السابق رسالة Slack مشابهة للآتي[[ملف:basic-slack-attachment.png|مركز|تصغير|وصلة=https://wiki.hsoub.com/%D9%85%D9%84%D9%81:basic-slack-attachment.png]]تسمح المرفقات أيضا بتحديد مصفوفة بيانات تُقدم للمستخدم. ستقدم البيانات في شكل جدول لتسهيل القراءة:<syntaxhighlight lang="php">
 
/**
 
/**
* Get the Slack representation of the notification.
+
 
*
+
* Get the Slack representation of the notification.
* @param  mixed  $notifiable
+
*
* @return SlackMessage
+
* @param  mixed  $notifiable
*/
+
* @return SlackMessage
 +
*/
 
public function toSlack($notifiable)
 
public function toSlack($notifiable)
 +
 
{
 
{
    $url = url('/invoices/'.$this->invoice->id);
 
  
    return (new SlackMessage)
+
  $url = url('/invoices/'.$this->invoice->id);
                ->success()
+
  return (new SlackMessage)
                ->content('One of your invoices has been paid!')
+
              ->success()
                ->attachment(function ($attachment) use ($url) {
+
              ->content('One of your invoices has been paid!')
                    $attachment->title('Invoice 1322', $url)
+
              ->attachment(function ($attachment) use ($url) {
                              ->fields([
+
                  $attachment->title('Invoice 1322', $url)
                                    'Title' => 'Server Expenses',
+
                              ->fields([
                                    'Amount' => '$1,234',
+
                                  'Title' => 'Server Expenses',
                                    'Via' => 'American Express',
+
                                  'Amount' => '$1,234',
                                    'Was Overdue' => ':-1:',
+
                                  'Via' => 'American Express',
                                ]);
+
                                  'Was Overdue' => ':-1:',
                });
+
                              ]);
 +
              });
 
}
 
}
سينتج المثال السابق رسالة Slack تشبه ما يلي:
 
  
محتوى المرفقات Markdown
+
 
إن كانت بعض حقول المرفقات تحتوي Markdown، استعمل التابع markdown لإعلام Slack عن طريقة قراءة وعرض محتوى الحقل. القيم التي يقبلها التابع هي pretext أو text أو fields. لمزيد من المعلومات عن تنسيق مرفقات Slack راجع توثيق Slack API:
+
</syntaxhighlight>سينتج المثال السابق رسالة Slack تشبه ما يلي:[[ملف:slack-fields-attachment.png|مركز|تصغير|وصلة=https://wiki.hsoub.com/%D9%85%D9%84%D9%81:slack-fields-attachment.png]]
 +
====محتوى المرفقات Markdown====
 +
إن كانت بعض حقول المرفقات تحتوي Markdown، استعمل التابع markdown لإعلام Slack عن طريقة قراءة وعرض محتوى الحقل. القيم التي يقبلها التابع هي <code>pretext</code> أو <code>text</code> أو <code>fields</code>. لمزيد من المعلومات عن تنسيق مرفقات Slack راجع توثيق [https://api.slack.com/docs/message-formatting#message_formatting Slack API]:<syntaxhighlight lang="php">
 
/**
 
/**
* Get the Slack representation of the notification.
 
*
 
* @param  mixed  $notifiable
 
* @return SlackMessage
 
*/
 
public function toSlack($notifiable)
 
{
 
    $url = url('/exceptions/'.$this->exception->id);
 
  
    return (new SlackMessage)
+
* Get the Slack representation of the notification.
                ->error()
+
*
                ->content('Whoops! Something went wrong.')
+
* @param  mixed  $notifiable
                ->attachment(function ($attachment) use ($url) {
+
* @return SlackMessage
                    $attachment->title('Exception: File Not Found', $url)
+
*/
                              ->content('File [background.jpg] was *not found*.')
+
public function toSlack($notifiable) {
                              ->markdown(['text']);
+
 
                });
+
  $url = url('/exceptions/'.$this->exception->id);
 +
  return (new SlackMessage)
 +
              ->error()
 +
              ->content('Whoops! Something went wrong.')
 +
              ->attachment(function ($attachment) use ($url) {
 +
                  $attachment->title('Exception: File Not Found', $url)
 +
                              ->content('File [background.jpg] was *not found*.')
 +
                              ->markdown(['text']);
 +
              });
 
}
 
}
  
  
توجيه إشعارات slack
+
 
لتوجيه إشعارات Slack لأماكنها الصحيحة، عرف التابع routeNotificationForSlack في الكائن المنَبه. سيعيد التابع مسار Webhook URL يجب أن تُرسل الإشعارات إليه. يمكن الحصول على المسار أيضًا عبر خدمة "Incoming Webhook" في فريق Slack:
+
 
 +
</syntaxhighlight>
 +
===توجيه إشعارات slack===
 +
لتوجيه إشعارات Slack لأماكنها الصحيحة، عرف التابع <code>routeNotificationForSlack</code> في الكائن المنَبه. سيعيد التابع مسار Webhook URL يجب أن تُرسل الإشعارات إليه. يمكن الحصول على المسار أيضًا عبر خدمة "Incoming Webhook" في فريق Slack:<syntaxhighlight lang="php">
 
<?php
 
<?php
 +
 
namespace App;
 
namespace App;
  
use Illuminate\Notifications\Notifiable;
+
use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Foundation\Auth\User as Authenticatable;
 
  
class User extends Authenticatable
+
class User extends Authenticatable {
{
 
    use Notifiable;
 
  
    /**
+
  use Notifiable;
    * Route notifications for the Slack channel.
+
  /**
    *
+
    * Route notifications for the Slack channel.
    * @param  \Illuminate\Notifications\Notification  $notification
+
    *
    * @return string
+
    * @param  \Illuminate\Notifications\Notification  $notification
    */
+
    * @return string
    public function routeNotificationForSlack($notification)
+
    */
    {
+
  public function routeNotificationForSlack($notification)
        return $this->slack_webhook_url;
+
  {
    }
+
      return $this->slack_webhook_url;
 +
  }
 
}
 
}
أحداث الإشعارات
+
 
عندما يُرسَل تنبيه ما، يُطلِق نظام الإشعارات الحدث Illuminate\Notifications\Events\NotificationSent. يحتوي الحدث على الكائن المنبَه ونسخة التنبيه نفسه. يمكنك تسجيل مستمعات على هذا الحدث في EventServiceProvider:
+
 
 +
</syntaxhighlight>
 +
==أحداث الإشعارات==
 +
عندما يُرسَل تنبيه ما، يُطلِق نظام الإشعارات الحدث <code>Illuminate\Notifications\Events\NotificationSent</code>. يحتوي الحدث على الكائن المنبَه ونسخة التنبيه نفسه. يمكنك تسجيل مستمعات على هذا الحدث في <code>EventServiceProvider</code>:<syntaxhighlight lang="php">
 
/**
 
/**
* The event listener mappings for the application.
+
 
*
+
* The event listener mappings for the application.
* @var array
+
*
*/
+
* @var array
 +
*/
 
protected $listen = [
 
protected $listen = [
    'Illuminate\Notifications\Events\NotificationSent' => [
+
 
        'App\Listeners\LogNotification',
+
  'Illuminate\Notifications\Events\NotificationSent' => [
    ],ا
+
      'App\Listeners\LogNotification',
 +
  ],ا
 
];
 
];
ملاحظة: بعد تسجيل المستمع في EventServiceProvider، استعمل الأمر event:generate لإنشاء أصناف المستمعات بسرعة.
+
 
في صنف المستمع، يمكنك الوصول إلى الخاصيات notifiable و notification و channel للحدث للمعرفة معلومات عن المتلقي وعن التنبيه:
+
 
 +
</syntaxhighlight>'''ملاحظة:''' بعد تسجيل المستمع في <code>EventServiceProvider</code>، استعمل الأمر <code>event:generate</code> لإنشاء أصناف المستمعات بسرعة.
 +
 
 +
في صنف المستمع، يمكنك الوصول إلى الخاصيات <code>notifiable</code> و <code>notification</code> و <code>channel</code> للحدث للمعرفة معلومات عن المتلقي وعن التنبيه:<syntaxhighlight lang="php">
 
/**
 
/**
* Handle the event.
+
 
*
+
* Handle the event.
* @param  NotificationSent  $event
+
*
* @return void
+
* @param  NotificationSent  $event
*/
+
* @return void
public function handle(NotificationSent $event)
+
*/
{
+
public function handle(NotificationSent $event) {
  // $event->channel
+
 
  // $event->notifiable
+
  // $event->channel
  // $event->notification
+
  // $event->notifiable
 +
  // $event->notification
 
}
 
}
القنوات الخاصة
+
 
يأتي Laravel مع عدد من قنوات إرسال الإشعارات، لكن قد تريد كتابة مشغلك الخاص لإرسال الإشعارات عبر قنوات أخرى. يجعل Laravel هذا سهلا. للبدء، عرف صنفا يحتوي التابع send. يجب أن يقبل هذا التابع معاملين notifiable$ و notification$:
+
 
 +
</syntaxhighlight>
 +
==القنوات المخصصة==
 +
يأتي [[Laravel]] مع عدد من قنوات إرسال الإشعارات، لكن قد تريد كتابة مشغلك الخاص لإرسال الإشعارات عبر قنوات أخرى. يجعل [[Laravel]] هذا سهلا. للبدء، عرف صنفا يحتوي التابع <code>send</code>. يجب أن يقبل هذا التابع معاملين <code>notifiable$</code> و <code>notification$</code>:<syntaxhighlight lang="php">
 
<?php
 
<?php
  
سطر 694: سطر 709:
 
use Illuminate\Notifications\Notification;
 
use Illuminate\Notifications\Notification;
  
class VoiceChannel
+
class VoiceChannel {
{
 
    /**
 
    * Send the given notification.
 
    *
 
    * @param  mixed  $notifiable
 
    * @param  \Illuminate\Notifications\Notification  $notification
 
    * @return void
 
    */
 
    public function send($notifiable, Notification $notification)
 
    {
 
        $message = $notification->toVoice($notifiable);
 
  
       // Send notification to the $notifiable instance...
+
  /**
    }
+
    * Send the given notification.
 +
    *
 +
    * @param  mixed  $notifiable
 +
    * @param  \Illuminate\Notifications\Notification  $notification
 +
    * @return void
 +
    */
 +
  public function send($notifiable, Notification $notification)
 +
  {
 +
       $message = $notification->toVoice($notifiable);
 +
      // Send notification to the $notifiable instance...
 +
  }
 
}
 
}
بعد تعريف قناة التنبيه، يمكنك إعادة اسم الصنف من التابع via من أي تنبيه:
+
 
 +
 
 +
</syntaxhighlight>بعد تعريف قناة التنبيه، يمكنك إعادة اسم الصنف من التابع <code>via</code> من أي تنبيه:<syntaxhighlight lang="php">
 
<?php
 
<?php
  
 
namespace App\Notifications;
 
namespace App\Notifications;
  
use Illuminate\Bus\Queueable;
+
use Illuminate\Bus\Queueable; use App\Channels\VoiceChannel; use App\Channels\Messages\VoiceMessage; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue;
use App\Channels\VoiceChannel;
+
 
use App\Channels\Messages\VoiceMessage;
+
class InvoicePaid extends Notification {
use Illuminate\Notifications\Notification;
 
use Illuminate\Contracts\Queue\ShouldQueue;
 
  
class InvoicePaid extends Notification
+
  use Queueable;
{
+
  /**
    use Queueable;
+
    * Get the notification channels.
 +
    *
 +
    * @param  mixed  $notifiable
 +
    * @return array|string
 +
    */
 +
  public function via($notifiable)
 +
  {
 +
      return [VoiceChannel::class];
 +
  }
 +
  /**
 +
    * Get the voice representation of the notification.
 +
    *
 +
    * @param  mixed  $notifiable
 +
    * @return VoiceMessage
 +
    */
 +
  public function toVoice($notifiable)
 +
  {
 +
      // ...
 +
  }
 +
}
  
    /**
 
    * Get the notification channels.
 
    *
 
    * @param  mixed  $notifiable
 
    * @return array|string
 
    */
 
    public function via($notifiable)
 
    {
 
        return [VoiceChannel::class];
 
    }
 
  
    /**
+
</syntaxhighlight>
    * Get the voice representation of the notification.
+
==مصادر==
    *
+
*[https://laravel.com/docs/5.6/notifications صفحة Notifications في توثيق Laravel الرسمي]
    * @param  mixed  $notifiable
+
[[تصنيف:Laravel|{{SUBPAGENAME}}]]
    * @return VoiceMessage
+
[[تصنيف:Laravel Digging deeper|{{SUBPAGENAME}}]]
    */
 
    public function toVoice($notifiable)
 
    {
 
      // ...
 
    }
 
}
 

المراجعة الحالية بتاريخ 14:09، 24 أكتوبر 2018

مقدمة

بالإضافة لدعم إرسال البريد الإلكتروني، يوفّر Laravel دعمًا لإرسال إشعارات بين قنوات توصيل عديدة من ضمنها البريد الإكتروني، والرسائل القصيرة (عبر Nexmo)، و Slack. يمكن أيضًا حفظ الإشعارات في قاعدة البيانات لإظهارها في واجهة الويب.

تكون الإشعارات في العادة على شكل رسائل قصيرة، وغنية بالمعلومات، تنبّه المستخدم لشيءٍ حدث في التطبيق. مثلًا، إن كنت تكتب تطبيق فواتير، يمكنك إرسال تنبيه "تمّ خلاص الفاتورة" للمستخدم عبر بريد إلكتروني أو رسالة قصيرة.

إنشاء الإشعارات

في Laravel، يُمثَّل كل تنبيه بصنف (موجودٌ عادةً في المجلد app/Notifications). لا تقلق إذا لم تجد المجلد، إذ سيُنشَأ عند تنفيذ الأمر make:notification:

Php artisan make:notification InvoicePaid

سيُنشِئ هذا الأمر صنفًا جديدًا في المجلد app/Notifications. يحتوي كل صنف إشعارات على التابع via وعدد من التوابع لكتابة نص التنبيه (مثل toMail أو toDatabase) التي تحوّل التنبيه لرسالة ملائمة للقناة التي ستُرسَل عليها.

إرسال الإشعارات

استعمال الخاصية Notifiable

يمكن إرسال الإشعارات بطريقتين: باستعمال التبع notify من صنف الخاصية Notifiable أو باستعمال الواجهة الثابتة Notification. لنبدأ أولًا باستعمال الخاصية:

<?php

namespace App;

use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable {

   use Notifiable;
}

تُستعمل هذه الخاصية تلقائيا في النموذج App/User وتحتوي تابعًا واحدًا يُستعمل لإرسال الإشعارات وهو notify. يتلقى هذا التابع نسخة من كائن التنبيه:

use App\Notifications\InvoicePaid;

$user->notify(new InvoicePaid($invoice));

ملاحظة: يمكن استعمال الخاصية illuminiate\Notifications\Notifiable في أي نموذج. لستَ محدودًا باستعماله في النموذج User.

استعمال الواجهة الساكنة Notification

بدلًا من ذلك، يمكن إرسال الإشعارات عبر الواجهة الساكنة Notification. يكون هذا مفيدًا خاصة عندما تريد إرسال تنبيه لعدة عناصر قابلة لتلقي إشعارات (notifiable) مثل مجموعة مستخدمين. لاستخدام لواجهة الثابتة لإرسال الإشعارات، مرّر جميع العناصر القابلة لتلقّي إشعارات مع نسخة لكائن التنبيه إلى التابع send:

Notification::send($users, new InvoicePaid($invoice));

تحديد قنوات التوصيل

يحتوي كل صنف إشعارات على التابع via الذي يحدد أي قناة ستُستخدم لإرسال التنبيه. من البداية، يمكن إرسال التنبيه على القنوات mail و database و broadcast و nexmo و slack.

ملاحظة: إذا أردت استخدام قنوات أخرى مثل Telegram أو Pusher، ابحث في موقع قنوات الإشعارات المدعوم من المجموعة.

يتلقّى التابع via نسخة الكائن notifiable$، وهو نسخة من الصنف الذي سيتلقّى الإشعارات. يمكن استعمال notifiable$ لتحديد القناة الإرسال:

/**

* الحصول على قنوات الإرسال.
*
* @param  mixed  $notifiable
* @return array
*/
public function via($notifiable) {

   return $notifiable->prefers_sms ? ['nexmo'] : ['mail', 'database'];
}

إضافة إشعارات لطابور الانتظار

تنبيه: قبل استخدام قوائم الانتظار، يجب ضبطها وإطلاق عامل في قائمة الإنتظار.

قد يستغرق إرسال إشعارات بعض الوقت، خاصة إذا احتاجت قناة الإرسال إلى واجهة API خارجية. لتسريع وقت إجابة التطبيق، أعط إشعاراتك إمكانية الإضافة لقائمة انتظار بإضافة الواجهة الثابتة ShouldQueue أو خاصية الصنف Queueable لصنف التنبيه. تُحمّل الخاصية والواجهة تلقائيًا في الأصناف المُنشَأة بالأمر make:notification لذا يمكنك استعمالها مباشرة في الصنف:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue;

class InvoicePaid extends Notification implements ShouldQueue {

   use Queueable;
  // ...
}

حالما تضاف الواجهة ShouldQueue لصنف التنبيه، يمكنك إرسال الإشعارات كالمعتاد. سيكتشف Laravel الواجهة ويضيف تلقائيًا إرسال الإشعارات إلى قائمة انتظار:

$user->notify(new InvoicePaid($invoice));

إذا أردت تأخير إيصال التنبيه، يمكنك إضافة التابع delay حين صناعة التنبيه:

$when = now()->addMinutes(10);

$user->notify((new InvoicePaid($invoice))->delay($when));

إشعارات حسب الطلب

قد تحتاج في بعض الأحيان لإرسال تنبيه لشخص ليس مسّجلًا كمستخدم. باستخدام التابع Notifications::route، يمكنك تحديد توجيه تنبيه مخصّص قبل إرسال التوجيه:

Notification::route('mail', 'taylor@example.com')

           ->route('nexmo', '5555555555')
           ->notify(new InvoicePaid($invoice));

إشعارات البريد الإلكتروني

تنسيق رسائل البريد

إذا كان التنبيه يدعم الإرسال بالبريد، يجب تعريف التابع toMail في صنف التنبيه. يقبل هذا التابع كائن notifiable$ ويعيد نسخة الكائن Illuminate\Notifications\Messages\MailMessage. يمكن أن تحتوي رسائل سطورًا من النصوص بالإضافة مِحث لاتخاذ إجراء. لنلقِ نظرةً على مثال تابع toMail:

/**

*الحصول على تمثيل للتنبيه.
*
* @param  mixed  $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable) {

   $url = url('/invoice/'.$this->invoice->id);
   return (new MailMessage)
               ->greeting('Hello!')
               ->line('One of your invoices has been paid!')
               ->action('View Invoice', $url)
               ->line('Thank you for using our application!');
}

ملاحظة: لاحظ أنّنا استعملنا this->invoice->id$ في التابع toMail. يمكنك تمرير أي بيانات تحتاجها لتوليد الرسالة في التابع الباني للتنبيه. في هذا المثال، سجّلنا تحيةً، ثم سطرًا من النص، ثم محثًا لاتخاذ إجراءٍ ما، ثمّ سطرًا آخر من النص. هذه التوابع التي يوفّرها الكائن MailMessage تجعل تنسيق بريدٍ قصيرٍ أمرًا سريعًا وبسيطًا. بعد ذلك، ستُحوّل قناة البريد المكوّنات لقالب HTML جميل ومتجاوب يحتوي على نص بسيط. في ما يلي مثال على بريد مولّد بالقناة mail:

notification-example.png

ملاحظة: عند بعث إشعارات بالبريد الإلكتروني، تأكد من ضبط قيمة name في ملف الضبط config/app.php. ستُستعمل هذه القيمة في رأس وتذييل الرسالة.

خيارات تنسيق إضافية

بدل تعريف الأسطر في صنف التنبيه، يمكن استعمال التابع view لتحديد قالب خاص يُستعمَل لإظهار بريد التنبيه:

/**

*الحصول على تمثيل للتنبيه.
*
* @param  mixed  $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable) {

   return (new MailMessage)->view(
       'emails.name', ['invoice' => $this->invoice]
   );

يمكن أيضًا إعادة كائن قابل للإرسال من التابع toMail:

/**

* الحصول على تمثيل للتنبيه.
*
* @param  mixed  $notifiable
* @return Mailable
*/
public function toMail($notifiable) {

   return (new Mailable($this->invoice))->to($this->user->email);
} .

*
* @param  mixed  $notifiable
* @return Mailable
*/
public function toMail($notifiable) {

   return (new Mailable($this->invoice))->to($this->user->email);
}

رسائل الأخطاء

بعض الإشعارات تُعلم المستخدم بوقوع خطأ. مثل خطأ في دفع فاتورة. يمكنك ذكر أن رسالة التنبيه بخصوص خطأ باستعمال التابع error عند صنع الرسالة. عند استعمال التابع error على رسالة، يكون زرّ نداء العمل أحمرَ اللون بدل الأزرق:

/**

* الحصول على تمثيل للتنبيه.
*
* @param  mixed  $notifiable
* @return \Illuminate\Notifications\Message
*/
public function toMail($notifiable) {

   return (new MailMessage)
               ->error()
               ->subject('Notification Subject')
               ->line('...');
}

تخصيص المتلقي

عند إرسال التنبيه عبر القناة mail، سيبحث نظام الإشعارات تلقائيًا عن خاصية email في الكائن القابل لتلقي تنبيه. يمكنك تخصيص أي عنوان إلكتروني يُستعمَل لإرسال الإشعارات بتعريف التابع routeNotifcationForMail:

<?php

namespace App;

use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable {

   use Notifiable;
   /**
    * توجيه الرسالة للقناة.
    *
    * @param  \Illuminate\Notifications\Notification  $notification
    * @return string
    */
   public function routeNotificationForMail($notification)
   {
       return $this->email_address;
   }
}

تخصيص الموضوع

في العادة، يكون موضوع الرسالة هو اسم صنف التنبيه منسّق بطريقة "كتابة العنوان". لذا إذا كان اسم صنف الإشعارات InvoicePaid، فسيكون الموضوع Invoice Paid. إذا أردت تحديد موضوع معيّن للرسالة، فاستعمل التابع subject عند بناء الرسالة:

/**

*الحصول على تمثيل للتنبيه.
*
* @param  mixed  $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable) {

   return (new MailMessage)
               ->subject('Notification Subject')
               ->line('...');
}

تخصيص القالب

يمكن تغيير قالب HTML وقالب النص البسيط المستخدَم من قِبل الرسالة بنشر مصدر حزمة التنبيه.

php artisan vendor:publish --tag=laravel-notifications

إشعارات بريد Markdown

تسمح إشعارات Markdown بالإستفادة من القوالب المصنوعة مسبقًا في البريد الإلكتروني في حين توفّر في نفس الوقت حريّة كتابة أطول وأكثر تخصيصًا حيث تُكتب بلغة Markdown. يُظهر Laravel قوالب HTML بطريقة جميلة ومتجاوبة للرسائل في حين يولّد تلقائيًا نسخة مقابلة من النص البسيط.

توليد الرسالة

لتوليد تنبيه بقالب Markdown معيّن، استعمل الخيار markdown-- مع الأمر make:notification:

php artisan make:notification InvoicePaid --markdown=mail.invoice.paid

كبقية إشعارات البريد، يجب على الإشعارات التي تستعمل Markdown أن تُعرِّف التابع toMail في صنف الإشعارات. لكن بدل استعمال line و action، استعمل التابع markdown لتحديد اسم القالب الذي يجب استعماله:

/**

*
* @param  mixed  $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable) {

   $url = url('/invoice/'.$this->invoice->id);
   return (new MailMessage)
               ->subject('Invoice Paid')
               ->markdown('mail.invoice.paid', ['url' => $url]);
}

كتابة الرسالة

تستعمل إشعارات Markdown مزيجًا من مكونات Blade و نصوص Markdown مما يسمح ببناء الإشعارات بسهولة إلى جانب استعمال مكونات Laravel المصنوعة مسبقًا:

@component('mail::message')
# Invoice Paid

Your invoice has been paid!

@component('mail::button', ['url' => $url])
View Invoice
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

المكوّن Button

يُظهر المكوّن button زًرَّ ربط في وسط الصفحة. يقبل المكوّن معاملَين: مسار url ولونًا اختياريًا color. الألوان المدعومة هي blue و green و red. يمكنك إضافة أي عدد تريده من الأزرار:

@component('mail::button', ['url' => $url, 'color' => 'green']) View Order @endcomponent

المكوّن Panel

يُظهرالمكوّن Panel النص المُمرّر كمجموعة بخلفية مختلفة قليلًا عن بقية الرسالة مما يسمح بجذب الانتباه لهذا النص:

@component('mail::panel') This is the panel content. @endcomponent

المكوّن Table

يسمح المكوّن table بتحويل جدول Markdown لجدول HTML. يقبل المكوّن جدول Markdown كمحتوى ويدعم المحاذاة باستخدام نص محاذاة Markdown:

@component('mail::table')
| Laravel        | Table              | Example      |
| ---------------|:-----------------: | ------------:|
| Col 2 is       | Centered           | $10          |
| Col 3 is       | Right-Aligned      | $20          |
@endcomponent

تخصيص المكونات

يمكنك تحميل كل المكونات من Markdown في التطبيق لتخصيصها. لتحويل المكونات، استعمل الأمر vendor:publish لنشر الأصول laravel-mail:

php artisan vendor:publish --tag=laravel-mail

سيُحمّل هذا الأمر مكونات Markdown في المجلد resources/views/vendor/mail. سيحتوي المجلد mail على مجلد html ومجلد markdown، كل منهما يحتوي على النسخة الملائمة من المكوّنات. تُستخدم المكونات في المجلد html لبناء نسخة html من الرسالة في حين تُستخدم المكونات من المجلد Markdown لبناء نسخة نص عادي. لك حرية تخصيص هذه المكونات كما تريد.

تخصيص ال CSS

بعد تحميل المكونات، يحتوي المجلد resources/views/vendor/mail/html/themes على ملف css المبدئي default.css. يمكنك تخصيص تخطيط CSS في هذا الملف وسيُضمّن تلقائيًا في العرض HTML لرسائل Markdown. ملاحظة: إذا أردت بناء نمط جديد لمكون Markdown، اكتب ملف CSS جديد في المجلد html/themes وغيّر الخيار theme في ملف الضبط mail.

إشعارات قاعدة البيانات

المتطلبات

تُخزّن قناة الإشعارات database معلومات الإشعارات في جدول في قاعدة البيانات. يحتوي هذا الجدول على معلومات مثل نوع التنبيه ومعلومات JSON خاصّة لوصف التنبيه.

يمكن مساءلة الجدول لعرض الإشعارات في واجهة المستخدم لكن قبل أن تتمكن من القيام بذلك، ستحتاج لإنشاء جدول قاعدة بيانات لحمل الإشعارات. يمكن استعمال notifications:table لتوليد ترحيل بالمخطط المناسب:

php artisan notifications:table
php artisan migrate

تنسيق إشعارات قواعد البيانات

إذا كان التنبيه يدعم التخزين في قاعدة البيانات، يجب تعريف أحد التابعين toDatabase أو toArray في صنف التنبيه. سيتلقى هذا التابع كائن notifiable$ يُعيد مصفوفة PHP. تُشفّر المصفوفة المُرجعة كشيفرة JSON وتُخزَّن في الخانة data من الجدول notifications. فلنلق نظرة على التابع toArray:

/**

* Get the array representation of the notification.
*
* @param  mixed  $notifiable
* @return array
*/
public function toArray($notifiable) {

   return [
       'invoice_id' => $this->invoice->id,
       'amount' => $this->invoice->amount,
   ];
}

المقارنة بين toDatabase و toArray

يُستعمل التابع toArray أيضًا من خدمة البث (broadcast) للتثبت من أي بيانات يجب بثها لعميل JavaScript. إذا أردت استخدام مصفوفتين مختلفتين لتمثيل القناتين roadcast و database، فيجب عليك تعريف التابع toDatabase و toArray.

الوصول للإشعارات

بعد تسجيل التنبيه في قاعدة البيانات، ستحتاج إلى طريقةٍ سهلةٍ للوصول إليها من الكائنات القابلة للتنبيه في التطبيق. الخاصية Illuminate\Notifications\Notifiable المضمّنة أصلًا في النموذج App\User تحمل علاقة Eloquent هي notifications التي تُعيد التنبيه للكائن. لإعادة الإشعارات، استخدم التابع كأي علاقة Eloquent. ستُصنَّف الإشعارات تلقائيًا حسب وقت الصنع created_at:

$user = App\User::find(1);

foreach ($user->notifications as $notification) {

   echo $notification->type;
}

إذا أردت إعادة الإشعارات غير المقروءة فقط، استعمل العلاقة unreadNotifications. ستُرتَّب الإشعارات هنا أيضًا حسب وقت الصنع created_at:

$user = App\User::find(1);

foreach ($user->unreadNotifications as $notification) {

   echo $notification->type;
}

ملاحظة: للوصول إلى الإشعارات من عميل JavaScript، فيجب تعريف وحدة تحكم للتطبيق حيث تُعيد الإشعارات للكائنات القابلة للتنبيه ككائن المستخدم الحالي. ثم يمكنك إطلاق طلب HTTP لوحدة التحكم تلك من عميل JavaScript.

وضع إشارة مقروء على الإشعارات

في العادة، ستريد وضع علامة مقروء على التنبيه بعد أن يراه المستخدم. توفّر الخاصية Illuminate\Notifications\Notifiable التابع markAsRead الذي يحيّن الخانة read_at في قاعدة البيانات:

$user = App\User::find(1);

foreach ($user->unreadNotifications as $notification) {

   $notification->markAsRead();
}

لكن بدل تكرار العمل على كل الإشعارات، يمكن استعمال التابع markAsRead مباشرة على مجموعة إشعارات:

$user->unreadNotifications->markAsRead();

يمكن أيضًا طلب تحيين جماعي لوضع الإشعارات كمقروءة دون الحاجة للحصول عليها من قاعدة البيانات:

$user = App\User::find(1);

$user->unreadNotifications()->update(['read_at' => now()]);

طبعًا، يمكن استعمال التابع delete لحذف التنبيه نهائيًا من قاعدة البيانات:

$user->notifications()->delete();

إشعارات البث

المتطلبات

قبل بث الإشعارات، يجب ضبط البث التعوّد على العمل بخدمات البث. يوفّر بث الأحداث طريقة للتفاعل مع أحداث Laravel المُطلق من جهة الخادم من عميل JavaScript.

تنسيق بث الإشعارات

تبث القناة broadcast الإشعارات باستعمال خدمة بث الأحداث في Laravel، مما يسمح لعميل JavaScript بالحصول على الإشعارات في وقتها الحقيقي. إذا كانت الإشعارات تدعم البث، يجب تعريف التابع toBroadcast في صنف التنبيه. يتلقى هذا التابع كائن notfifiable$ ويعيد نسخة الكائن BroadcastMessage، ترمَّز البيانات المعادة كشيفرة JSON وتُبثّ لعميل JavaScript. لنلق نظرة على مثال عن التابع toBroadcast:

use Illuminate\Notifications\Messages\BroadcastMessage;

/**

* الحصول على البث.
*
* @param  mixed  $notifiable
* @return BroadcastMessage
*/
public function toBroadcast($notifiable) {

   return new BroadcastMessage([
       'invoice_id' => $this->invoice->id,
       'amount' => $this->invoice->amount,
   ]);
}

ضبط طابور انتظار البث

توجد كل الإشعارات المعدة للبث في طابور انتظار البث. إذا أردت ضبط اسم أو وصلة الطابور المستخدم مع بث الإشعارات، استخدم التابعين onConnection و onQueue من

BroadcastMessage: return (new BroadcastMessage($data))

               ->onConnection('sqs')
               ->onQueue('broadcasts');

ملاحظة: بالإضافة للبيانات التي تُحدّدها، يحتوي بث الإشعارات أيضًا على خانة type تحتوي اسم صنف التنبيه.

الاستماع للإشعارات

تُبث الإشعارات على قناة خاصة منسقة بطريقة {notifiable}.{id}. لذا، إذا أرسلتَ تنبيهًا للنموذج App\User مع ID يساوي 1، سيُبث التنبيه على القناة الخاصة App.User.1. عند استعمال Laravel echo يمكنك الاستماع بسهولة لأي قناة باستعمال التابع المساعد notification:

Echo.private('App.User.' + userId)

   .notification((notification) => {
       console.log(notification.type);
   });

تخصيص قنوات الإشعار

إذا أردت اختيار القناة التي تتلقى عليها بث الإشعارات، يمكنك تعريف التابع receivesBroadcastNotificationsOn للكائن المتلقي للإشعارات:

<?php

namespace App;

use Illuminate\Notifications\Notifiable; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable {

   use Notifiable;
   /**
    * The channels the user receives notification broadcasts on.
    *
    * @return string
    */
   public function receivesBroadcastNotificationsOn()
   {
       return 'users.'.$this->id;
   }
}

إشعارات الرسائل القصيرة SMS

المتطلبات

خدمة إرسال إشعارات SMS مدعومة من قبل Nexmo. قبل أن تتمكن من إرسال الإشعارات، يجب أن تُثبت الحزمة nexmo/client وأن تضيف خيارات الضبط لملف الضبط config/services.php. يمكنك نسخ مثال الضبط التالي للبدء:

'nexmo' => [

   'key' => env('NEXMO_KEY'),
   'secret' => env('NEXMO_SECRET'),
   'sms_from' => '15556666666',
],

الخيار sms_from هو رقم الهاتف الذي ستُرسل منه رسالة التنبيه. يجب عليك إنشاء رقم هاتف لتطبيقك من لوحة تحكم Nexmo.

تنسيق إشعارات SMS

إذا كان التنبيه يدعم الإرسال عبر رسالة SMS، يجب تعريف التابع toNexmo في صنف التنبيه. سيتلقى التابع كائن notifiable$ ويعيد نسخة الكائن Illuminate\Notifications\Messages\NexmoMessage:

/**

* Get the Nexmo / SMS representation of the notification.
*
* @param  mixed  $notifiable
* @return NexmoMessage
*/
public function toNexmo($notifiable) {

   return (new NexmoMessage)
               ->content('Your SMS message content');
}

محتوى Unicode

إذا الرسالة تحتوي رموز unicode، فيجب استدعاء التابع unicode عند بناء الرسالة NexmoMessage:

/**

* Get the Nexmo / SMS representation of the notification.
*
* @param  mixed  $notifiable
* @return NexmoMessage
*/
public function toNexmo($notifiable) {

   return (new NexmoMessage)
               ->content('Your unicode message')
               ->unicode();
}

تخصيص الرقم "from"

إن أردت إرسال تنبيه من رقم غير الرقم المُعرَف في config/services.php، استعمل التابع from في NexmoMessage:

/**

* Get the Nexmo / SMS representation of the notification.
*
* @param  mixed  $notifiable
* @return NexmoMessage
*/
public function toNexmo($notifiable) {

   return (new NexmoMessage)
               ->content('Your SMS message content')
               ->from('15554443333');
}

توجيه إشعارات SMS

عند إرسال إشعارات عبر قناة nexmo، سيبحث نظام الإشعارات تلقائيًا عن الخاصية phone_number في الكائن المنبَه. إن أردت تخصيص رقم هاتف يُرسَل إليه التنبيه، عرِّف التابع routeNotificationForNexmo في الكائن:

<?php

namespace App;

use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable {

   use Notifiable;
   /**
    * Route notifications for the Nexmo channel.
    *
    * @param  \Illuminate\Notifications\Notification  $notification
    * @return string
    */
   public function routeNotificationForNexmo($notification)
   {
       return $this->phone;
   }
}

إشعارات Slack

المتطلبات

قبل أن تتمكن من إرسال إشعارات عبر Slack، يجب عليك تثبيت المكتبة Guzzle HTTP عبر Composer:

composer require guzzlehttp/guzzle

ستحتاج أيضًا إلى ضبط "Incoming Webhook" لفريق Slack. سيُوفر هذا الضبط مسار URL يمكنك استعماله عند إرسال إشعارات Slack.

تنسيق إشعارات slack

إن كان التنبيه يدعم الإرسال عبر Slack، يجب تعريف التابع toSlack في صنف التنبيه. سيتلقى التابع كائن notifiable$ ويعيد نسخة الكائن Illuminate\Notifications\Messages\SlackMessage. يمكن أن تحتوي إشعارات Slack على نصوص بالإضافة لمرفقات تنسق نصوصًا إضافية أو مصفوفة من الخانات. لنلق نظرة على مثال toSlack أساسي:

/**

* Get the Slack representation of the notification.
*
* @param  mixed  $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable) {

   return (new SlackMessage)
               ->content('One of your invoices has been paid!');
}

في هذا المثال، أرسلنا فقط سطر نص مما سينتج تنبيها كالآتي:

basic-slack-notification.png

تخصيص المرسل و المتلقي

يمكن استعمال التابعين from و to لتخصيص مرسل ومتلق للتنبيه. يقبل التابع from اسم مستخدم أو معرف emoji بينما يقبل التابع to قناة إرسال أو اسم مستخدم:

/**

* Get the Slack representation of the notification.
*
* @param  mixed  $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable) {

   return (new SlackMessage)
               ->from('Ghost', ':ghost:')
               ->to('#other')
               ->content('This will be sent to #other');
}

يمكنك أيضًا استعمال صورة بدل وجه emoji:

/**

* Get the Slack representation of the notification.
*
* @param  mixed  $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable) {

   return (new SlackMessage)
               ->from('Laravel')
               ->image('https://laravel.com/favicon.png')
               ->content('This will display the Laravel logo next to the message');
}

مرفقات Slack

يمكنك إضافة مرفقات لإشعارات Slack، توفر المرفقات خيارات تنسيق أكثر من النص العادي. في هذا المثال، سنرسل تنبيه خطأ عن استثناء حصل في التطبيق مع رابط لعرض معلومات إضافية عن الاستثناء:

/**

* Get the Slack representation of the notification.
*
* @param  mixed  $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable) {

   $url = url('/exceptions/'.$this->exception->id);
   return (new SlackMessage)
               ->error()
               ->content('Whoops! Something went wrong.')
               ->attachment(function ($attachment) use ($url) {
                   $attachment->title('Exception: File Not Found', $url)
                              ->content('File [background.jpg] was not found.');
               });
}

سيُنتج المثال السابق رسالة Slack مشابهة للآتي

basic-slack-attachment.png

تسمح المرفقات أيضا بتحديد مصفوفة بيانات تُقدم للمستخدم. ستقدم البيانات في شكل جدول لتسهيل القراءة:

/**

* Get the Slack representation of the notification.
*
* @param  mixed  $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)

{

   $url = url('/invoices/'.$this->invoice->id);
   return (new SlackMessage)
               ->success()
               ->content('One of your invoices has been paid!')
               ->attachment(function ($attachment) use ($url) {
                   $attachment->title('Invoice 1322', $url)
                              ->fields([
                                   'Title' => 'Server Expenses',
                                   'Amount' => '$1,234',
                                   'Via' => 'American Express',
                                   'Was Overdue' => ':-1:',
                               ]);
               });
}

سينتج المثال السابق رسالة Slack تشبه ما يلي:

slack-fields-attachment.png

محتوى المرفقات Markdown

إن كانت بعض حقول المرفقات تحتوي Markdown، استعمل التابع markdown لإعلام Slack عن طريقة قراءة وعرض محتوى الحقل. القيم التي يقبلها التابع هي pretext أو text أو fields. لمزيد من المعلومات عن تنسيق مرفقات Slack راجع توثيق Slack API:

/**

* Get the Slack representation of the notification.
*
* @param  mixed  $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable) {

   $url = url('/exceptions/'.$this->exception->id);
   return (new SlackMessage)
               ->error()
               ->content('Whoops! Something went wrong.')
               ->attachment(function ($attachment) use ($url) {
                   $attachment->title('Exception: File Not Found', $url)
                              ->content('File [background.jpg] was *not found*.')
                              ->markdown(['text']);
               });
}

توجيه إشعارات slack

لتوجيه إشعارات Slack لأماكنها الصحيحة، عرف التابع routeNotificationForSlack في الكائن المنَبه. سيعيد التابع مسار Webhook URL يجب أن تُرسل الإشعارات إليه. يمكن الحصول على المسار أيضًا عبر خدمة "Incoming Webhook" في فريق Slack:

<?php

namespace App;

use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable {

   use Notifiable;
   /**
    * Route notifications for the Slack channel.
    *
    * @param  \Illuminate\Notifications\Notification  $notification
    * @return string
    */
   public function routeNotificationForSlack($notification)
   {
       return $this->slack_webhook_url;
   }
}

أحداث الإشعارات

عندما يُرسَل تنبيه ما، يُطلِق نظام الإشعارات الحدث Illuminate\Notifications\Events\NotificationSent. يحتوي الحدث على الكائن المنبَه ونسخة التنبيه نفسه. يمكنك تسجيل مستمعات على هذا الحدث في EventServiceProvider:

/**

* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [

   'Illuminate\Notifications\Events\NotificationSent' => [
       'App\Listeners\LogNotification',
   ],ا
];

ملاحظة: بعد تسجيل المستمع في EventServiceProvider، استعمل الأمر event:generate لإنشاء أصناف المستمعات بسرعة. في صنف المستمع، يمكنك الوصول إلى الخاصيات notifiable و notification و channel للحدث للمعرفة معلومات عن المتلقي وعن التنبيه:

/**

* Handle the event.
*
* @param  NotificationSent  $event
* @return void
*/
public function handle(NotificationSent $event) {

  // $event->channel
  // $event->notifiable
  // $event->notification
}

القنوات المخصصة

يأتي Laravel مع عدد من قنوات إرسال الإشعارات، لكن قد تريد كتابة مشغلك الخاص لإرسال الإشعارات عبر قنوات أخرى. يجعل Laravel هذا سهلا. للبدء، عرف صنفا يحتوي التابع send. يجب أن يقبل هذا التابع معاملين notifiable$ و notification$:

<?php

namespace App\Channels;

use Illuminate\Notifications\Notification;

class VoiceChannel {

   /**
    * Send the given notification.
    *
    * @param  mixed  $notifiable
    * @param  \Illuminate\Notifications\Notification  $notification
    * @return void
    */
   public function send($notifiable, Notification $notification)
   {
       $message = $notification->toVoice($notifiable);
      // Send notification to the $notifiable instance...
   }
}

بعد تعريف قناة التنبيه، يمكنك إعادة اسم الصنف من التابع via من أي تنبيه:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable; use App\Channels\VoiceChannel; use App\Channels\Messages\VoiceMessage; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue;

class InvoicePaid extends Notification {

   use Queueable;
   /**
    * Get the notification channels.
    *
    * @param  mixed  $notifiable
    * @return array|string
    */
   public function via($notifiable)
   {
       return [VoiceChannel::class];
   }
   /**
    * Get the voice representation of the notification.
    *
    * @param  mixed  $notifiable
    * @return VoiceMessage
    */
   public function toVoice($notifiable)
   {
      // ...
   }
}

مصادر