加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

在Asp.Net Web服务中推送Sharp

发布时间:2020-12-16 06:28:00 所属栏目:asp.Net 来源:网络整理
导读:这更像是一般的Asp.Net / .Net生命周期问题. 我正在寻找在Asp.Net Web服务中使用PushSharp来使用APNS发送通知. 鉴于PushSharp的性质使用队列异步发送消息然后事件回调通知’OnNotificationSent’/’OnServiceException’等.这将如何在Asp.net中工作? Web Se
这更像是一般的Asp.Net / .Net生命周期问题.

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

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

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

谢谢
?您的帮助.

解决方法

由于应用程序池干扰了进程,因此在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
                {
                }

            }
     }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读