架构原理
- 应用启用推送通知功能,需要用户确认;
- 应用收到设备识别ID(device token),相当于接收推送通知的地址;
- 应用将设备识别ID发送到你开发的服务器;
- 当有推送通知的需要时,你就可以通过你开发的服务组件发送信息到苹果的服务器上;
- 苹果推送通知服务将信息推送到用户的设备上。
苹果推送通知服务要注意的
- 一台iPhone或iPad,苹果推送通知服务不能在模拟器上工作,你必须在真机上测试
- 有付费开通的iOS 开发者会员资格
- 服务器端要安装苹果发放的证书
- 推送不保证成功,也无法知道送达消息的状态
客户端教程
Apple Push Notification Services in iOS 6 Tutorial
.Net下证书的转换
到这一步时我们已经有的证书文件
* .cerSigningRequest
证书请求文件
* .p12
私钥文件
* .cer
SSL证书
APNS所要使用的连接证书
- 将apsdeveloperidentity.cer转换成apsdeveloperidentity.pem格式
openssl x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem -outform PEM
- 将p12格式的私钥转换成pem
openssl pkcs12 -nocerts -out Push_Noenc.pem -in Push.p12
- 创建p12文件
openssl pkcs12 -export -in aps_developer_identity.pem -inkey Push_Noenc.pem -certfile Push.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12
.Net的下的解决方案
第三方开源类库APNS-Sharp
//如果你使用沙盒方式请赋值true,否则请赋值false
bool sandbox = true;
//输入你的设备令牌在此
string testDeviceToken = "ccc4c058e6a0452162a2abd45024b21b7afc2cb2eaf7d09889a6ebb24a72648e";
//输入你在苹果授权网站上获得的p12证书文件
string p12File = "aps_developer_identity.p12";
//p12证书密码
string p12FilePassword = "Passw0rd";
//Number of notifications to send
int count = 3;
//Number of milliseconds to wait in between sending notifications in the loop
// This is just to demonstrate that the APNS connection stays alive between messages
int sleepBetweenNotifications = 15000;
//Actual Code starts below:
//--------------------------------
string p12Filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, p12File);
NotificationService service = new NotificationService(sandbox, p12Filename, p12FilePassword, 1);
service.SendRetries = 5; //5 retries before generating notificationfailed event
service.ReconnectDelay = 5000; //5 seconds
service.Error += new NotificationService.OnError(service_Error);
service.NotificationTooLong += new NotificationService.OnNotificationTooLong(service_NotificationTooLong);
service.BadDeviceToken += new NotificationService.OnBadDeviceToken(service_BadDeviceToken);
service.NotificationFailed += new NotificationService.OnNotificationFailed(service_NotificationFailed);
service.NotificationSuccess += new NotificationService.OnNotificationSuccess(service_NotificationSuccess);
service.Connecting += new NotificationService.OnConnecting(service_Connecting);
service.Connected += new NotificationService.OnConnected(service_Connected);
service.Disconnected += new NotificationService.OnDisconnected(service_Disconnected);
//The notifications will be sent like this:
// Testing: 1...
// Testing: 2...
// Testing: 3...
// etc...
for (int i = 1; i <= count; i++)
{
//Create a new notification to send
Notification alertNotification = new Notification(testDeviceToken);
alertNotification.Payload.Alert.Body = string.Format("Testing {0}...", i);
alertNotification.Payload.Sound = "default";
alertNotification.Payload.Badge = i;
//Queue the notification to be sent
if (service.QueueNotification(alertNotification))
Console.WriteLine("Notification Queued!");
else
Console.WriteLine("Notification Failed to be Queued!");
//Sleep in between each message
if (i < count)
{
Console.WriteLine("Sleeping " + sleepBetweenNotifications + " milliseconds before next Notification...");
System.Threading.Thread.Sleep(sleepBetweenNotifications);
}
}
Console.WriteLine("Cleaning Up...");
//First, close the service.
//This ensures any queued notifications get sent befor the connections are closed
service.Close();
//Clean up
service.Dispose();
Console.WriteLine("Done!");
Console.WriteLine("Press enter to exit...");
Console.ReadLine();