IOS设备推送通知服务编程


-译自 weimenglee 的Programming ApplePush Notification Services

iPhone 对于应用程序在后台运行有诸多限制(除非你越狱)。因此,当用户切换到其他程序后,原先的程序无法保持运行状态。对于那些需要保持持续连接状态的应用程序(比如社区网络应用),将不能收到实时的信息。

为解决这一限制,苹果推出了APNs(苹果推送通知服务)。APNs 允许设备与苹果的推送通知服务器保持常连接状态。当你想发送一个推送通知给某个用户的iPhone上的应用程序时,你可以使用 APNs 发送一个推送消息给目标设备上已安装的某个应用程序。

本文中,你将学到创建使用 APNs 的iOS 应用的详细步骤。


创建证书请求

使用APNs 的第一步是生成一个证书请求,使用该证书请求来申请一个用于开发的 SSL 证书。

1. 打开“钥匙串访问”应用程序。

2. 选择“KeychainAccess -> Certificate Assistant -> Request a Certificate From CertificateAuthority”:

3. 输入所需的信息,勾选“Saved to disk”选项,点击 Continue:

4. 使用默认文件名把证书请求进行保存:在弹出窗口中,点击Done。

 

创建 App ID

每个使用 APNs 的 iOS 应用必须有一个唯一的 App ID。在本步骤中,你将学到如何创建推送通知中要用到的App ID。

1. 登录iPhoneDeveloper Program:http://developer.apple.com/iphone/。点击页面右边的“ iPhone Developer Program Portal ”:

2. 首先看到的是欢迎页面:

3. 点击左边的“App ID”,然后点击右边的“New App ID”按钮:

4. 在 Description 栏输入“PushAppID”,在“Bundle Seed ID”栏中选择“Generate New”。在“Bundle Identifier”栏,输入“net.learn2develop.MyPushApp”,然后点击“Submit”:

5. 现在你应该能看到所创建的 App ID 了:


配置 App

一旦创建了 App ID,你还要为推送通知对 App ID 进行一些配置。

1. 点击App ID 右边的 Configure 链接,会看到如下选项:

勾选“Enable for Apple Push Notificationservice”,点击“Development Push SSL Certificate”右边的“Configure”按钮。

2. 接下来你会看到“Apple Push Notification service SSL Certificate Assistant”页面。点击Continue:

3. 点击Choose File 按钮,选择前面保存的证书请求文件存放地址。点击 Generate: 

4. 你的SSL 证书会被生成。点击 Continue:

5. 点击Download Now 按钮,下载 SSL 证书。点击 Done:

6. 下载的 SSL 证书文件名为 aps.developer.identity.cer。双击,将证书安装到钥匙串中。这个证书会在你的程序中用到,它允许程序接收 APNs 发送来的推送通知。

 

创建 Provisioning Profile

接下来,需要创建 provisioning profile 以便允许应用程序安装到真实设备上。

1. 回到iPhone Development Program Portal,点击 Provisioning 栏,点击 New Profile 按钮:

2. Profile Name 栏输入 MyDevicesProfile,在 App ID 栏选择 PushAppID。在Devices 栏,勾选所有你想激活的设备(在 iPhone Developer Program Portal 的 Devices 页中注册的所有设备)。点击 Submit。

3. provisioning  profile 会等待审核。几秒钟后,它会显示在页面上。点击Download 按钮下载该 provisioning profile: 

4. 下载下来的provisioning profile 名为 MydevicesProfile.mobileprovision。


激活设备

创建 provision profile 后,你可以将它安装到真实设备中。

1. 将iPhone 或 iPod 连接到 Mac。

2. 把下载下来的 MyDevicesProfile.mobileprovision 文件拖到Dock 栏的 Xcode 图标上。

3. Xcode 的 Organizer 程序将启动,选择当前连机的设备。可以看到MyDevicesProfile 已自动安装到设备上了。

 

创建 iPhone 应用程序

1. 打开Xcode,创建 View-Based Application 项目,命名为 ApplePushNotification。

2. 把一个 WAV 文件(本例是 beep.wav)拖到Xcode 的 Resouces 文件夹。

3. 展开Xcode 中的 Targets 项目,选择ApplePushNotification,按下 ⌘+i,在 info 出口,点击Properties 标签栏: 

在 Identifier 文本框,输入net.learn2develop.MyPushApp.

4. 点击 Build 标签栏,在 search 输入框中键入Code Signing。在 Any iPhone OS Device 选项,选择正确的 profile:

5. 在 ApplePushNotificationAppDelegate.m 文件中,输入以下代码(加粗部分):

 1 #import "ApplePushNotificationAppDelegate.h"
