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.


Posted one year ago

Many Thanks.

 my service-worker.js below variable are hard coded,

how to define such variable where i can send with above .net code and showing atleast body var message. Whenever i'm sending push up notification with above .net code its showing same body becoz its hard coded.

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

Thanks

Basit.


Posted one year ago Modified on one year ago

Hi,

I changed my service-worker.js.

 Below is my service-worker.js file.

self.addEventListener('push', function (event) {
    var data = {};
    var icon = 'images/icon.png';

    if (event.notification) {
        console.log('Received a push message');
        data = event.notification.json();

        event.waitUntil(
            self.registration.showNotification(data.title, {
                body: data.body,
                icon: icon,
                tag: data.tag
            })
        );
    } else {
        console.log('No push data received');
        console.log(event);
        var title = 'Yay a message!';
        var body = 'We have received a push message.';
        var icon = 'images/icon.png';
        var tag = 'simple-push-demo-notification-tag';

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

But how to pass 

body: data.body
//parameter to above .net code

Posted one year ago

Hi basit0079,

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