项目要求做个推送功能,研究了一天,在此跟各位分享下。因为之前做了一年的php,所以服务端我自己写的,php语言。
1:请求证书,APPID各种繁琐的操作,我就不多话了,我也是看网上例子的。 http://luoyl.info/blog/2012/02/apple_push_notification_guide/
iphone端的代码: 
#define push_server @"http://192.168.0.123/push/apns.php"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    /** 注册推送通知功能, */
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    application.applicationIconBadgeNumber = 0;
    //判断程序是不是由推送服务完成的
    if (launchOptions) {
        NSDictionary* pushNotificationKey = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if (pushNotificationKey) {
            application.applicationIconBadgeNumber = 0;
        }
    }}/** 接收从苹果服务器返回的唯一的设备token,然后发送给自己的服务端*/ 
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString* devices_token = [NSString stringWithFormat:@"%@",deviceToken];
    NSString* devices_name = [[UIDevice currentDevice] name];
    NSString* devices_version = [[UIDevice currentDevice] systemVersion];
    NSString* devices_type = [[UIDevice currentDevice] model];
    NSString* mode = @"Development";
    NSString *strUrl = [NSString stringWithFormat:@"%@?action=registerDevices&devices_token=%@&devices_name=%@&devices_version=%@&devices_type=%@&mode=%@",
                        push_server,devices_token,devices_name,devices_version,devices_type,mode];
    strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strUrl];
    
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    //发送URL请求
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}//程序处于启动状态,或者在后台运行时,会接收到推送消息,解析处理
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"\napns -> didReceiveRemoteNotification,Receive Data:\n%@", userInfo);
    //把icon上的标记数字设置为0,
    application.applicationIconBadgeNumber = 0;
    if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
        if(application.applicationState ==UIApplicationStateActive){
            [self alertNotice:@"推送通知" withMSG:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] cancleButtonTitle:@"OK" otherButtonTitle:nil];
        }
        NSString *strUrl = [NSString stringWithFormat:@"%@?action=cleanBadgeNumber&id=%@&badge=%d",
                            push_server,[[userInfo objectForKey:@"aps"] objectForKey:@"id"],0];
        strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:strUrl];
        
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
        //发送URL请求
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
}
php端代码,下载地址:http://www.cocoachina.com/bbs/job.php?action=download&aid=47929,环境搭配:php+mysql如果不明白的地方,上微博问我。
http://weibo.com/1999711542/ @吴伟滨Double

解决方案 »

  1.   

    哎,我做推送的时候PHP端总是不好使,想找一个会PHP 又会IOS的真是难~~附上我写的PHP,虽然不能直接拿来用,但是,思想在里面~<?php
    // 发布地址:gateway.push.apple.com:2195 // 测试通过
    require_once "/home/bae/app/include/db.php";
     
    $db_obj = new DB();
    $sql = "select appleID from appleDevice ";
    $result = $db_obj->db_array($sql);
     
    $countArray = count($result); 
     
    $passphrase = 'zhaojian';
    $message = '开发-推送-测试!'; 
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    $fp = stream_socket_client(
        'ssl://gateway.sandbox.push.apple.com:2195', $err,
        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);
     
    echo 'Connected to APNS' . PHP_EOL;
     
    $body['aps'] = array(
        'alert' => $message,
        'sound' => 'default'
        );
     
    $payload = json_encode($body); for($i = 0; $i < $countArray; $i++)
    {
    $deviceToken = $result[$i]['appleID'];
       
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
     
    $result1 = fwrite($fp, $msg, strlen($msg));
     
    if (!$result1)
        echo 'Message not delivered' . PHP_EOL;
    else
        echo 'Message successfully delivered' . PHP_EOL;
      }
    fclose($fp);   ?>
    请大神们指教!