【问题标题】:Push Sharp Within Asp.Net Web Service在 Asp.Net Web 服务中推送 Sharp
【发布时间】:2014-08-03 11:17:20
【问题描述】:

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

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

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

  • Web 服务公开了一个实例化 PushSharp 的方法,注册各种回调事件并将通知消息排队。
  • 消费者调用网络服务
  • 一旦 Web 服务方法返回,该方法是继续接收事件回调,还是将其处理掉并且不会调用事件?

谢谢 为您提供帮助。

【问题讨论】:

    标签: asp.net pushsharp


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      查看 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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-27
        • 1970-01-01
        • 2014-12-31
        • 2013-06-26
        • 2016-09-14
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        相关资源
        最近更新 更多