2
3 #import "ApplePushNotificationViewController.h"
4
5
6
7 @implementation ApplePushNotificationAppDelegate
8
9
10
11 @synthesize window;
12
13 @synthesize viewController;
14
15
16
17 - (void)applicationDidFinishLaunching:(UIApplication*)application {
18
19 [window addSubview:viewController.view];
20
21 [window makeKeyAndVisible];
22
23
24
25 NSLog(@"Registeringfor push notifications...");
26
27 [[UIApplication sharedApplication]
28
29 registerForRemoteNotificationTypes:
30
31 (UIRemoteNotificationTypeAlert |
32
33 UIRemoteNotificationTypeBadge |
34
35 UIRemoteNotificationTypeSound)];
36
37 }
38
39
40
41 - (void)application:(UIApplication*)appdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
42
43 NSString *str = [NSString
44
45 stringWithFormat:@"Device ];
46
47 NSLog(str);
48
49 }
50
51
52
53 - (void)application:(UIApplication*)appdidFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
54
55 NSString *str = [NSStringstringWithFormat: @"Error: %@", err];
56
57 NSLog(str);
58
59
60
61 }
62
63
64
65 - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
66
67 for (id key in userInfo) {
68
69 NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
70
71 }
72
73 }
74
75 - (void)dealloc {
76
77 [viewController release];
78
79 [window release];
80
81 [super dealloc];
82
83 }
84
85 @end

 

6. 按下 ⌘+R 运行程序(在真实设备)。按下 shift+⌘+R 显示Debugger Console 窗口。查看设备输出到控制台的 device token。,device token 是 38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7c e90d56e9fe145bcc 6c2c594b。记下device token(复制、粘贴到一个文本文件里)

7. 如果查看 iPhone/iPod 上的“Settings”程序,你会发现一个 Notifications 的项:

 

创建 Push Notification Provider

Push Notification Provider 是一个应用程序,用于通过 APNs 发送推送通知给 iPhone 应用。

通过 APNs 发送推送通知有几个步骤:
1. 使用前面创建的 SSL 证书与 APNs 通讯;

2. 构造所要发送的消息载体;

3. 发送载体到APNs;

APNs 是一个基于流的 TCP socket,你的 provider 以 SSL 协议与其通讯。推送通知(包括载体)是以二进制流的方式发送的。和APNs 建立连接后,你可以维持该连接并在连接中断之前发送多个通知。

技巧: 应避免每发送一次推送通知就建立、关闭一次连接。频繁的建立、关闭连接可能会被 APNs 认为是 DOS 攻击,从而拒绝发送 provider 的推送通知发送请求。

一个推送通知消息的格式如图24 所示:

 

更多细节,请参考 Apple Push Notification Service Programming Guide。

载体(payload)是 JSON 字符串(最长 256 字节),封装了你发送给 iOS 应用的信息。这是一个 payload 的例子:

 1 {
2
3 "aps": {
4
5 "alert" : "Yougot a new message!" ,
6
7 "badge" : 5,
8
9 "sound" : "beep.wav"},
10
11 "acme1" : "bar",
12
13 "acme2" : 42
14
15 }



为了省去自己编写 push notification provider 的麻烦,你可以使用 Stefan Hafeneger 写的一个 Mac OS X 应用程序:PushMeBaby,下载地址

1. 在Xcode 中打开 PushMeBaby。

2. 右击 Resouces 文件夹,选择 Add Existing Files…,选择前面所下载到的aps.developer.identity.cer 文件。

3. 在 ApplicationDelegate.m 文件中,修改如下代码(加粗部分):

 1 - (id)init {
2
3 self = [super init];
4
5 if(self != nil) {
6
7 self.deviceToken = @"38c866dd bb323b39 ffa73487 5e157ee5 a85e0b7ce90d56e9 fe145bcc 6c2c594b";
8
9 self.payload = @"{\"aps\":{\"alert\":\"Yougot a new message!\",\"badge\":5,\"sound\":\"beep.wav\"},\"acme1\":\"bar\",\"acme2\":42}";
10
11 self.certificate = [[NSBundle mainBundle]
12
13 pathForResource:@"aps_developer_identity" ofType:@"cer"];
14
15 }
16
17 return self;
18
19 }

 

4. 按下 ⌘+R,运行程序。将会问你是否允许使用证书,点击Always Allow(总是允许):

 

在 iPhone/iPod,确认 ApplePushNotification 程序未运行。点击 Push 按钮,会向设备发送一条推送通知。服务器实际上发送了下列消息给APN 服务器:

 1 {
2
3 "aps": {
4
5 "alert" : "Yougot a new message!" ,
6
7 "badge" : 5,
8
9 "sound" : "beep.wav"},
10
11 "acme1" : "bar",
12
13 "acme2" : 42
14
15 }

 

5. 如果消息推送成功,将会在 iPhone/iPod 上出现下图:

 

6. 如果现在按下 ⌘+R 调试 ApplePushNotification 程序,然后从 PushMeBaby 中发送一条消息,控制台会显示如下输出:

2009-11-24 21:11:49.182 ApplePushNotification[1461:207]key: acme1, value: bar

2009-11-24 21:11:49.187 ApplePushNotification[1461:207]key: aps, value: {

   alert = "You got a new message!";

   badge = 5;

   sound = "beep.wav";

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM