在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送
From: http://saeapns.sinaapp.com/doc.html
7, 客户端程序:
-
设置profile:

-
在info.plist中设置Bundle identifier:

-
在合适的位置加入下面代码,将你的应用注册到消息中心:
-
1234567891011121314
- (IBAction)action:(id)sender {//注册到消息中心:[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeNewsstandContentAvailability)];} -
在AppDelegate中加入下面代码:
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
#pragma mark -#pragma mark APNS- (void)application:(UIApplication*)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {//获得 device tokenNSLog(@"deviceToken: %@", deviceToken);/*. . . . . .在这里把deviceToken和用户信息发送到服务器端. . . . . .*///获得 唯一标示NSLog(@"uniqueIdentifier: %@", [[UIDevicecurrentDevice]uniqueIdentifier]);}- (void)application:(UIApplication*)applicationdidFailToRegisterForRemoteNotificationsWithError:(NSError*)error {NSLog(@"Error in registration. Error: %@", error);}- (void)application:(UIApplication*)applicationdidReceiveRemoteNotification:(NSDictionary*)userInfo {/*收到消息自定义事件*/if([[userInfoobjectForKey:@"aps"]objectForKey:@"alert"] !=nil) {UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"通知"message:[[userInfoobjectForKey:@"aps"]objectForKey:@"alert"]delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];[alertshow];[alertrelease];}}
8, 服务器端程序:
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
/**** 实例代码** SaeAPNS调用方法详见API文档:http://apidoc.sinaapp.com/sae/SaeAPNS.html** @author Bruce Chen**/header("Content-Type: text/html;charset=utf-8");include_once("saeapns.class.php");/* int $cert_id 许可证序号(1-10)*/$cert_id= 1;/* string $device_token 设备令牌 */$device_token='xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx';/* string $message 消息内容 */$message=date('Y-m-d H:i:s') .": ".'测试消息 from SAE';/*array $body 消息体(包括消息、提醒声音等等),格式请参考示例和{@link http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW1 Apple官方文档}*/$body=array('aps'=>array('alert'=>$message,'badge'=> 1,'sound'=>'in.caf'),'others'=>array());//实例化SaeAPNS$apns=newSaeAPNS();//推送消息$result=$apns->push($cert_id,$body,$device_token);if($result!== false) {echo'发送成功';}else{echo'发送失败';var_dump($apns->errno(),$apns->errmsg());}
源码实例下载地址: http://vdisk.weibo.com/s/SbY2
转自:http://blog.csdn.net/wave_1102/article/details/7669152






























