Uma*_*mil 3 c# push-notification apple-push-notifications pushsharp
我正在尝试使用PushSharp向各种设备发送通知.我的服务器端应用程序将通知发送到MSSQL中的表,以便另一个应用程序(Bot)处理这些通知并将它们发送到Apple服务器.
我使用以下代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我将通知排队到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"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3181 次 |
| 最近记录: |