在Asp.Net Web服务中推送Sharp

时间:2014-06-13 00:46:18

标签: asp.net pushsharp

这更像是一般的Asp.Net / .Net生命周期问题。

我正在寻找在Asp.Net Web服务中使用PushSharp来使用APNS发送通知。

鉴于PushSharp的性质,使用队列异步发送消息,然后事件回调通知'OnNotificationSent'/'OnServiceException'等。这将如何在Asp.net中工作?

  • Web服务公开了一种方法,该方法实例化PushSharp,注册各种回调事件并对通知消息进行排队。
  • 消费者呼叫网络服务
  • 一旦Web服务方法返回,该方法是否继续接收事件回调,或者它是否已被处理并且不会调用事件?

由于  为了你的帮助。

2 个答案:

答案 0 :(得分:5)

Asp.net中不强烈推荐,因为应用程序池干扰了进程(PushSharp作者说队列中的通知但未被发送)。我已经在Asp.net网站上实现了这个功能。

我将此移至Windows服务。

Global.asax.cs 档案:

using PushSharp;

using PushSharp.Core;

public class Global : System.Web.HttpApplication
{

    private static PushBroker myPushBroker;

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            myPushBroker = new PushBroker();

            myPushBroker.OnNotificationSent += NotificationSent;
            myPushBroker.OnChannelException += ChannelException;
            myPushBroker.OnServiceException += ServiceException;
            myPushBroker.OnNotificationFailed += NotificationFailed;
            myPushBroker.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
            myPushBroker.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
            myPushBroker.OnChannelCreated += ChannelCreated;
            myPushBroker.OnChannelDestroyed += ChannelDestroyed;

            HttpContext.Current.Application["MyPushBroker"] = myPushBroker;

         }

         //IMPLEMENT PUSHBROKER DELEGATES HERE
}

aspx.cs 文件(示例Notifications.aspx.cs):

using PushSharp;

using PushSharp.Apple;

using PushSharp.Core;

public partial class Notifications : System.Web.UI.Page {

     private PushBroker myPushBroker = HttpContext.Current.Application["MyPushBroker"] as PushBroker;

        //SO I CAN SWITCH FROM DEVELOPMENT TO PRODUCTION EASILY I SET THIS IN THE DATABASE
        private string pushCertificate = "";
        private string certPass = "";
        private bool isProduction = false;

     protected void btnSendNotification_Click(object sender, EventArgs e)
     {
            bool hasError = false;
            lblError.Text = "";

            if (!string.IsNullOrEmpty(txtMessage.Text))
            {
                try
                {
                   GetCertificate(); 

                    //GET DEVICE TOKENS TO SEND MESSAGES TO
                    //NOT THE BEST WAY TO SEND MESSAGES IF YOU HAVE HUNDREDS IF NOT THOUSANDS OF TOKENS. THAT'S WHY A WINDOWS SERVICE IS RECOMMENDED.

                    string storedProcUser = "sp_Token_GetAll";
                    string userTableName = "User_Table";

                    DataSet dsUser = new DataSet();

                    UserID = new Guid(ID.Text);
                    dsUser = srvData.GetDeviceToken(UserID, storedProcUser, userTableName, dataConn);

                    DataTable userTable = new DataTable();
                    userTable = dsUser.Tables[0];

                    if (userTable.Rows.Count != 0)
                    {
                        string p12FileName = Server.MapPath(pushCertificate); //SET IN THE GET CERTIFICATE
                        var appleCert = File.ReadAllBytes(p12FileName);
                        string p12Password = certPass;

                        //REGISTER SERVICE
                        myPushBroker.RegisterAppleService(new ApplePushChannelSettings(isProduction, appleCert, p12Password));

                        DataRow[] drDataRow;
                        drDataRow = userTable.Select();
                        string savedDeviceToken = "";

                        for (int i = 0; i < userTable.Rows.Count; i++)
                        {
                            if (drDataRow[i]["DeviceToken"] is DBNull == false)
                            {
                                savedDeviceToken = drDataRow[i]["DeviceToken"].ToString();

                                myPushBroker.QueueNotification(new AppleNotification()
                                           .ForDeviceToken(savedDeviceToken)
                                           .WithAlert(txtMessage.Text)
                                           .WithBadge(1)
                                           .WithSound("sound.caf"));

                                //NOTHING TO DO ANYMORE. CAPTURE IN THE PUSH NOTIFICATION DELEGATE OF GLOBAL ASCX FILE WHAT HAPPENED TO THE SENT MESSAGE.
                            }
                        }
                    }

                }
                catch(Exception ex)
                {
                }
                 finally
                {
                }

            }
     }
}

答案 1 :(得分:0)

查看EasyServices,它允许您使用PushSharp轻松将通知推送到各种推送服务器,即使在使用ASP.NET时也无需处理未收到的通知

var _pushNotificationService = EngineContext.Current.Resolve<IPushNotificationService>();
_pushNotificationService.InsertNotification(NotificationType type, string title, string message, int subscriberId, PushPriority Priority = PushPriority.Normal);

https://easyservices.codeplex.com