Google Chrome Push Notification in ASP.Net

Last Reply one year ago By dharmendr

Posted one year ago

I sucessfully send notification to google chrome. My service-worker.js is below.

 

 

'use strict';

self.addEventListener('push', function(event) {
  console.log('Received a push message', event);

  var title = 'Pushup Notification';
  var body = 'Test';
  var icon = 'images/icon.png';
  var badge = 'images/badge.png'
  var tag = 'simple-push-demo-notification-tag';

  event.waitUntil(
    self.registration.showNotification(title, {
      body: body,
      icon: icon,
      badge: badge,
      tag: tag
    })
  );
});

self.addEventListener('notificationclick', function(event) {
  console.log('On notification click: ', event.notification.tag);
  // Android doesn’t close the notification when you click on it
  // See: http://crbug.com/463146
  event.notification.close();

  // This looks to see if the current is already open and
  // focuses if it is
  event.waitUntil(clients.matchAll({
    type: 'window'
  }).then(function(clientList) {
    for (var i = 0; i < clientList.length; i++) {
      var client = clientList[i];
      if (client.url === '/' && 'focus' in client) {
        return client.focus();
      }
    }
    if (clients.openWindow) {
      return clients.openWindow('/');
    }
  }));
});

Below is the sending code to Google Chrome.

 

Public Function SendNotification(ByVal deviceId As String, ByVal message As String) As String

        Dim SERVER_API_KEY As String = "API KEY"
        Dim SENDER_ID = "Sender ID"
        Dim value = message
        Dim tRequest As WebRequest
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send")
        tRequest.Method = "post"
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8"
        tRequest.Headers.Add(String.Format("Authorization: key={0}", SERVER_API_KEY))

        tRequest.Headers.Add(String.Format("Sender: id={0}", SENDER_ID))

        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() + "&registration_id=") & deviceId) + ""
        Console.WriteLine(postData)
        Dim byteArray As [Byte]() = Encoding.UTF8.GetBytes(postData)
        tRequest.ContentLength = byteArray.Length

        Dim dataStream As Stream = tRequest.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()

        Dim tResponse As WebResponse = tRequest.GetResponse()

        dataStream = tResponse.GetResponseStream()

        Dim tReader As New StreamReader(dataStream)

        Dim sResponseFromServer As [String] = tReader.ReadToEnd()


        tReader.Close()
        dataStream.Close()
        tResponse.Close()
        Return sResponseFromServer
        MsgBox("ok")

    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.

You are viewing reply posted by: dharmendr one year ago.
Posted one year ago

Hi basit0079,

What is the problem with this code. Are you getting any error?