私人资料库
本博客大部分技术文章,均从网络搜索得来,旨在收集整理技术资料,文章版权归属原作者,由此引起的任何版权问题,与本人无关。

转自:http://blog.csdn.net/yick8799/article/details/6447365

 

iOS的通知(notifications)有两种形式:

  • push notifications,从iOS3开始就有了,由远程服务器发起通知
  • local notifications,从iOS4开始支持,由本地应用发起的通知

两种通知都是为了提醒用户后台执行的应用有了变化。从用户角度来看,效果是一样的,都是通知。只是实现的方式不一样,对于技术实现来说。

本文主要说明push notification的device token的步骤。

可以通过《偷窥iPhone Push Notification的幕后》《iPhone的Push(推送通知)功能原理浅析》对push notification有个原理上的了解。

 

设备的准备

首先要知道,push notification只能在真机上运行的,无法在模拟器上使用,如果在模拟器上运行,在注册设备的时候会有类似如下报错:

Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator" UserInfo=0x5d249d0 {NSLocalizedDescription=remote notifications are not supported in the simulator}

真机也要注意,如果没有越狱,没有问题。越狱的话,比如通过blacksnOw,因为没有经过iTunes,无法生成有效的设备证书(device certificate),因此注册的时候不会成功。

检查越狱版本是否可用,可以ssh到设备上,执行命令:

ls /var/mobile/Library/Preferences/com.apple.apsd.plist  -l

-rw——- 1 mobile mobile 119 Aug 24 19:21 /var/mobile/Library/Preferences/com.apple.apsd.plist

返回的文件大小是119,就没有问题。

获取device token的原理

在说操作步骤之前,先说一下获取device token的一些原理方面的事情。

device token,即设备令牌,不是系统唯一标识(见获取iOS设备的基本信息),需要在应用启动时发起到apple服务器请求,注册自己的设备和应用,并获得这个device token。

device token有什么用?如果应用需要push notification给手机,那么它要有个服务器端(provider),但是它发出的信息不是直接给手机的,而是必须统一交给apple的服务器,这个服务器就是apple push notification server(apns)。apple服务器通过这个token,知道应用要发的消息是给哪个手机设备的,并转发该消息给手机,手机再通知应用程序。

获取device token的操作步骤

这里主要参照了这篇文章:Programming Apple Push Notification Services

该文档很详细,照做就应该没有问题。

需要注意的是identifier一定要和provision portal profile中的app id一致,即:

image

要和:

image

一致。

另外,要确保设备绑定的是唯一的profile:

image

编写代码,是在AppDelegate中增加两个方法:

  • didRegisterForRemoteNotificationsWithDeviceToken:当应用第一次运行的时候,ios获取到device token后调用,用于注册设备到apns上之后的操作(比如将device token通知应用的服务器端provider)
  • didFailToRegisterForRemoteNotificationsWithError:如果注册的时候失败,ios会调用这个方法,可以打印一些报错日志或者提醒用户通知不可用

另外,有一个方法需要增加内容,主要是打印日志,说明是否已经注册:

 1 #import "ApplePushNotificationAppDelegate.h"
 2 #import "ApplePushNotificationViewController.h"
 3  
 4 @implementation ApplePushNotificationAppDelegate
 5  
 6 @synthesize window;
 7 @synthesize viewController;
 8  
 9 - (void)applicationDidFinishLaunching:(UIApplication *)application {    
10     [window addSubview:viewController.view];
11     [window makeKeyAndVisible];
12  
13     NSLog(@"Registering for push notifications...");    
14     [[UIApplication sharedApplication] 
15         registerForRemoteNotificationTypes:
16         (UIRemoteNotificationTypeAlert | 
17          UIRemoteNotificationTypeBadge | 
18          UIRemoteNotificationTypeSound)];
19  
20 }
21  
22 - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
23  
24     NSString *str = [NSString 
25         stringWithFormat:@"Device Token=%@",deviceToken];
26     NSLog(str);
27  
28 }
29  
30 - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { 
31  
32     NSString *str = [NSString stringWithFormat: @"Error: %@", err];
33     NSLog(str);    
34  
35 }
36  
37 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
38  
39     for (id key in userInfo) {
40         NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
41     }    
42  
43 }
44  
45 - (void)dealloc {
46     [viewController release];
47     [window release];
48     [super dealloc];
49 }
50  
51 @end

第一次运行带注册方法的应用,会看到类似这样的提示窗口:

J%ZIE~[N)`A%S_MRQRS6I92

然后,在日志中看到类似下面的日志,主要是看到打印出device token的64位字符串,就说明成功了。

 

posted on 2012-07-07 15:44  该显示名称已被其他用户使用  阅读(474)  评论(0编辑  收藏  举报