net-core-push-notifications
net-core-push-notifications copied to clipboard
Version 3.1.1 & 3.1.0 - ApnSender Fails With Serialization Error (.Net Core 6.0.7)
Hi,
We are not able to send notifications via Apn with version 3.1.1 and 3.1.0 (older ones work!). ApnSender fails with:
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0.
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonReader.ReadAndMoveToContent()
at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at CorePush.Utils.JsonHelper.Deserialize[TObject](String json)
at CorePush.Apple.ApnSender.SendAsync(Object notification, String deviceToken, String apnsId, Int32 apnsExpiration, Int32 apnsPriority, Boolean isBackground, CancellationToken cancellationToken)
Thanks!
very strange.. the version 3.1.1 works well for me. It seems like it's a parsing error for when the response is received from the APN server. I wonder what it is.. Would be good to debug it. I'd be curious to see what you get in ApnSender in the line:
var content = await response.Content.ReadAsStringAsync();
Hey @andrei-m-code!
Okay I spent some time with it - you are right, it seems to fail when the error is parsed:
using (var response = await http.SendAsync(message, cancellationToken))
{
var succeed = response.IsSuccessStatusCode;
var content = await response.Content.ReadAsStringAsync();
var error = JsonHelper.Deserialize<ApnsError>(content); <-- Here
return new ApnsResponse
{
IsSuccess = succeed,
Error = error
};
}
The Content I got is the following:
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>404.</b> <ins>That’s an error.</ins>
<p>The requested URL <code>/3/device/C689B30554D56AAD13ED65D7D305C85235F79029233997D98484C0537F3183A6</code> was not found on this server. <ins>That’s all we know.</ins>
I did this several times and every time the same html came back with Google references (sending via Apple), also not sure why the html is not closed...but I am unsure what the format is supposed to be of these.
Does this point you in the right direction? Thanks!
Hey @andrei-m-code!
I spent some more time on it and found the issue!
Basically, I have shared the same HttpClient instance with FcmSender and ApnSender which has worked fine up until 3.1.0.
But with 3.1.1 the HttpClient BaseAddress is now set within the FcmSender and ApnSender constructors once, while the previous version did that during sending every time (within the SendAsync function) - so that was it!
For anyone who has the same issue - simply use a separate HttpClient instance for FcmSender and ApnSender. Thanks!