gpt4 book ai didi

c# - PushSharp 通知 Apple 的 APNS 不工作,不抛出任何异常

转载 作者:行者123 更新时间:2023-12-02 05:20:48 26 4
gpt4 key购买 nike

我不知道为什么这不起作用,也没有抛出任何错误。所有订阅的事件都不会被触发(包括 OnNotificationSentOnNotificationSendFailure)。该代码几乎与 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 项目运行它时,问题才会重现。从控制台应用程序,一切正常。我认为这是阻止网络访问的权限问题,但我无法让它工作。

最佳答案

我遇到了完全相同的问题!我在我的 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 文件夹中!

关于c# - PushSharp 通知 Apple 的 APNS 不工作,不抛出任何异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13871806/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com