Ramjeet Maurya

Ramjeet Maurya

  • NA
  • 149
  • 11.6k

Real time Notification When any Changes In Datasase

Jan 24 2020 4:39 PM
Firstly  i say Thanks ,
 
My stuck is That i have try to Implement Real time Notification  since Last few day  but i cant Done  Becouse When i implement it then   my Code are Working fine but No Notification Has Been Displayin client Side .
 
My code are give Below.

 
Its Is My Repositrycore When I checke that Data from Data Base.
 

 
Use Folowiing JS library
 
 
 
This Is My Code :
  1. <script type="text/javascript">  
  2. function getAllMessagesForBlog() {  
  3. $.ajax({  
  4. type: "POST",  
  5. url: "Chat.aspx/getAllMessagesForBlog",  
  6. contentType: "application/json; charset=utf-8",  
  7. dataType: "json",  
  8. success: function (result) {  
  9. $('#messagesTable tbody').empty();  
  10. var table = $('#messagesTable tbody');  
  11. var row = '';  
  12. for (var i = 0; i < result.d.length; i++) {  
  13. displayNotification("Click Here""http://localhost:1386/asset/images/Icontesty.png", result.d[i].BlogTitle, 'http://localhost:1386/blog/' + result.d[i].BlogTitleURL);  
  14. }  
  15. },  
  16. error: ErrorMessage  
  17. });  
  18. function ErrorMessage(result) {  
  19. document.location.href = '/something-went-wrong?error=' + result.status + ' ' + result.statusText + ' ' + result.responseText;  
  20. //c  
  21. }  
  22. function getAllMessagesForProduct() {  
  23. $.ajax({  
  24. type: "POST",  
  25. url: "Chat.aspx/getAllMessagesForProduct",  
  26. contentType: "application/json; charset=utf-8",  
  27. dataType: "json",  
  28. success: function (result) {  
  29. $('#messagesTable tbody').empty();  
  30. var table = $('#messagesTable tbody');  
  31. var row = '';  
  32. for (var i = 0; i < result.d.length; i++) {  
  33. displayNotification("Click Here""http://localhost:1386/asset/images/Icontesty.png", result.d[i].ProductName, 'http://localhost:1386/' + result.d[i].CategoryAlias + '/' + result.d[i].FlavourAlias + '/' + result.d[i].OccasionAlias + '/' + result.d[i].ProductAlias, 6000000);  
  34. }  
  35. },  
  36. error: ErrorMessage  
  37. });  
  38. function ErrorMessage(result) {  
  39. document.location.href = '/something-went-wrong?error=' + result.status + ' ' + result.statusText + ' ' + result.responseText;  
  40. //c  
  41. }  
  42. function displayNotification(body, icon, title, link, duration) {  
  43. link = link || 0; // Link is optional  
  44. duration = 30000; // Default duration is 5 seconds  
  45. var options = {  
  46. body: body,  
  47. icon: icon  
  48. };  
  49. var n = new Notification(title, options);  
  50. if (link) {  
  51. n.onclick = function () {  
  52. window.open(link);  
  53. };  
  54. }  
  55. setTimeout(n.close.bind(n), duration);  
  56. }  
  57. function makeNotification() {  
  58. $(function () {  
  59. var notifications = $.connection.myChatHub;  
  60. notifications.client.updateMessages = function () {  
  61. // alert('hello');  
  62. //getAllMessagesForProduct();  
  63. getAllMessagesForBlog()  
  64. };  
  65. $.connection.hub.start().done(function () {  
  66. //alert('hello12');  
  67. // getAllMessagesForProduct();  
  68. getAllMessagesForBlog();  
  69. }).fail(function (e) {  
  70. alert(e);  
  71. });  
  72. });  
  73. }  
  74. window.onload = function notifyMe() {  
  75. if (!("Notification" in window)) {  
  76. alert("This browser does not support desktop notification");  
  77. }  
  78. else if (Notification.permission === "granted") {  
  79. makeNotification();  
  80. }  
  81. else if (Notification.permission != 'denied') {  
  82. Notification.requestPermission(function (permission) {  
  83. if (permission === 'granted') {  
  84. makeNotification();  
  85. }  
  86. });  
  87. }  
  88. else {  
  89. var notification = new Notification("New notification Blocked!");  
  90. }  
  91. }  
  92. </script> 
But I have No Notification In Real Time please chaeck 
 
this code in global file 

 
 
Please Provide Best solution If Have you Done Before and have Working fine Please Share Code With all REfrence with all dependency
 
thankyou. 

Answers (1)