我不知道为什么这不起作用,也没有抛出任何错误。没有触发任何订阅的事件(包括OnNotificationSent
和OnNotificationSendFailure
)。代码几乎与PushSharp的sample code in 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项目运行时,只能重现问题。从控制台应用程序,一切正常。我认为这是一个阻止网络访问的权限问题,但我无法使其正常工作,
答案 0 :(得分:4)
我有完全相同的问题!我在我的exe引用的dll中使用了push sharp。我通过在app.config中添加以下内容来解决问题:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
确保Newtonsoft.Json dll位于bin文件夹中!
答案 1 :(得分:1)
我有类似的问题。我在ASP.Net MVC应用程序中使用了PushSharp。确保应用程序和PushSharp版本中的Newtonsoft.Json版本相同。
答案 2 :(得分:0)
您的服务是否以“LocalSystem”运行?如果不是,请尝试将其安装为“LocalSystem”。
也许该服务没有网络访问权限,因此无法连接到Internet或数据库。
顺便说一下,设备令牌看起来很短,但如果它作为控制台应用程序运行,它应该可以作为服务使用。
答案 3 :(得分:0)