如何在渐进式Web应用程序中发送推送通知

时间:2016-04-14 10:18:05

标签: google-cloud-messaging progressive-web-apps

我使用curl命令发送推送通知它正常工作。这是我的卷曲命令

curl --header "Authorization: key=GjO1y_hPm-xUdgnM25Ny4" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"dFWzS2cc7I0:APmREGNkRg8YKdlVp89vUVNTuEI0ygJ8TZ-7lhzs9wGek6nEiojzA-N2BC1dxbPpT1_VsTtM6kS9LLJ90bIK_xvt5Y2TTx6qB_GUsjYxGF3Ni4UBr7_H6NeVMIYmYPj7\"]}"

当我通过发送给用户的命令行通知执行此命令时。我想发送关于按钮click的通知。我可以这样做。

2 个答案:

答案 0 :(得分:0)

在启用按钮之前,我们需要检查一些API。以下是Push Notifications on the Open Web中给出的步骤的简要概述,其中还可以找到示例代码:

  
      
  • 我们检查ServiceWorkerRegistration原型中是否有 showNotification 。没有它,我们将无法在收到推送消息时向我们的服务人员显示通知。
  •   
  • 我们检查当前的 Notification.permission 是什么,以确保它不被“拒绝”。拒绝权限意味着在用户手动更改浏览器中的权限之前,您无法显示通知。
  •   
  • 要检查是否支持推送消息,我们检查窗口对象中是否有 PushManager
  •   
  • 最后,我们使用 pushManager.getSubscription()来检查我们是否已经有订阅。如果我们这样做,我们会将订阅详细信息发送到我们的服务器,以确保我们拥有正确的信息并设置我们的UI以指示推送消息已经启用。我们将在本文后面查看订阅对象中存在哪些详细信息。
  •   

Your first push notifications web app还可以帮助您正确实施推送通知到Web应用程序。

答案 1 :(得分:0)

我使用asp.net c#发送按钮点击通知。这是我的代码

protected void Button1_Click(object sender, EventArgs e)
    {
string RegArr = string.Empty;
RegArr = string.Join("\",\"", RegistrationID);      

string message = "some test message";
string tickerText = "example test GCM";
string contentTitle = "content title GCM";
 postData =
"{ \"registration_ids\": [ \"" + RegArr + "\" ], " +
"\"data\": {\"tickerText\":\"" + tickerText + "\", " +
"\"contentTitle\":\"" + contentTitle + "\", " +
"\"message\": \"" + message + "\"}}";

string response = SendGCMNotification("Api key", postData);
}

SendGCMNotification功能: -

private string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json")
    {

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        //  CREATE REQUEST
        HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
        Request.Method = "POST";
        Request.KeepAlive = false;
        Request.ContentType = postDataContentType;
        Request.Headers.Add(string.Format("Authorization: key={0}", apiKey));
        Request.ContentLength = byteArray.Length;

        Stream dataStream = Request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        //
        //  SEND MESSAGE
        try
        {
            WebResponse Response = Request.GetResponse();
            HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode;
            if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden))
            {
                var text = "Unauthorized - need new token";
            }
            else if (!ResponseCode.Equals(HttpStatusCode.OK))
            {
                var text = "Response from web service isn't OK";
            }
            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            string responseLine = Reader.ReadToEnd();
            Reader.Close();

            return responseLine;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return "error";
    }