我一直在努力从C#控制台应用程序发送推送通知。我有来自苹果开发网站的工作p12证书,我也有想要发送通知的设备的设备令牌。以下是我用来尝试发送通知的代码。我正在使用旧版本的PushSharp(2.2.1.0),因为我使用相同的控制台应用程序发送100%的Android推送通知。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PushSharp;
using PushSharp.Android;
using PushSharp.Apple;
using PushSharp.Core;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Net.Sockets;
namespace PushNotificationServiceTradeway
{
class Program
{
static void Main(string[] args)
{
var push = new PushBroker();
push.OnNotificationSent += NotificationSent;
push.OnChannelException += ChannelException;
push.OnServiceException += ServiceException;
push.OnNotificationFailed += NotificationFailed;
push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
push.OnChannelCreated += ChannelCreated;
push.OnChannelDestroyed += ChannelDestroyed;
//Registering the Apple Service and sending an iOS Notification
var appleCert = File.ReadAllBytes("Certificate");
push.RegisterAppleService(new ApplePushChannelSettings(appleCert, ""));
push.QueueNotification(new AppleNotification()
.ForDeviceToken("Phone_Token")
.WithAlert("Hello World!")
.WithBadge(7));
push.StopAllServices();
}
static void DeviceSubscriptionChanged(object sender,
string oldSubscriptionId, string newSubscriptionId, INotification notification)
{
Console.WriteLine(notification);
}
//this even raised when a notification is successfully sent
static void NotificationSent(object sender, INotification notification)
{
Console.WriteLine(notification);
}
//this is raised when a notification is failed due to some reason
static void NotificationFailed(object sender,
INotification notification, Exception notificationFailureException)
{
Console.WriteLine(notificationFailureException);
}
//this is fired when there is exception is raised by the channel
static void ChannelException
(object sender, IPushChannel channel, Exception exception)
{
Console.WriteLine(exception);
}
//this is fired when there is exception is raised by the service
static void ServiceException(object sender, Exception exception)
{
Console.WriteLine(exception);
}
//this is raised when the particular device subscription is expired
static void DeviceSubscriptionExpired(object sender,
string expiredDeviceSubscriptionId,
DateTime timestamp, INotification notification)
{
Console.WriteLine(notification);
}
//this is raised when the channel is destroyed
static void ChannelDestroyed(object sender)
{
Console.WriteLine(sender);
}
//this is raised when the channel is created
static void ChannelCreated(object sender, IPushChannel pushChannel)
{
Console.WriteLine(pushChannel);
}
}
}
但是,当我运行应用程序发送通知时,它会挂起并在控制台中出现以下错误一分钟后超时
System.Net.Sockets.Socketexception(0x80004005)连接尝试失败导致连接方在一段时间后没有正确响应或建立连接失败,因为连接的主机无法响应17.172.232.45:2196 在System.Net.Sockets.TcpClient..ctor(String hostname,int32 port) 在Pushsharp.Apple.FeedbackService.Run(ApplePushChannelSetings,CancellationToken cancelToken) 在PushSharp.Apple.ApplePushService。(ApplePushService)c_AnonStorey0。()m_1(对象状态)
有关如何解决/解决此问题的任何帮助或建议将不胜感激。
答案 0 :(得分:0)
好的找到了修复,我公司的防火墙设置阻止了对Apple推送通知服务器的访问。