登录developer.apple.com,创建新的App ID,要求此ID的Bundle Identifier不包含通配符,否则不能启用Push以及IAP功能。例如 com.soso.sosoimage。
在App IDs列表页面,点击刚创建的app id右面的Configure链接,进入Configure App ID界面,选中"Enable for App Push Notification service"。点击Development Push SSL Certificate一行的Configure按钮,弹出"Apple Push Notification service SSL Certificate Assistant"对话框,依对话框操作,类似于创建开发或发布用的Certificate。
最终将Development Push SSL Certificate下载并安装到本地Keychain Access。导出成p12文件,备用。导出时需要设置密码,不得为空。
在developer.apple.com,创建一个新的Provisioning Profile,使用我们刚刚创建的支持Push功能的App ID。下载并安装到本地。
程序第一次执行的时候,调用如下代码.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
三个参数分别代表消息(横幅或提醒,由用户Setting决定,程序不可更改)、数字标记、声音。
在AppDelegate.m中添加两个方法.//iPhone 从APNs服务器获取deviceToken后回调此方法 - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString* dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; NSLog(@"deviceToken:%@", dt); } //注册push功能失败 后 返回错误信息,执行相应的处理 - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { NSLog(@"Push Register Error:%@", err.description); }获取DeviceToken后,将其传给Provider。
Push.badge(2, keystore, password, false, "7bb8d508e32df651c6c239439737dbd40a88d2461ad2ac1e5dbe49ecea5ccc67");其中,2为要显示的数字;
String keystore = "PushCertificates.p12"; //P12文件的路径; String password = "sosoimage"; //P12文件的密码;false,指的是使用测试环境,使用正式产品环境应传入true.
Provider可以向客户端Push一条Message,但客户端有权限决定这条Message的显示方式(无、横幅、提醒)。
Push.alert("A Message", keystore, password, )false, "7bb8d508e32df651c6c239439737dbd40a88d2461ad2ac1e5dbe49ecea5ccc67");
PushNotificationPayload payload = PushNotificationPayload.complex(); payload.addAlert("A Message"); payload.addBadge(2); payload.addSound("test.aiff"); Push.payload(payload, , keystore, password, false, "7bb8d508e32df651c6c239439737dbd40a88d2461ad2ac1e5dbe49ecea5ccc67");上面的代码都有可能会有相应的Exception抛出来,需要处理。更多的使用方式可以参考 http://code.google.com/p/javapns/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... NSDictionary* payload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (payload) { ... } ... }(2)当程序在前台运行,接收到消息不会有消息提示(提示框或横幅)。当程序运行在后台,接收到消息会有消息提示,点击消息后进入程序,AppDelegate的didReceiveRemoteNotification函数会被调用(需要自己重写),消息做为此函数的参数传入,代码如下
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)payload { ... }(3)无论在哪个函数传入,消息总是一个NSDictionary对象,处理方式可以参考如下代码
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)payload { NSLog(@"remote notification: %@",[payload description]); NSString* alertStr = nil; NSDictionary *apsInfo = [payload objectForKey:@"aps"]; NSObject *alert = [apsInfo objectForKey:@"alert"]; if ([alert isKindOfClass:[NSString class]]) { alertStr = (NSString*)alert; } else if ([alert isKindOfClass:[NSDictionary class]]) { NSDictionary* alertDict = (NSDictionary*)alert; alertStr = [alertDict objectForKey:@"body"]; } application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue]; if ([application applicationState] == UIApplicationStateActive && alertStr != nil) { UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Pushed Message" message:alertStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } }