- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个 C# 网络应用程序,我正在尝试使用 PushSharp 包来获取推送通知。我在项目的 Global.asax 文件中拥有用于推送通知的所有代码,但我不断收到错误消息:
The collection has been marked as complete with regards to additions.
这是我的 Global.asax 文件:
using BYC.Models;
using BYC.Models.Enums;
using Newtonsoft.Json.Linq;
using PushSharp.Apple;
using PushSharp.Google;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace BYC
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
protected void Application_End()
{
PushBrokerSingleton pbs = new PushBrokerSingleton();
pbs.SendQueuedNotifications();
}
}
public sealed class PushBrokerSingleton
{
private static ApnsServiceBroker Apns { get; set; }
private static GcmServiceBroker Gcm { get; set; }
private static bool ApnsStarted = false;
private static bool GcmStarted = false;
private static object AppleSyncVar = new object();
private static object GcmSyncVar = new object();
private static readonly log4net.ILog log = log4net.LogManager.GetLogger
(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public PushBrokerSingleton()
{
if (Apns == null)
{
string thumbprint = (AppSettings.Instance["APNS:Thumbprint"]);
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
ApnsConfiguration.ApnsServerEnvironment production = Convert.ToBoolean(AppSettings.Instance["APNS:Production"]) ?
ApnsConfiguration.ApnsServerEnvironment.Production : ApnsConfiguration.ApnsServerEnvironment.Sandbox;
X509Certificate2 appleCert = store.Certificates
.Cast<X509Certificate2>()
.SingleOrDefault(c => string.Equals(c.Thumbprint, thumbprint, StringComparison.OrdinalIgnoreCase));
ApnsConfiguration apnsConfig = new ApnsConfiguration(production, appleCert);
Apns = new ApnsServiceBroker(apnsConfig);
Apns.OnNotificationFailed += (notification, aggregateEx) => {
aggregateEx.Handle(ex => {
// See what kind of exception it was to further diagnose
if (ex is ApnsNotificationException)
{
var notificationException = ex as ApnsNotificationException;
// Deal with the failed notification
var apnsNotification = notificationException.Notification;
var statusCode = notificationException.ErrorStatusCode;
log.Error($"Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");
}
else {
// Inner exception might hold more useful information like an ApnsConnectionException
log.Error($"Notification Failed for some (Unknown Reason) : {ex.InnerException}");
}
// Mark it as handled
return true;
});
};
Apns.OnNotificationSucceeded += (notification) => {
log.Info("Notification Successfully Sent to: " + notification.DeviceToken);
};
}
if(Gcm == null)
{
GcmConfiguration gcmConfig = new GcmConfiguration(AppSettings.Instance["GCM:Token"]);
Gcm = new GcmServiceBroker(gcmConfig);
}
}
public bool QueueNotification(Notification notification, Device device)
{
if (!ApnsStarted)
{
ApnsStarted = true;
lock (AppleSyncVar)
{
Apns.Start();
}
}
if(!GcmStarted)
{
GcmStarted = true;
lock (GcmSyncVar)
{
Gcm.Start();
}
}
switch (device.PlatformType)
{
case PlatformType.iOS:
return QueueApplePushNotification(notification, device.PushRegistrationToken);
case PlatformType.Android:
return QueueAndroidPushNotification(notification, device.PushRegistrationToken);
default: return false;
}
}
private bool QueueApplePushNotification(Notification notification, string pushNotificationToken)
{
string appleJsonFormat = "{\"aps\": {\"alert\":" + '"' + notification.Subject + '"' + ",\"sound\": \"default\", \"badge\": " + notification.BadgeNumber + "}}";
lock (AppleSyncVar)
{
Apns.QueueNotification(new ApnsNotification()
{
DeviceToken = pushNotificationToken,
Payload = JObject.Parse(appleJsonFormat)
});
}
return true;
}
private bool QueueAndroidPushNotification(Notification notification, string pushNotificationToken)
{
string message = "{\"alert\":\"" + notification.Subject + "\",\"badge\":" + notification.BadgeNumber + "\"}";
lock (GcmSyncVar)
{
Gcm.QueueNotification(new GcmNotification()
{
RegistrationIds = new List<string>
{
pushNotificationToken
},
Data = JObject.Parse(message),
Notification = JObject.Parse(message)
});
}
return true;
}
public void SendQueuedNotifications()
{
if(Apns != null)
{
if (ApnsStarted)
{
lock(AppleSyncVar){
Apns.Stop();
log.Info("Sent Apns Notifications");
ApnsStarted = false;
}
}
}
if(Gcm != null)
{
if (GcmStarted)
{
lock (GcmSyncVar)
{
Gcm.Stop();
log.Info("Sent Gcm Notifications");
GcmStarted = false;
}
}
}
}
}
最佳答案
当您尝试重用 ApnsServiceBroker
的服务代理实例(例如:Stop()
)时,就会发生这种情况。已被调用。
我猜你的 Application_End
在某个时候被调用并且Application_Start
再次被调用,但自 PushBrokerSingleton.Apns
不为空(它是一个静态字段,因此即使应用程序已停止/启动,它也必须继续存在),它永远不会被重新创建。
PushSharp 很难与 ASP.NET 模式很好地协同工作,某种服务守护进程会更好。
主要问题是您的应用可能会在您不希望的情况下被回收或终止。同一个应用程序中的不相关请求可能会导致您的进程中断,或者您的 AppDomain 可能会被拆除。如果发生这种情况,经纪人的 Stop()
调用无法成功结束,一些排队的消息可能会丢失。这是一篇关于一些注意事项的好文章:http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/在实践中,这可能不是什么大问题,您当然可以减轻部分影响,但请记住这一点。
说了这么多,我认为一个简单的解决方法是创建一个新的 PushBrokerSingleton.Apns
实例。和 PushBrokerSingleton.Gcm
在你的Application_Start
.这可能会给您带来其他问题,所以我不确定它是否是正确的修复,但它可以解决在 Stop()
之后不打算重用代理的问题。已被调用。
我还将考虑添加一些方法来“重置”集合。我不确定在 .Stop()
之后是否会自动执行此操作ends 是个好主意,但我可能会考虑添加一个 .Reset()
或类似的方法来实现这一点。无论如何,现在创建一个新的代理实例是完全可以接受的。
关于c# - PushSharp 关注点分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36049744/
我使用 PushSharp 发送一些应用程序的通知。PushSharp 很棒,它确实简化了推送服务的工作,我想知道使用它的正确方法是什么?我还没有找到相关的示例/解释。 现在,当我有消息要发送时,我.
我正在实现 Apple 通知推送服务并正在处理内部队列。 我想知道 PushSharp 通知是否总是引发“NotificationSent”或“NotificationFailed”事件(一个或另一个
我在我的应用程序中使用 PushSharp 发送推送通知。它在 Android 上运行良好。 对于 Apple iOS - 它不工作,我们正在使用带密码的开发证书文件。最初我们使用了错误的密码,后来我
当我在生产环境中使用具有以下推送通知证书的企业帐户时,我的应用程序正在收到推送通知: 苹果生产 iOS 推送服务 然后为了在应用商店发布我的应用程序,我开始使用应用商店帐户,无论我尝试什么,苹果都会以
我目前正在开发一个 C# 网络应用程序,我正在尝试使用 PushSharp 包来获取推送通知。我在项目的 Global.asax 文件中拥有用于推送通知的所有代码,但我不断收到错误消息: The co
我们正在使用 PushSharp发送 GCM (Google Android) 通知。将来我们还将使用该库的 IOS 和 WinPhone 部分。 有一些事件,例如: OnDeviceSubscrip
我有一个简单的代码: PushBroker pushBroker = new PushBroker(); string path = HttpContext.Current.Server.MapPat
使用 Pushsharp(nuget 包)的 v4.0.10,出现错误 GCM Authorization Failed向 Android 设备发送推送通知时。 似乎问题 574 ( https://
我正在开发一个针对 iOS 和 Android 的 Xamarin Forms 项目。因为我想要推送通知,所以我遇到了 PushSharp。在此视频中(大约 25 - 30 分钟):https://w
我正在使用 PushSharp 4.0.10在 Apns 代理的 OnNotificationFailed 事件中,我收到“不支持请求的功能”异常。有我的 Broker 创建者 private sta
我正在尝试测试与 Apple 的推送通知沙箱服务器的对话。 我按照this link做了一个证书(启用推送通知、请求证书颁发机构、上传、生成证书、导出 p12) 我制作了一个示例 C# 控制台应用程序
为了加快大量 Apple 推送通知 (APN) 的发送速度,建议并行调用 APN(APN 服务器)- 最多 20 个并发调用。 C#跨平台推送库吗PushSharp iOS 通知默认这样做吗?如果没有
我有如下所示的标准 pushsharp 代码 PushBroker push = new PushBroker(); var appleCert = File.ReadAllBytes(@"m
我正在使用 PushSharp向应用程序发送 iOS 通知。 我已经按照示例设法使其在开发环境中工作,但是当我尝试在生产环境中发送它们时,出现以下错误: InnerException = {"No s
我正在使用 APNS Sharp 在 IOS 中发送推送通知。我已经在开发环境中成功地使用了它。但是当应用程序运行时它不再工作。我已将推送证书更改为部署证书。并更改代码以指向苹果服务器 push.Re
PushSharp 仅在调用停止时处理队列。有谁知道 push sharp 处理队列或刷新队列的频率?我不想每次发送通知时都调用 stop 和 start 来节省资源。 pus
我使用的是 PushSharp 2.1.2 版。该应用程序是 .NET 4.5.1(尽管我也尝试过针对 .NET 4.5 和 .NET 4) 我正在尝试通过沙盒 Apple APNS 发送推送消息,但
我们已经实现了 PushSharp 来向我们的企业 iOS 设备发送推送通知。这是从 Windows 机器发送的,我们自定义了 PushSharp 代码,以便从计划每分钟运行一次的控制台应用程序调用(
我的 Android 和 Apple 推送通知与 PushSharp 配合使用,但我无法通过 Apple 通知发送自定义数据。 这发送完美: push.QueueNotification(new Ap
所以我在我的 ASP.NET mvc 应用程序中使用 push sharp,但我一直收到连接失败错误。我为 push sharp 安装了 NUGET 包,所以我无法访问源代码。我花了一段时间才意识到我
我是一名优秀的程序员,十分优秀!