推送通知 Chrome 手机版本 42+

Push notification Chrome Mobile Ver 42+

我正在尝试本文建议的推送通知演示。 https://developers.google.com/web/updates/2015/03/push-notificatons-on-the-open-web?hl=en

并尝试 运行 来自 github https://github.com/GoogleChrome/samples/tree/gh-pages/push-messaging-and-notifications

的代码

按照说明我做了以下事情 -
1. 在 google 开发者控制台上创建项目。
2. 将浏览器应用程序密钥添加到 config.js > gcmAPIKey :'browser api key'
3.Added gsm_sender_id :'Project number' 变成 manifest.json
4. https://somedomain.com
上的托管应用程序 结果是


每当我单击 SendPushMessage 按钮时,它都不会显示任何通知。在调试时,我发现这个请求被触发了
https://xxsomedomainxxx.azurewebsites.net/api/get/?subscriptionId=APA91bE_xyl2sP8l1pa8j4n84VGfJgKVb28I0DJK5qo9zUVLy0ZSRsyl2BbjLDSZ-Y625LqsmMp3rIH4PW3s1v_ccBOdCbWYsxaF525FHRbx5odr-z1a1uPrP4zqy4DFlKkwa9pyHhkdxL0ggxGBbC_bB6LTZSDuTKlDeXTRhywcY9X5KxBXrxhS_4M8oJFUi3eW6FikEUiJ

根据我的观察,我需要在服务器上捕获 substriptionId 并需要对其进行一些处理。那么我需要在服务器上为下面写什么样的代码 API

https://xxsomedomainxxx.azurewebsites.net/api/get/

我使用以下代码创建了一个 WCF REST 服务,并将其发送通知给 android chrome 42+。
但是这里有一个问题,通知显示来自服务-worker.js 文件的消息,而不是来自此代码

的消息

var value = "Hello from server";

。我正在努力并很快更新这个答案。

Please update this function in main.js file


function getNotification(subscription) {

    navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) {
        serviceWorkerRegistration.pushManager.subscribe()
          .then(function (subscription) {
              console.log('getNotification', subscription)
              fetch('/pushnotificationservice.svc/sendnotification?key=' + subscription.subscriptionId).then(function (obj) {
                  console.log('getNotification', obj)
              });
          });
    });

WCF Service Code

public string SendNotification(string key)
        {
             ServiceData myServiceData = new ServiceData();
            try
            {
                string GoogleAppID = "2440XXXXXXXX";//put your project number here
                string redId = key;
                var SENDER_ID = "youremail@gmail.com";//I haven't tried with another junk value
                var value = "Hello from server";//this message do not displayed in notification
                WebRequest tRequest;
                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}", "Your browser key"));

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

                string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + key + "";
                Console.WriteLine(postData);
                Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                tRequest.ContentLength = byteArray.Length;

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

                WebResponse tResponse = tRequest.GetResponse();

                dataStream = tResponse.GetResponseStream();

                StreamReader tReader = new StreamReader(dataStream);

                String sResponseFromServer = tReader.ReadToEnd();

                tReader.Close();
                dataStream.Close();
                tResponse.Close();
                return sResponseFromServer;
            }
            catch (FaultException fex)
            {
                myServiceData.Result = false;
                myServiceData.ErrorMessage = "unforeseen error occurred. Please try later.";
                myServiceData.ErrorDetails = fex.ToString();
                throw new FaultException<ServiceData>(myServiceData);
            }
        }