关于苹果PUSH(APNS)机制的那点事

Push的原理:

Push 的工作机制可以简单的概括为下图

图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider。 
APNS 是Apple Push Notification Service(Apple Push服务器)的缩写,是苹果的服务器。

上图可以分为三个阶段。

第一阶段:.net应用程序把要发送的消息、目的iPhone的标识打包,发给APNS。 
第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发到iPhone。 
第三阶段:iPhone把发来的消息传递给相应的应用程序, 并且按照设定弹出Push通知。

 

    从上图我们可以看到。

   1、首先是应用程序注册消息推送。

   2、 IOS跟APNS Server要deviceToken。应用程序接受deviceToken。

   3、应用程序将deviceToken发送给PUSH服务端程序。

   4、 服务端程序向APNS服务发送消息。

   5、APNS服务将消息发送给iPhone应用程序。

    无论是iPhone客户端跟APNS,还是Provider和APNS都需要通过证书进行连接的。下面我介绍一下几种用到的证书。

几种证书:

一、*.certSigningRequest文件

   1、生成Certificate Signing Request (CSR):

2、填写你的邮箱和Common Name,这里填写为PushChat。选择保存到硬盘。

这样就在本地生成了一个PushChat.certSigningRequest文件。

二、生成*.p12文件

1、导出密钥,并输入你的密码。

输入你的密码:

这样就生成了一个PushChatKey.p12文件。

三、新建一个App ID 和SSL certificate文件

1、用你的付过费的apple帐号登录到iOS Provisioning Portal。新建一个App ID。

     Description:中输入PushChat

     Bundle Seed ID:默认选择Generate New

     Bundle Identifier:输入com.mysoft.PushChat

    点击提交

这样就会生成下面这条记录:

点击配置:

出现下面界面,点击继续:

这里我们选择前面生成好的PushChat.certSigningRequest文件,点击生成。

正在生成

生成完毕,我们把它下载下来。命名为aps_developer_identity.cer

点击完成,你会发现状态变成Enabled。

到现在为止,我们已经生成了3个文件。

1、PushChat.certSigningRequest

2、PushChatKey.p12

3、aps_developer_identity.cer

现在我们创建一个简单的iPhone应用程序。

1、打开Xcode,选择创建一个View-based Application。命名如下图:

2、在PushChatAppDelegate中的didFinishLaunchingWithOptions方法中加入下面代码:

  
  
- (BOOL)application:(UIApplication * )application didFinishLaunchingWithOptions:(NSDictionary * )launchOptions { self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; // Let the device know we want to receive push notifications [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; return YES; }

通过registerForRemoteNotificationTypes方法,告诉应用程序,能接受push来的通知。

3、在xcode中运行,会弹出下面的提示框:

选择OK。表示此应用程序开启消息通知服务。

在 PushChatAppDelegate.m代码中添加下面方法获取deviceToken :

  
  
- ( void )application:(UIApplication * )application didRegisterForRemoteNotificationsWithDeviceToken:(NSData * )deviceToken { NSLog( @" My token is: %@ " , deviceToken); } - ( void )application:(UIApplication * )application didFailToRegisterForRemoteNotificationsWithError:(NSError * )error { NSLog( @" Failed to get token, error: %@ " , error); }
获取到的deviceToken,我们可以通过webservice服务提交给.net应用程序,这里我简单处理,直接打印出来,拷贝到.net应用环境中使用。

发送通知的.net应用程序出来需要知道deviceToken之外,还需要一个与APNS连接的证书。

这个证书可以通过我们前面生成的两个文件中得到。

使用OpenSSL生成.net和APNS通信的证书文件。

1、将aps_developer_identity.cer转换成 aps_developer_identity.pem格式。

openssl x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem -outform PEM

2、将p12格式的私钥转换成pem,需要设置4次密码,这里密码都设置为:abc123。

openssl pkcs12 -nocerts -out PushChat_Noenc.pem -in PushChat.p12

3、用certificate和the key 创建PKCS#12格式的文件。

openssl pkcs12 -export -in aps_developer_identity.pem -inkey PushChat_Noenc.pem -certfile PushChat.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12

这样我们就得到了在.net应用程序中使用的证书文件:aps_developer_identity.p12

在.net应用程序中发送通知。

有个开源的类库:apns-sharp。

地址是:http://code.google.com/p/apns-sharp/

我们下载源代码,对里面的JdSoft.Apple.Apns.Notifications做相应的调整就能用了。

我们根据DeviceToken和p12File对JdSoft.Apple.Apns.Notifications.Test代码做相应的调整,如下图。

这样就OK了。

效果:

通知的代码:

  
  
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); } }

用手机拍的ipad上面的显示:

总结:这篇文章主要是详细的讲述了ios消息推送机制的实现,如何通过.net应用程序发送消息给ios应用程序。


from:http://www.cnblogs.com/aspnethot/articles/2248903.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
根据提供的引用[1],TNetHttpClient是Delphi中的一个HTTP客户端组件,而APNS是Apple Push Notification Service的缩写,是苹果公司提供的推送服务。因此,TNetHttpClient APNS是使用Delphi中的TNetHttpClient组件来与APNS进行通信。 使用TNetHttpClient APNS的步骤如下: 1.创建TNetHttpClient对象,并设置请求的URL和请求方法为POST。 2.设置请求头部信息,包括Content-Type、Authorization和apns-topic等信息。 3.设置请求体信息,包括设备的token、推送的payload等信息。 4.发送请求并获取响应结果。 以下是一个使用TNetHttpClient APNS发送推送通知的示例代码: ```delphi uses System.Net.HttpClient, System.Net.HttpClientComponent; procedure SendAPNSNotification; var HttpClient: TNetHttpClient; Request: TNetHTTPRequest; Response: IHTTPResponse; APNSUrl: string; APNSPayload: string; APNSToken: string; APNSAuth: string; APNSTopic: string; begin HttpClient := TNetHttpClient.Create(nil); Request := TNetHTTPRequest.Create(nil); try APNSUrl := 'https://api.push.apple.com/3/device/'; APNSPayload := '{"aps":{"alert":"Hello World!"}}'; APNSToken := 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; APNSAuth := 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; APNSTopic := 'com.example.app'; Request.MethodString := 'POST'; Request.URL := APNSUrl + APNSToken; Request.ContentType := 'application/json'; Request.CustomHeaders['Authorization'] := APNSAuth; Request.CustomHeaders['apns-topic'] := APNSTopic; Request.Source := TStringStream.Create(APNSPayload, TEncoding.UTF8); Response := HttpClient.Execute(Request); if Response.StatusCode = 200 then ShowMessage('Push notification sent successfully.') else ShowMessage('Failed to send push notification.'); finally Request.Free; HttpClient.Free; end; end; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值