Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc Firebase浏览器通知:无法发送自定义消息_Asp.net Mvc_Firebase_Push Notification_Customization_Firebase Cloud Messaging - Fatal编程技术网

Asp.net mvc Firebase浏览器通知:无法发送自定义消息

Asp.net mvc Firebase浏览器通知:无法发送自定义消息,asp.net-mvc,firebase,push-notification,customization,firebase-cloud-messaging,Asp.net Mvc,Firebase,Push Notification,Customization,Firebase Cloud Messaging,我们在ASP.NET MVC中开发了一个Web应用程序,并通过Firebase实现了浏览器通知 通知工作正常,但每次触发通知时,它总是显示与以下所述相同的消息: “嘿,有最新消息。请访问我们的网站” 我们无法在通知中动态自定义消息 已经联系了谷歌支持,但他们告诉我们从堆栈溢出中获得帮助 如果你们能帮助我们解决这个问题,那就太好了,因为我们陷入了困境,无法前进 请按要求查找以下代码: sw.js: use strict; console.log('Started', self); self.

我们在ASP.NET MVC中开发了一个Web应用程序,并通过Firebase实现了浏览器通知

通知工作正常,但每次触发通知时,它总是显示与以下所述相同的消息:

“嘿,有最新消息。请访问我们的网站”

我们无法在通知中动态自定义消息

已经联系了谷歌支持,但他们告诉我们从堆栈溢出中获得帮助

如果你们能帮助我们解决这个问题,那就太好了,因为我们陷入了困境,无法前进

请按要求查找以下代码:

sw.js:

use strict;

console.log('Started', self);

self.addEventListener('install', function(event) {
self.skipWaiting();  
console.log('Installed', event);
});

self.addEventListener('activate', function(event) {
console.log('Activated', event);
});

 self.addEventListener('push', function (event) {
event.waitUntil(
     self.registration.pushManager.getSubscription().then(function    (subscription) {
      var notificationsPath = 'http://localhost:11546?endpoint=' + encodeURIComponent(subscription.endpoint);
      var headers = new Headers();
      headers.append('Accept', 'application/json');
      return fetch(notificationsPath, { headers: headers }).then(function (response) {
          return response.json().then(function (notifications) {
              return Promise.all(
                notifications.map(function (notification) {
                    return self.registration.showNotification(notification.title, {
                        body: notification.body,
                        //icon: notification.icon_url
                    });
                })
              );
          });
      }).catch(function (xhr, status, error) {
          console.error('Unable to retrieve the server.', error);
      });
  })
);
});
windows服务:

WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
tRequest.ContentType = "application/json";
var objNotification = new
            {
                to = "fQLxHW2fzuc:APA91bEkGKWqVlmZkM_Z9rZTF5bYbytOOsHFbfg7lL1vVzb6XycWkf-0NuMSdwo7hfpSlNuVBbx7o4i-E549eVCkvBbtPEb-dohuHiKLg18ROthj6ds3QIYyZKi_dsrHBn-uNxKGOhX-",                   
                data = new
               {
                    body = "This is the message",
                   title = "This is the title"
               }

            };
string jsonNotificationFormat = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            Byte[] byteArray =       Encoding.UTF8.GetBytes(jsonNotificationFormat);
tRequest.Headers.Add(string.Format("Authorization: key={0}", "AAAA80JHkUE:APA91bG2LuMawMpo9TiWbDxbc3GBRVbERu3TyKZIbNb_jTMs5C4q_pQrjLmF5-QJFGgj3FzMthR16nFCi2qrYOlCDrPvFop1uhlqMIMEoKaJsUu7vUZjKMvwazWZFbgalKEb1uhbOCIK7YK5yotusvbwyPO60pgANw"));
 tRequest.Headers.Add(string.Format("Sender: id={0}", "1044789039425"));
            tRequest.ContentLength = byteArray.Length;
            tRequest.ContentType = "application/json; charset=UTF-8";
            using (Stream dataStream = tRequest.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);

                using (WebResponse tResponse = tRequest.GetResponse())
                {
                    using (Stream dataStreamResponse = tResponse.GetResponseStream())
                    {
                        using (StreamReader tReader = new StreamReader(dataStreamResponse))
                        {
                            String responseFromFirebaseServer = tReader.ReadToEnd();

                            FCMResponse response = Newtonsoft.Json.JsonConvert.DeserializeObject<FCMResponse>(responseFromFirebaseServer);
                            if (response.success == 1)
                            {
                                //new NotificationBLL().InsertNotificationLog(dayNumber, notification, true);
                            }
                            else if (response.failure == 1)
                            {
                                //new NotificationBLL().InsertNotificationLog(dayNumber, notification, false);
                                //sbLogger.AppendLine(string.Format("Error sent from FCM server, after sending request : {0} , for following device info: {1}", responseFromFirebaseServer, jsonNotificationFormat));

                            }

                        }
                    }

                }
            }


manifest.json:

{
  "name": "MVCNotificationsAPI",
  "gcm_sender_id": "1044789039425"
}


main.js

if ('serviceWorker' in navigator) {
console.log('Service Worker is supported');
navigator.serviceWorker.register('../sw.js').then(function () {
    return navigator.serviceWorker.ready;
}).then(function (reg) {
    console.log('Service Worker is ready :^)', reg);

    reg.pushManager.subscribe({ userVisibleOnly: true }).then(function (sub) {
        console.log('endpoint:', sub.endpoint);
        document.getElementById("Hidden1").value = sub.endpoint;
    });
}).catch(function (error) {
    console.log('Service Worker error :^(', error);
});

当我们动态设置数据时,不会触发通知,我们的IDE或Firebase帐户上也不会出现任何错误

请发布。您好,我已经编辑了我的问题,并发布了复制问题的源代码。如果有任何问题,请告诉我。您好,我们已设法解决了动态问题,但现在我们面临的问题是,手机浏览器通知上没有显示图标。你能帮忙吗?@Vinetmore你是怎么解决的?我正在尝试实现相同的概念,但我没有收到浏览器上的通知。请发布。您好,我已经编辑了我的问题,并发布了复制问题的源代码。如果有任何问题,请告诉我。您好,我们已设法解决了动态问题,但现在我们面临的问题是,手机浏览器通知上没有显示图标。你能帮忙吗?@Vinetmore你是怎么解决的?我试图实现相同的概念,但我没有收到浏览器上的通知
  data = new
               {
                    body = "This is the message",
                   title = "This is the title"
               }