gpt4 book ai didi

c# - 推送通知 Chrome Mobile Ver 42+

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:01 27 4
gpt4 key购买 nike

我正在尝试本文建议的推送通知演示。 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 developer console上创建项目。
2. 将浏览器应用程序 key 添加到 config.js > gcmAPIKey :'browser api key'
3.在manifest.json中添加gsm_sender_id :'Project number'
4. 在 https://somedomain.com 上托管应用程序
结果是这个 enter image description here


每当我单击 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+。
但是这里有一个问题,通知显示来自 service-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);
}
}

关于c# - 推送通知 Chrome Mobile Ver 42+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31853669/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com