# نمایش بلادرنگ اعلامی به تمام کاربران در هنگام درج یک رکورد جدید به صورت notification

در ادامه می‌خواهیم مثالی را که در این مطلب مورد بررسی قرار گرفت، به صورتی تغییر دهیم که با ثبت یک آیتم جدید درون دیتابیس، یک notification، به تمامی کاربران متصل به هاب ارسال شود. همچنین با کلیک بر روی

- Published: 2015-01-02
- Language: fa
- Tags: DNTips
- Canonical: https://sirwan.info/blog/fa/dntips-1955

---

> این نوشته نخستین بار در [دات‌نت تیپس](https://www.dntips.ir/post/1955) منتشر شده است.

<div class="postBody">در ادامه می‌خواهیم مثالی را که در <a href="/blog/fa/dntips-1885/">این مطلب</a> مورد بررسی قرار گرفت، به صورتی تغییر دهیم که با ثبت یک آیتم جدید درون دیتابیس، یک notification، به تمامی کاربران متصل به هاب ارسال شود. همچنین با کلیک بر روی Notification سطر جدید نیز بلافاصله نمایش داده شود:<div> <br/> <div> <p> <img src="/img/dntips/081b2ef67744c3996cf6.jpg" style="display: block; margin-left: auto; margin-right: auto; cursor: default;"/> </p>
 در این مثال برای نمایش پیام به صورت notification، از کتابخانه <a href="https://github.com/CodeSeven/toastr">toastr</a> استفاده می‌کنیم که از طریق nuget می‌توانید آن را به پروژه اضافه کنید:</div> <div> <div align="left" dir="ltr">
<pre language="CSharp" name="code">PM&gt; Install-Package toastr</pre>
 </div>
کار با این کتابخانه خیلی ساده است؛ کافی است فایل‌های js و css آن را به فایل layout اضافه کرده و به این صورت از آن استفاده کنیم:</div> <div> <div align="left" dir="ltr"> <div align="left" dir="ltr">
<pre language="CSharp" name="code">toastr.info("نمایش یک پیام - info");&#10;toastr.success("نمایش یک پیام - success");&#10;toastr.error("نمایش یک پیام - error");&#10;toastr.warning("نمایش یک پیام - warning");</pre>
 </div> <div style="text-align: right; direction: rtl;">دستورات فوق خروجی‌های زیر را نمایش می‌دهد:</div> <div style="text-align: right; direction: rtl;"> <p> <img src="/img/dntips/f2dbac8e453c239512a8.jpg" style="display: block; margin-left: auto; margin-right: auto; cursor: default;"/> </p> </div> </div> </div> <div>برای پیام‌های فوق نیز می‌توانید عنوانی را انتخاب کنید:</div> <div> <div align="left" dir="ltr">
<pre language="CSharp" name="code">toastr.success("نمایش یک پیام - success", "عنوان");</pre>
 </div>
اگر به فایل js این کتابخانه مراجعه کنید، می‌توانید مقادیر پیش‌فرض آن را برای نمایش یک پیام مشاهده کنید. برای سفارشی‌سازی آن نیز می‌توانید به این صورت عمل کنید:</div> <div> <div align="left" dir="ltr">
<pre language="JScript" name="code">toastr.options = {&#10;            tapToDismiss: true,&#10;            toastClass: 'toast',&#10;            containerId: 'toast-container',&#10;            debug: false,&#10;&#10;            showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery&#10;            showDuration: 300,&#10;            showEasing: 'swing', //swing and linear are built into jQuery&#10;            onShown: undefined,&#10;            hideMethod: 'fadeOut',&#10;            hideDuration: 1000,&#10;            hideEasing: 'swing',&#10;            onHidden: undefined,&#10;&#10;            extendedTimeOut: 1000,&#10;            iconClasses: {&#10;                error: 'toast-error',&#10;                info: 'toast-info',&#10;                success: 'toast-success',&#10;                warning: 'toast-warning'&#10;            },&#10;            iconClass: 'toast-info',&#10;            positionClass: 'toast-top-right',&#10;            timeOut: 5000, // Set timeOut and extendedTimeOut to 0 to make it sticky&#10;            titleClass: 'toast-title',&#10;            messageClass: 'toast-message',&#10;            target: 'body',&#10;            closeHtml: '&lt;button&gt;&amp;times;&lt;/button&gt;',&#10;            newestOnTop: true,&#10;            preventDuplicates: false,&#10;            progressBar: false&#10;};</pre>
 </div>
اکنون برای نمایش این نوع پیام‌ها در زمان اتصال به هاب (در واقع در زمان ثیت یک رکورد جدید) نیاز به ارسال پارامتر خاصی به سرور (از سمت کلاینت) نمی‌باشد. تنها باید کدهای سمت سرور یعنی هاب را به گونه‌ایی تغییر دهیم تا به محض فراخوانی SendNotification، آخرین رکورد ثبت شده در دیتابیس را به تمامی کلاینت‌های متصل به هاب ارسال کند: </div> <div> <div align="left" dir="ltr">
<pre language="CSharp" name="code">public class NotificationHub : Hub&#10;{&#10;        private readonly IProductService _productService;&#10;&#10;        public NotificationHub(IProductService productService)&#10;        {&#10;            _productService = productService;&#10;        }&#10;&#10;        public void SendNotification()&#10;        {&#10;            Clients.Others.ShowNotification(_productService.GetLastProduct());&#10;        }&#10;}</pre>
 </div>
در سمت کلاینت نیز کدها همانند مثال قبل هستند؛ با این تفاوت که در متد سمت کلاینت باید اطلاعات ارسال شده از سمت سرور را با نمایش یک notification به کاربران اطلاع دهیم:</div> <div> <div align="left" dir="ltr"> <div align="left" dir="ltr">
<pre language="CSharp" name="code">var notify = $.connection.notificationHub;&#10;notify.client.showNotification = function (data) {&#10;toastr.info("رکورد جدیدی ثبت گردید جهت نمایش اینجا کلیک کنید");&#10;};&#10;$.connection.hub.start().done(function () {&#10;            @{&#10;                if (ViewBag.NotifyUsers)&#10;                {&#10;                    &lt;text&gt;notify.server.sendNotification();&lt;/text&gt;&#10;                }&#10;            }&#10;});</pre>
 </div> <div style="direction: rtl; text-align: right;">تا اینجا همانند مثال قبلی عمل کردیم. یعنی به جای نمایش یک alert بوت‌استرپ، از کتابخانه toastr استفاده گردید. در مثال قبلی کاربر برای دیدن تغییرات می‌بایستی یکبار صفحه را ریفرش کند، اکنون می‌خواهیم کاربر بعد از کلیک بر روی پیام، بلافاصله سطر جدید را نیز مشاهده کند:</div> <div style="direction: rtl; text-align: right;"> <div align="left" dir="ltr">
<pre language="CSharp" name="code">var positionClasses = {&#10;            topRight: 'toast-top-right',&#10;            bottomRight: 'toast-bottom-right',&#10;            bottomLeft: 'toast-bottom-left',&#10;            topLeft: 'toast-top-left',&#10;            topCenter: 'toast-top-center',&#10;            bottomCenter: 'toast-bottom-center'&#10;        };&#10;        var notify = $.connection.notificationHub;&#10;        notify.client.showNotification = function (data) {&#10;            toastr.options = {&#10;                showDuration: 300,&#10;                positionClass: positionClasses.bottomRight,&#10;                onclick: function () {&#10;                    $('#table tr:last').after("&lt;tr&gt;" +&#10;                    "&lt;td&gt;" + data.Title + "&lt;/td&gt;" +&#10;                    "&lt;td&gt;" + data.Description + "&lt;/td&gt;" +&#10;                    "&lt;td&gt;" + data.Price + "&lt;/td&gt;" +&#10;                    "&lt;td&gt;" + data.Category + "&lt;/td&gt;" +&#10;                    "&lt;td&gt;  &lt;/td&gt;" +&#10;                    "&lt;/tr&gt;");&#10;&#10;                }&#10;            };&#10;            toastr.info("رکورد جدیدی ثبت گردید جهت نمایش اینجا کلیک کنید");&#10;            &#10;            &#10;        };&#10;        $.connection.hub.start().done(function () {&#10;            @{&#10;                if (ViewBag.NotifyUsers)&#10;                {&#10;                    &lt;text&gt;notify.server.sendNotification();&lt;/text&gt;&#10;                }&#10;            }&#10;});</pre>
 </div>
همانطور که مشاهده می‌کنید از onClick برای toastr استفاده کرده‌ایم. با این callback گفته‌ایم که اگر بر روی پیام کلیک شد، اطلاعات را به صورت یک سطر جدید به جدول اضافه کن:</div> <div style="direction: rtl; text-align: right;"> <div align="left" dir="ltr">
<pre language="CSharp" name="code">onclick: function () {&#10;                    $('#table tr:last').after("&lt;tr&gt;" +&#10;                    "&lt;td&gt;" + data.Title + "&lt;/td&gt;" +&#10;                    "&lt;td&gt;" + data.Description + "&lt;/td&gt;" +&#10;                    "&lt;td&gt;" + data.Price + "&lt;/td&gt;" +&#10;                    "&lt;td&gt;" + data.Category + "&lt;/td&gt;" +&#10;                    "&lt;td&gt;  &lt;/td&gt;" +&#10;                    "&lt;/tr&gt;");&#10;&#10;}</pre>
 </div>
مقادیر به صورت یک شیء جاوااسکریپتی برگردانده خواهند شد:</div> <div style="direction: rtl; text-align: right;"> <div align="left" dir="ltr">
<pre language="CSharp" name="code">data {Id: 12, Title: "Item1", Description: "Des", Price: 100000, Category: 0}</pre>
 </div>
که توسط data می‌توانیم به هر کدام از فیلدها، جهت نمایش در خروجی، دسترسی داشته باشیم.</div> </div> <b>
 دریافت سورس مثال جاری</b>: <a href="https://www.dntips.ir/file/userfile?name=ShowAlertSignalR-5442e7f4f1774489bceff96d10a79287.zip">ShowAlertSignalR  </a> </div> </div></div>
