4

This is more of a general Asp.Net / .Net lifecycle question.

I'm looking at using PushSharp within a Asp.Net Web Service to send notifications using APNS.

Given the nature of PushSharp using a queue to async send messages and then event callbacks to notify of 'OnNotificationSent' / 'OnServiceException' etc.. how would this work within Asp.net?

  • The Web Service exposes a method that instantiates PushSharp, registers for the various callback events and queues Notification Messages.
  • The consumer calls the web service
  • Once The Web service method returns, does that method continue to receive the event callbacks or is it disposed and the events will not be called?

Thanks for your help.

Lukie
  • 905
  • 1
  • 11
  • 21

2 Answers2

5

Not highly recommended in Asp.net, due to application pool interfering in the process (PushSharp author says notifications in the queue but not get sent). I have implemented this though in an Asp.net website and it works.

I have moved this to a Windows service since.

Global.asax.cs file:

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 file (example 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
                {
                }

            }
     }
}
Pratik Bhoir
  • 2,074
  • 7
  • 19
  • 36
joelrb
  • 216
  • 1
  • 3
0

Check out EasyServices it allows you to easily push notifications to various push servers using PushSharp without having to take care of un-received notifications even when using 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