4、.net 項(xiàng)目中引用PushSharp.Apple.dll,PushSharp.Core.dll(這兩個(gè)文件在網(wǎng)上搜一下,有源碼的)
protected void startApp()
{
pusher.RegisterAppleService(new ApplePushChannelSettings(File.ReadAllBytes(CertificatePath), CertificatePassword));
pusher.OnDeviceSubscriptionChanged += pusher_OnDeviceSubscriptionChanged;
pusher.OnDeviceSubscriptionExpired += pusher_OnDeviceSubscriptionExpired;
pusher.OnNotificationSent += pusher_OnNotificationSent;
pusher.OnNotificationFailed += pusher_OnNotificationFailed;
}
static void pusher_OnNotificationFailed(object sender, INotification notification, Exception error)
{
var n = (AppleNotification)notification;
//error.Message ...獲取推送出錯(cuò)的信息
Log.Error("推送出錯(cuò)的信息", error);
}
static void pusher_OnNotificationSent(object sender, INotification notification)
{
//消息推送成功后
var n = (AppleNotification)notification;
//n.Payload.Alert.Body 獲取推送的消息內(nèi)容...
Log.Error("推送內(nèi)容"+n.Payload.Alert.Body);
}
static void pusher_OnDeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, INotification notification)
{
// 從數(shù)據(jù)庫刪除過期的expiredSubscriptionId
}
static void pusher_OnDeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, INotification notification)
{
// 把數(shù)據(jù)庫中的oldSubscriptionId更新為newSubscriptionId
}
pusher.QueueNotification(new AppleNotification().ForDeviceToken(TokenID) .WithAlert("推送的內(nèi)容").WithBadge(1).WithSound("default"));// 從數(shù)據(jù)庫或者其他等地方獲取設(shè)備的TokenID,每個(gè)iphone一個(gè)TokenID
9、如果想在Android設(shè)備上推送,項(xiàng)目要引進(jìn)PushSharp.Android.dll,代碼的話后期會(huì)為大家更新,敬請關(guān)注!