4

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

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

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

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

谢谢你的帮助。

4

2 回答 2

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

            }
     }
}
于 2014-06-24T10:08:29.233 回答
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

于 2014-11-22T14:09:27.127 回答