C# 向苹果公司发送PushSharp通知';s APNS不工作,未引发任何异常
我不知道为什么这不起作用,它也没有抛出任何错误。未触发任何订阅的事件(包括C# 向苹果公司发送PushSharp通知';s APNS不工作,未引发任何异常,c#,.net,apple-push-notifications,pushsharp,C#,.net,Apple Push Notifications,Pushsharp,我不知道为什么这不起作用,它也没有抛出任何错误。未触发任何订阅的事件(包括onnotificationsend和OnNotificationSendFailure)。该代码与PushSharp的几乎相同,唯一的区别是它在线程中运行: public class MyNotificationService { ILog _log = LogManager.GetLogger(""); PushService _PushService; public void Start()
onnotificationsend
和OnNotificationSendFailure
)。该代码与PushSharp的几乎相同,唯一的区别是它在线程中运行:
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服务.Net项目运行该问题时,才能重现该问题。在控制台应用程序中,一切正常。我以为是权限问题阻碍了网络访问,但我无法让它正常工作。您的服务是否以“本地系统”运行?如果没有,请尝试将其安装为“LocalSystem”
也许这项服务没有网络接入,所以无法连接到互联网或数据库
顺便说一句,设备令牌看起来很短,但如果它作为控制台应用程序工作,它应该作为服务工作。首先
我也有同样的问题。不会触发任何事件,但会传递通知。仍然在寻找答案。
也许会有帮助
第二(关于“StopAllServices永远挂起”)
也许会有帮助。简而言之-不应提供空设备令牌我遇到了完全相同的问题!我在我的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文件夹中 我也有类似的问题。我在ASP.NETMVC应用程序中使用了PushSharp。确保应用程序和PushSharp版本中的Newtonsoft.Json版本相同。我遇到了没有触发事件的问题。原来是因为我在调用
RegisterService
后添加了事件处理程序。它们必须在之前添加。我一直在PushService
下得到一条红线,我是否遗漏了要包含的内容?关于这个答案的更多信息:我删除了我项目中较旧的json引用,并向较新的json dll(pushsharp附带的)添加了一个引用,然后对我的配置文件做了列出的更改,它开始工作。这对我来说是修复了它。除了创建的频道外,我并没有触发任何事件,然后它会不停地跳,而不会超时或抛出异常。原来web项目引用的是Newtownsoft的旧版本-删除了旧DLL并添加了绑定重定向到新版本,收到推送通知。不确定它是否有什么不同,但我还引用了旧的JdSoft.APNS库(PushSharp的前身),我同时也删除了它。。。