在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进行排队. 谢谢 解决方法
由于应用程序池干扰了进程,因此在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 { } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-4 – ASP.Net MVC 4如何在没有密码的情况下创建
- asp.net-mvc – 在ASP.NET MVC中未调用WebClient异步回调
- asp.net-mvc – 填写下拉列表中选择另一个下拉列表
- asp.net mvc客户端验证
- asp.net-mvc – ASP.NET MVC自定义授权
- asp.net-mvc-4 – 禁止从地址栏调用操作方法
- asp.net-mvc – 我应该选择云吗?
- 在ASP.NET MVC中获取服务器机器名称?
- 从ASP.NET应用程序使用Active Directory时,DirectoryServic
- asp.net – 从中??等信任环境的Web.config读取system.net/m
推荐文章
站长推荐
热点阅读