Basit Khan

Basit Khan

  • 1.3k
  • 336
  • 115.2k

Google Chrome Push Notification

Dec 21 2017 1:51 AM
I successfully send the notification to google chrome. My service-worker.js is below.
  1. 'use strict';  
  2. self.addEventListener('push'function(event) {  
  3. console.log('Received a push message', event);  
  4. var title = 'Pushup Notification';  
  5. var body = 'Test';  
  6. var icon = 'images/icon.png';  
  7. var badge = 'images/badge.png'  
  8. var tag = 'simple-push-demo-notification-tag';  
  9. event.waitUntil(  
  10. self.registration.showNotification(title, {  
  11. body: body,  
  12. icon: icon,  
  13. badge: badge,  
  14. tag: tag  
  15. })  
  16. );  
  17. });  
  18. self.addEventListener('notificationclick'function(event) {  
  19. console.log('On notification click: ', event.notification.tag);  
  20. // Android doesn’t close the notification when you click on it  
  21. // See: http://crbug.com/463146  
  22. event.notification.close();  
  23. // This looks to see if the current is already open and  
  24. // focuses if it is  
  25. event.waitUntil(clients.matchAll({  
  26. type: 'window'  
  27. }).then(function(clientList) {  
  28. for (var i = 0; i < clientList.length; i++) {  
  29. var client = clientList[i];  
  30. if (client.url === '/' && 'focus' in client) {  
  31. return client.focus();  
  32. }  
  33. }  
  34. if (clients.openWindow) {  
  35. return clients.openWindow('/');  
  36. }  
  37. }));  
  38. });  
Below is the sending code to Google Chrome.
  1. Public Function SendNotification(ByVal deviceId As StringByVal message As StringAs String  
  2. Dim SERVER_API_KEY As String = "API KEY"  
  3. Dim SENDER_ID = "Sender ID"  
  4. Dim value = message  
  5. Dim tRequest As WebRequest  
  6. tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")  
  7. tRequest.Method = "post"  
  8. tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8"  
  9. tRequest.Headers.Add(String.Format("Authorization: key={0}", SERVER_API_KEY))  
  10. tRequest.Headers.Add(String.Format("Sender: id={0}", SENDER_ID))  
  11. Dim postData As String = (Convert.ToString((Convert.ToString("collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=") & value) + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=") & deviceId) + ""  
  12. Console.WriteLine(postData)  
  13. Dim byteArray As [Byte]() = Encoding.UTF8.GetBytes(postData)  
  14. tRequest.ContentLength = byteArray.Length  
  15. Dim dataStream As Stream = tRequest.GetRequestStream()  
  16. dataStream.Write(byteArray, 0, byteArray.Length)  
  17. dataStream.Close()  
  18. Dim tResponse As WebResponse = tRequest.GetResponse()  
  19. dataStream = tResponse.GetResponseStream()  
  20. Dim tReader As New StreamReader(dataStream)  
  21. Dim sResponseFromServer As [String] = tReader.ReadToEnd()  
  22. tReader.Close()  
  23. dataStream.Close()  
  24. tResponse.Close()  
  25. Return sResponseFromServer  
  26. MsgBox("ok")  
  27. End Function  
What my question is When i'm sending with above code the push up notification showing whatever in the Hard coded title and body which is in service-worker.js. How to show my message which is in above .net code.
 
Thanks, Basit.