在 c# 中使用 Pushsharp 的 IOS 推送通知
Posted
技术标签:
【中文标题】在 c# 中使用 Pushsharp 的 IOS 推送通知【英文标题】:IOS Push notification using Pushsharp in c# 【发布时间】:2016-04-14 11:27:01 【问题描述】:我已经在 c# 中为 ios 中的推送通知开发了代码,但它没有在移动设备中发送通知。 我用过 pushsharp 库。
我的代码如下:
PushNotificationApple pushNotification = new PushNotificationApple();
pushNotification.SendNotification(postData);
我的 PushNotificationApple 构造函数代码如下:-
public PushNotificationApple()
if (_pushBroker == null)
//Create our push services broker
_pushBroker = new PushBroker();
//Wire up the events for all the services that the broker registers
_pushBroker.OnNotificationSent += NotificationSent;
_pushBroker.OnChannelException += ChannelException;
_pushBroker.OnServiceException += ServiceException;
_pushBroker.OnNotificationFailed += NotificationFailed;
_pushBroker.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
_pushBroker.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
_pushBroker.OnChannelCreated += ChannelCreated;
_pushBroker.OnChannelDestroyed += ChannelDestroyed;
var appleCert = File.ReadAllBytes(System.Web.Hosting.HostingEnvironment.MapPath("~/Certificates" + ConfigSettings.SnaptymAPNSCertificate));
_pushBroker.RegisterAppleService(new ApplePushChannelSettings(false, appleCert,ConfigSettings.SnaptymAPNSPassword)); //Extension method
我的 SendNotification 功能如下:-
public bool SendNotification(GcmNotificationPostDataModel postData)
if (_pushBroker != null)
foreach (var registrationId in postData.RegistrationIds)
_pushBroker.QueueNotification(new AppleNotification()
.ForDeviceToken(registrationId) //the recipient device id
.WithAlert(postData.Data.Message) //the message
.WithBadge(1)
.WithSound("sound.caf"));
return true;
【问题讨论】:
您收到什么错误?您可以在推送代理的不同事件中跟踪该过程。检查是否有异常 确保你不是这种情况***.com/questions/28499395/… @patel 很好地呼吁生产环境,但目前 OP 正在发送到沙箱。可以在new ApplePushChannelSettings(false, ....)
的第一个参数中看到,而且,这会引发异常。
您的代码正在向苹果的沙箱服务器发送消息。确保您的 ios 应用程序也配置为在沙盒服务器上注册(即使用开发配置文件构建!),就像@patel 所说:为所有推送代理的事件添加处理程序,并查看是否引发任何错误事件。
【参考方案1】:
我正在使用 PushSharp 4.0.10,以下代码适用于我。
private void SendMessage()
//IOS
var MainApnsData = new JObject();
var ApnsData = new JObject();
var data = new JObject();
MainApnsData.Add("alert", Message.Text.Trim());
MainApnsData.Add("badge", 1);
MainApnsData.Add("Sound", "default");
data.Add("aps", MainApnsData);
ApnsData.Add("CalledFromNotify", txtboxID.Text.Trim());
data.Add("CustomNotify", ApnsData);
//read the .p12 certificate file
byte[] bdata = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/CertificatesPushNew.p12"));
//create push sharp APNS configuration
var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,bdata,"YourPassword");
//create a apnService broker
var apnsBroker = new ApnsServiceBroker(config);
// Wire up events
apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
aggregateEx.Handle(ex =>
// See what kind of exception it was to further diagnose
if (ex is ApnsNotificationException)
var notificationException = (ApnsNotificationException)ex;
// Deal with the failed notification
var apnsNotification = notificationException.Notification;
var statusCode = notificationException.ErrorStatusCode;
Console.WriteLine($"Apple Notification Failed: ID=apnsNotification.Identifier, Code=statusCode");
else
// Inner exception might hold more useful information like an ApnsConnectionException
Console.WriteLine($"Apple Notification Failed for some unknown reason : ex.InnerException");
// Mark it as handled
return true;
);
;
apnsBroker.OnNotificationSucceeded += (notification) =>
Console.WriteLine("Apple Notification Sent!");
;
var fbs = new FeedbackService(config);
fbs.FeedbackReceived += (string deviceToken1, DateTime timestamp) =>
//Remove the deviceToken from your database
// timestamp is the time the token was reported as expired
Console.WriteLine("Feedback received!");
;
fbs.Check();
// Start the broker
apnsBroker.Start();
var deviceToken = "Your device token";
// Queue a notification to send
apnsBroker.QueueNotification(new ApnsNotification
DeviceToken = deviceToken,
Payload= data
);
// Stop the broker, wait for it to finish
// This isn't done after every message, but after you're
// done with the broker
apnsBroker.Stop();
【讨论】:
以上是关于在 c# 中使用 Pushsharp 的 IOS 推送通知的主要内容,如果未能解决你的问题,请参考以下文章
IOS 的 C# PushSharp 通知在 IIS 7.5 中不起作用