using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PushSharp;
using PushSharp.Apple;
using CMJ.Framework.Data;
using CMJ.Framework.Logging;
using CMJ.Framework.Windows.Forms;
namespace PushNotificationServer
{
public partial class frmMain : PersonalizeForm
{
private string _appID = @"com.cmjstudio.pushnotification";
private SqlHelper _helper = new SqlHelper();
public frmMain()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnSend_Click(object sender, EventArgs e)
{
List<string> deviceTokens = GetDeviceToken();
SendMessage(deviceTokens, tbMessage.Text);
}
#region 发送消息
/// <summary>
/// 取得所有设备令牌
/// </summary>
/// <returns>设备令牌</returns>
private List<string> GetDeviceToken()
{
List<string> deviceTokens = new List<string>();
string sql = string.Format("SELECT DeviceToken FROM dbo.Device WHERE AppID='{0}'",_appID);
DataTable dt = _helper.GetDataTable(sql);
if(dt.Rows.Count>0)
{
foreach(DataRow dr in dt.Rows)
{
deviceTokens.Add((dr["DeviceToken"]+"").TrimStart('<').TrimEnd('>').Replace(" ",""));
}
}
return deviceTokens;
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="deviceToken">设备令牌</param>
/// <param name="message">消息内容</param>
private void SendMessage(List<string> deviceToken, string message)
{
//创建推送对象
var pusher = new PushBroker();
pusher.OnNotificationSent += pusher_OnNotificationSent;//发送成功事件
pusher.OnNotificationFailed += pusher_OnNotificationFailed;//发送失败事件
pusher.OnChannelCreated += pusher_OnChannelCreated;
pusher.OnChannelDestroyed += pusher_OnChannelDestroyed;
pusher.OnChannelException += pusher_OnChannelException;
pusher.OnDeviceSubscriptionChanged += pusher_OnDeviceSubscriptionChanged;
pusher.OnDeviceSubscriptionExpired += pusher_OnDeviceSubscriptionExpired;
pusher.OnNotificationRequeue += pusher_OnNotificationRequeue;
pusher.OnServiceException += pusher_OnServiceException;
//注册推送服务
byte[] certificateData = File.ReadAllBytes(@"E:KenshinCui_Push.p12");
pusher.RegisterAppleService(new ApplePushChannelSettings(certificateData, "123"));
foreach (string token in deviceToken)
{
//给指定设备发送消息
pusher.QueueNotification(new AppleNotification()
.ForDeviceToken(token)
.WithAlert(message)
.WithBadge(1)
.WithSound("default"));
}
}
void pusher_OnServiceException(object sender, Exception error)
{
Console.WriteLine("消息发送失败,错误详情:" + error.ToString());
PersonalizeMessageBox.Show(this, "消息发送失败,错误详情:" + error.ToString(), "系统提示");
}
void pusher_OnNotificationRequeue(object sender, PushSharp.Core.NotificationRequeueEventArgs e)
{
Console.WriteLine("pusher_OnNotificationRequeue");
}
void pusher_OnDeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, PushSharp.Core.INotification notification)
{
Console.WriteLine("pusher_OnDeviceSubscriptionChanged");
}
void pusher_OnDeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, PushSharp.Core.INotification notification)
{
Console.WriteLine("pusher_OnDeviceSubscriptionChanged");
}
void pusher_OnChannelException(object sender, PushSharp.Core.IPushChannel pushChannel, Exception error)
{
Console.WriteLine("消息发送失败,错误详情:" + error.ToString());
PersonalizeMessageBox.Show(this, "消息发送失败,错误详情:" + error.ToString(), "系统提示");
}
void pusher_OnChannelDestroyed(object sender)
{
Console.WriteLine("pusher_OnChannelDestroyed");
}
void pusher_OnChannelCreated(object sender, PushSharp.Core.IPushChannel pushChannel)
{
Console.WriteLine("pusher_OnChannelCreated");
}
void pusher_OnNotificationFailed(object sender, PushSharp.Core.INotification notification, Exception error)
{
Console.WriteLine("消息发送失败,错误详情:" + error.ToString());
PersonalizeMessageBox.Show(this, "消息发送失败,错误详情:"+error.ToString(), "系统提示");
}
void pusher_OnNotificationSent(object sender, PushSharp.Core.INotification notification)
{
Console.WriteLine("消息发送成功!");
PersonalizeMessageBox.Show(this, "消息发送成功!", "系统提示");
}
#endregion
}
}