在Asp.Net Web服务中推送Sharp

Luk*_*kie 4 asp.net pushsharp

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

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

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

  • Web Service公开了一种方法,该方法实例化PushSharp,注册各种回调事件并对Notification Messages进行排队.
  • 消费者调用Web服务
  • 一旦Web服务方法返回,该方法是否继续接收事件回调或是否已处理并且不会调用事件?

谢谢你的帮助.

小智 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
}
Run Code Online (Sandbox Code Playgroud)

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
                {
                }

            }
     }
}
Run Code Online (Sandbox Code Playgroud)