- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在为我的 Android 应用程序使用 Google 云消息传递 (GCM) 服务。我已经按照所有规则实现了它,并且有效。嗯,差不多。
大多数情况下,我会说在 60-70% 的情况下,我可以使用 google 网页上讨论的网络服务从我的服务器成功发送 GCM 消息。
正常情况下,我会从网络服务收到以下回复,这表明我已成功发送 GCM 消息:
{
"multicast_id":8378088572050307085,
"success":1,
"failure":0,
"canonical_ids":0,
"results":
[
{
"message_id":"0:1363080282442710%7c4250c100000031"
}
]
}
这是在说:一切正常,消息已发送。
但是,在很多情况下,我在调用网络服务时会遇到 HTTP 错误,即:
Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.
这是告诉我调用网络服务(使用 HttpWebRequest 和 POST)失败的 .NET 消息。
这是一些显示问题的日志消息:
这是我用来调用 WS 的代码:
public static string SendMessage(string registrationId, string command, string extra, bool retry)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
request.Method = PostWebRequest;
request.KeepAlive = false;
GCMPostPacket json = new GCMPostPacket()
{
collapse_key = "1",
time_to_live = 60,
registration_ids = new List<string>(new string[] { registrationId }),
data = new GcmData()
{
message = command,
misc = extra
}
};
// Converting to JSON string
string jsonString = SICJsonProtocol.JSONHelper.Serialize<GCMPostPacket>(json);
byte[] byteArray = Encoding.UTF8.GetBytes(jsonString);
request.ContentType = "application/json";
request.ContentLength = byteArray.Length;
request.ProtocolVersion = HttpVersion.Version10;
request.Headers.Add(HttpRequestHeader.Authorization, "key=" + "MyVerySecretKey");
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
using (WebResponse response = request.GetResponse())
{
HttpStatusCode responseCode = ((HttpWebResponse)response).StatusCode;
if (responseCode.Equals(HttpStatusCode.Unauthorized) || responseCode.Equals(HttpStatusCode.Forbidden))
{
Console.WriteLine("Unauthorized - need new token");
}
else if (!responseCode.Equals(HttpStatusCode.OK))
{
Console.WriteLine("Response from web service not OK :");
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
}
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseLine = reader.ReadLine();
Console.WriteLine("************************");
Console.WriteLine("GCM send: " + responseCode + " | " + responseLine);
// This is the log shown in the image above
SRef.main.gui.ServiceUpdate("GCM send: " + responseCode + " | " + responseLine);
reader.Close();
response.Close();
return responseLine;
}
}
catch (Exception e)
{
// This is the log shown in the image above
SRef.main.gui.ServiceUpdate("Failed send GCM, " + (retry ? "retrying in 20 sec" : "not retrying") + ". Error=" + e.Message);
if (retry)
{
System.Threading.ThreadPool.QueueUserWorkItem(delegate(object obj)
{
try
{
System.Threading.Thread.Sleep(20000);
SendMessage(registrationId, command, extra, false);
}
catch (Exception ex)
{
}
});
}
return null;
}
}
任何人都可以看到我是否做错了什么,或者我是否遗漏了什么?
最佳答案
Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.
此错误与 GCM API 无关。这意味着您的客户端尝试联系 Web 服务,但建立的连接已在其中一个网络层中被切断。根据您收到此错误的位置和错误消息,它可能意味着几件事。
由于套接字超时,您的客户端决定中止连接。增加客户端读取/套接字超时以改善这种情况。
位于客户端和服务器之间的负载平衡器断开了连接。每一方都认为对方断开了连接。
网络超时通常是一个巨大的痛苦,因为永远不清楚连接在哪里断开以及是谁断开的。如果增加超时没有帮助,我建议通过可以嗅探 HTTPS 流量的代理发送请求(charles/TCPMON)或使用 Wireshark 查看哪些数据包被丢弃。
您的 Android 应用程序还具有 GCM 监控功能,您可以在“统计信息”选项卡上启用该功能。检查 GCM API 是否在该图表上报告了 200 OK 以外的状态消息。这将有助于进一步缩小问题范围。如果没有除 200 以外的状态代码报告,则意味着 GCM 从未收到您的 API 请求。
关于android - 发送 GCM 消息(服务器端)经常失败——但并非总是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15357577/
我想我理解 GCM 的新 notification_key 功能背后的基本原理是“只需将消息发送给用户并且只让他们阅读一次”。我从文档的这一部分推断出来: With user notification
我实现了网络推送通知。获取错误的步骤: 打开网站 订阅推送通知 通过 gcm 发送许多推送 - 一切正常 关闭网站标签 发送推送和接收“双推送”- 第一个正常,第二个是“此站点已在后台更新” 重新打开
我正在手机上测试 GCM。 (2.3.6 安卓). list 文件(MainActivity、First 和 Second Activity 不执行任何操作,它们用于其他一些测试目的,不干扰 GCM)
首先,我是 iOS 开发的新手,也是 swift 的新手。我正在尝试让适用于 iOS 的谷歌云消息传递示例。 我已经从 https://developers.google.com/cloud-mess
你们中的任何人都可以提供 GCM 推送通知的代码,其中消息从移动设备推送到 GCM 服务器。我已经从 GCM 服务器到移动设备及其工作进行了操作,但我不知道反之亦然如何操作。任何人都可以提供帮助吗?谢
我们有两个不同的应用程序,但都需要来自 Google Cloud Messages 的相同推送通知,并且我在两个应用程序中只使用了一个发件人 ID(启用 Google 控制台之一的项目编号)。 这样做
我有一个广播接收器,当应用程序打开并在后台时,它会检测即将到来的通知,但当最近的应用程序被清除时,接收器无法工作,请给我建议。 public class GcmBroadcastReceiver ex
我想从我的应用程序向 GCM 服务器发送一个心跳信号,以便连接保持有效。 我该怎么做,我怎么知道我的 GCM 服务器的 URL? 提前致谢! 最佳答案 如何发送心跳 这个类可以发送正确的 Intent
新的 GCM 3.0 应该允许 GCM 自动显示从服务器发送的通知,如果它们包含 notification 参数。 如 docs 中所述: The notification parameter wit
这个问题在这里已经有了答案: Do I need to migrate GCM to FCM on client side? (2 个答案) 关闭 3 年前。 Google 已于 2018 年 4
我遇到了 GCM 推送通知无法正确到达 Android 设备的问题。经过几天的研究,我发现 Android 设备使用心跳来保持与 GCM 服务的连接。遗憾的是,心跳似乎太高了,因此 Android 设
阅读堆栈溢出 2 天后我做了什么: 问题关键字:“Apple-Mach-O 链接器错误”、“libGcmLib.a(GCMRmqManager.o) ", sqlite3", "GCMRmq2Pers
我已经从 GCM 订阅了主题,当我通过 Android 设置删除所有应用程序数据时,GCM token 是相同的,关于主题的 GCM 通知仍然可用,所以我收到了我不想收到的通知。 我的问题是: 如何从
由于 gcm 已弃用,我们希望迁移我们的代码。正如在谷歌的迁移指南中提到的那样,对于我们的服务器应用程序,应该只需要将端点从 gcm 更改为 fcm。应用迁移已成功完成。 我们现在使用的是 com.g
这只发生在我的 Kyocera Rise 上。我有一个依赖 GCM 在手机之间进行通信的应用程序。我的 Nexus 4 和我的 HTC One X 之间的通信工作正常,每当我发送推送通知时,两部手机都
我遇到了与 this 相同的问题.我会尝试提供更多信息。 我正在使用 Play Framework,用 Java 编写。我写了一个叫做 PushNotificationQueue 的插件。 PushN
我需要在我的应用程序中接收来自不同发件人的推送通知。会成功吗? 最佳答案 你的问题的答案是是! 根据 GCM 的官方文档,您的应用可以接收来自多个发件人的消息(限制为 100 个不同的发件人),并且您
我有一个使用 GCM 推送通知的应用程序。它工作正常,我的设备注册并接收推送消息。 如果我从我的设备上卸载该应用程序,我将不再像您期望的那样收到消息。在我卸载应用程序后,您在服务器上发送消息的文本框仍
我正在处理另一个开发人员的现有项目,我需要获取 PubNub 设置的 GCM 服务器 key ,因为它被意外删除了。 有什么方法可以从 Google 设置中检索它?我在凭据和项目设置中找不到它。 我暂
嘿,我在调用 `subscribeToTopicP 类时遇到了一些 Gcm Intent 服务的问题,总是出现空指针异常。 这是我的代码: GcmIntentService.java private
我是一名优秀的程序员,十分优秀!