C# 未使用PushSharp发送iOS通知。也不会引发任何事件
我正在尝试使用PushSharp向各种设备发送通知。我的服务器端应用程序注册要发送到MSSQL表中的通知,以便另一个应用程序(Bot)处理这些通知并将其发送到苹果服务器 我正在使用以下代码:C# 未使用PushSharp发送iOS通知。也不会引发任何事件,c#,push-notification,apple-push-notifications,pushsharp,C#,Push Notification,Apple Push Notifications,Pushsharp,我正在尝试使用PushSharp向各种设备发送通知。我的服务器端应用程序注册要发送到MSSQL表中的通知,以便另一个应用程序(Bot)处理这些通知并将其发送到苹果服务器 我正在使用以下代码: static DataEntities Entities; static void Main(string[] args) { Entities = new DataEntities(); var appleCert = File.ReadAllByte
static DataEntities Entities;
static void Main(string[] args)
{
Entities = new DataEntities();
var appleCert = File.ReadAllBytes(Path.GetFullPath("My_Push_Notifications.p12"));
PushBroker broker = new PushBroker();
broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(false, appleCert, "XXXXX"));
broker.OnChannelCreated += broker_OnChannelCreated;
broker.OnChannelDestroyed += broker_OnChannelDestroyed;
broker.OnChannelException += broker_OnChannelException;
broker.OnNotificationRequeue += broker_OnNotificationRequeue;
broker.OnServiceException += broker_OnServiceException;
broker.OnNotificationSent += broker_OnNotificationSent;
broker.OnNotificationFailed += broker_OnNotificationFailed;
while (true)
{
var pendingNotifications = Entities.Notifications.Include("UserDevice").Where(n => n.Status == (byte)Constants.Notifications.Status.Pending);
if (pendingNotifications.ToList().Count > 0)
{
Console.WriteLine("Pending notifications: {0}", pendingNotifications.Count());
}
else
Console.WriteLine("No pending notifications");
foreach (var notification in pendingNotifications)
{
broker.QueueNotification<AppleNotification>(new AppleNotification()
.ForDeviceToken(notification.UserDevice.DeviceID)
.WithAlert(notification.Text)
.WithTag(notification.NotificationID));
notification.Status = (byte)Constants.Notifications.Status.Sending;
}
Entities.SaveChanges();
Thread.Sleep(2000);
}
}
静态数据实体;
静态void Main(字符串[]参数)
{
实体=新的数据实体();
var appleCert=File.ReadAllBytes(Path.GetFullPath(“My_Push_Notifications.p12”);
PushBroker broker=新的PushBroker();
RegisterAppleService(新的PushSharp.Apple.ApplePushChannelSettings(false,appleCert,“XXXXX”);
broker.OnChannelCreated+=broker\u OnChannelCreated;
broker.onchanneldestromed+=broker\u onchanneldestromed;
broker.OnChannelException+=broker\u OnChannelException;
broker.OnNotificationRequeue+=broker_OnNotificationRequeue;
broker.OnServiceException+=broker\u OnServiceException;
broker.OnNotificationSent+=broker\u OnNotificationSent;
broker.OnNotificationFailed+=broker\u OnNotificationFailed;
while(true)
{
var pendingNotifications=Entities.Notifications.Include(“用户设备”)。其中(n=>n.Status==(字节)常量。Notifications.Status.Pending);
如果(pendingNotifications.ToList().Count>0)
{
WriteLine(“挂起的通知:{0}”,pendingNotifications.Count());
}
其他的
Console.WriteLine(“无未决通知”);
foreach(pendingNotifications中的var通知)
{
broker.QueueNotification(新应用程序化()
.ForDeviceToken(notification.UserDevice.DeviceID)
.WithAlert(notification.Text)
.WithTag(notification.NotificationID));
notification.Status=(字节)Constants.Notifications.Status.Sending;
}
Entities.SaveChanges();
《睡眠》(2000年);
}
}
如您所见,我将通知排队发送给PushBroker,但从未调用任何事件,并且iOS设备没有收到任何消息。我还尝试在循环结束之前使用“StopAllServices”,但没有任何变化
这怎么可能呢
谢谢。我解决了这个问题。
PushSharp没有引发事件,因为在代理上注册apple服务之前必须添加事件处理程序。因此正确的代码是:
PushBroker broker = new PushBroker();
broker.OnChannelCreated += broker_OnChannelCreated;
broker.OnChannelDestroyed += broker_OnChannelDestroyed;
broker.OnChannelException += broker_OnChannelException;
broker.OnNotificationRequeue += broker_OnNotificationRequeue;
broker.OnServiceException += broker_OnServiceException;
broker.OnNotificationSent += broker_OnNotificationSent;
broker.OnNotificationFailed += broker_OnNotificationFailed;
// Now you can register the service.
broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(false, appleCert, "XXXXX"));
另一方面,Android通知也是如此。在注册GCM服务之前必须添加处理程序。目前还没有通知和事件。没有例外。完全沉默。即使在注册服务之前添加了事件处理程序。@ilyabreev如果您与其他用户遵循相同的过程,问题可能出在代码的其他地方。您确定通知已正确排队吗?