如何在渐进式网络应用程序中发送推送通知
How to send push notifications in progressive web app
我正在使用 curl 命令发送推送通知它有效 properly.Here 是我的 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\"]}"
当我通过传送到 user.I 的命令行通知执行此命令时,我想在按钮 click.how 上发送通知,我可以这样做吗?
在启用我们的按钮之前,我们需要检查许多 API。以下是 Push Notifications on the Open Web 中给出的步骤的简要概述,其中还可以找到示例代码:
- We check that showNotification is available in the ServiceWorkerRegistration prototype. Without it we won’t be able to show a notification from our service worker when a push message is received.
- We check what the current Notification.permission is to ensure it’s not “denied”. A denied permission means that you can’t show notifications until the user manually changes the permission in the browser.
- To check if push messaging is supported we check that PushManager is available in the window object.
- Finally, we used pushManager.getSubscription() to check whether we already have a subscription or not. If we do, we send the subscription details to our server to ensure we have the right information and set our UI to indicate that push messaging is already enabled or not. We’ll look at what details exist in the subscription object later in this article.
Your first push notifications web app 还可以帮助您了解如何正确地向 Web 应用程序实施推送通知。
我使用 asp.net c# 在按钮上发送通知 click.here 是我的代码
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);
}
发送GCMNotification函数:-
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";
}
我正在使用 curl 命令发送推送通知它有效 properly.Here 是我的 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\"]}"
当我通过传送到 user.I 的命令行通知执行此命令时,我想在按钮 click.how 上发送通知,我可以这样做吗?
在启用我们的按钮之前,我们需要检查许多 API。以下是 Push Notifications on the Open Web 中给出的步骤的简要概述,其中还可以找到示例代码:
- We check that showNotification is available in the ServiceWorkerRegistration prototype. Without it we won’t be able to show a notification from our service worker when a push message is received.
- We check what the current Notification.permission is to ensure it’s not “denied”. A denied permission means that you can’t show notifications until the user manually changes the permission in the browser.
- To check if push messaging is supported we check that PushManager is available in the window object.
- Finally, we used pushManager.getSubscription() to check whether we already have a subscription or not. If we do, we send the subscription details to our server to ensure we have the right information and set our UI to indicate that push messaging is already enabled or not. We’ll look at what details exist in the subscription object later in this article.
Your first push notifications web app 还可以帮助您了解如何正确地向 Web 应用程序实施推送通知。
我使用 asp.net c# 在按钮上发送通知 click.here 是我的代码
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);
}
发送GCMNotification函数:-
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";
}