【发布时间】: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;
}
【问题讨论】:
-
您收到什么错误?您可以在推送代理的不同事件中跟踪该过程。检查是否有异常
-
@patel 很好地呼吁生产环境,但目前 OP 正在发送到沙箱。可以在
new ApplePushChannelSettings(false, ....)的第一个参数中看到,而且,这会引发异常。 -
您的代码正在向苹果的沙箱服务器发送消息。确保您的 ios 应用程序也配置为在沙盒服务器上注册(即使用开发配置文件构建!),就像@patel 所说:为所有推送代理的事件添加处理程序,并查看是否引发任何错误事件。
标签: c# push-notification apple-push-notifications pushsharp