我不知道为什么这不起作用,也没有抛出任何错误。任何订阅的事件都不会被触发(包括OnNotificationSent
和OnNotificationSendFailure
)。该代码几乎与 PushSharp在 GIT 中的示例代码相同,唯一的区别是它在线程内运行:
public class MyNotificationService
{
ILog _log = LogManager.GetLogger("");
PushService _PushService;
public void Start()
{
PushService _PushService;
byte[] appleCert = File.ReadAllBytes("myCert.p12");
_PushService = new PushService();
_PushService.Events.OnChannelCreated += Events_OnChannelCreated;
_PushService.Events.OnChannelDestroyed += Events_OnChannelDestroyed;
_PushService.Events.OnChannelException += Events_OnChannelException;
_PushService.Events.OnDeviceSubscriptionExpired += Events_OnDeviceSubscriptionExpired;
_PushService.Events.OnDeviceSubscriptionIdChanged += Events_OnDeviceSubscriptionIdChanged;
_PushService.Events.OnNotificationSendFailure += Events_OnNotificationSendFailure;
_PushService.Events.OnNotificationSent += Events_OnNotificationSent;
_PushService.StartApplePushService(new ApplePushChannelSettings(false, appleCert, "myPass"));
_MainThread = new Thread(() =>
{
try
{
var nt = NotificationFactory.Apple()
.ForDeviceToken("60A378B0FF0628FB52461C6F9F2CEDAA29A05D52F97EF2E811")
.WithAlert("Test")
.WithSound("default")
.WithCustomItem("data", "some other data")
.WithBadge(7);
_PushService.QueueNotification(nt);
}
catch (Exception e)
{
_log.Error("In main thread", e);
}
}
});
_MainThread.Start();
}
static void Events_OnDeviceSubscriptionIdChanged(PlatformType platform, string oldDeviceInfo, string newDeviceInfo, Notification nt)
{
//Currently this event will only ever happen for Android GCM
_log.Debug("Device Registration Changed: Old-> " + oldDeviceInfo + " New-> " + newDeviceInfo);
}
static void Events_OnNotificationSent(Notification nt)
{
_log.Debug("Sent: " + nt.Platform.ToString() + " -> " + nt.ToString());
}
static void Events_OnNotificationSendFailure(Notification nt, Exception notificationFailureException)
{
_log.Error("Failure: " + nt.Platform.ToString() + " -> " + notificationFailureException.Message + " -> " + nt.ToString());
}
static void Events_OnChannelException(Exception exception, PlatformType platformType, Notification nt)
{
_log.Error("Channel Exception: " + platformType.ToString() + " -> " + exception.ToString());
}
static void Events_OnDeviceSubscriptionExpired(PlatformType platform, string deviceInfo, Notification nt)
{
_log.Debug("Device Subscription Expired: " + platform.ToString() + " -> " + deviceInfo);
}
static void Events_OnChannelDestroyed(PlatformType platformType, int newChannelCount)
{
_log.Debug("Channel Destroyed for: " + platformType.ToString() + " Channel Count: " + newChannelCount);
}
static void Events_OnChannelCreated(PlatformType platformType, int newChannelCount)
{
_log.Debug("Channel Created for: " + platformType.ToString() + " Channel Count: " + newChannelCount);
}
}
同样,没有任何事件被触发(没有记录,没有命中断点)。奇怪的是,呼叫StopAllServices
永远挂起,显然没有做任何事情。我已经多次检查了我的配置文件和证书,但没有发现它们有任何问题。有任何想法吗?
编辑:
事实证明,只有当我从 Windows Service .Net 项目运行它时,才能重现该问题。从控制台应用程序,一切正常。我认为这是阻止网络访问的权限问题,但我无法使其工作。