使用 PushSharp 和 C# 向 iOS 设备发送推送通知
Sending Push notifications to iOS devices using PushSharp and C#
我一直在努力获取要从 C# 控制台应用程序发送的推送通知。我有一个来自苹果开发网站的有效 p12 证书,我也有我想要发送通知的设备的设备令牌。下面是我用来尝试发送通知的代码。我正在使用旧版本的 PushSharp (2.2.1.0),因为我正在使用相同的控制台应用程序发送 android 推送通知,该通知可以 100%.
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(字符串主机名,int32 端口)
在 Pushsharp.Apple.FeedbackService.Run(ApplePushChannelSetings,CancellationToken cancelToken)
在 PushSharp.Apple.ApplePushService.(ApplePushService)c_AnonStorey0.()m_1(对象状态)
任何关于如何fix/solve的帮助或建议,我们将不胜感激。
好的,找到了解决方法,这是我公司的防火墙设置阻止访问 Apple 的推送通知服务器。
我一直在努力获取要从 C# 控制台应用程序发送的推送通知。我有一个来自苹果开发网站的有效 p12 证书,我也有我想要发送通知的设备的设备令牌。下面是我用来尝试发送通知的代码。我正在使用旧版本的 PushSharp (2.2.1.0),因为我正在使用相同的控制台应用程序发送 android 推送通知,该通知可以 100%.
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(字符串主机名,int32 端口) 在 Pushsharp.Apple.FeedbackService.Run(ApplePushChannelSetings,CancellationToken cancelToken) 在 PushSharp.Apple.ApplePushService.(ApplePushService)c_AnonStorey0.()m_1(对象状态)
任何关于如何fix/solve的帮助或建议,我们将不胜感激。
好的,找到了解决方法,这是我公司的防火墙设置阻止访问 Apple 的推送通知服务器。