本文转载至http://my.oschina.net/AStar/blog/176531

 
生成证书 
证书生成参考:https://parse.com/tutorials/ios-push-notifications 
会生成三个文件 
xxx.cer 
xxx.certSigningRequest 
xxx.p12

用PHP做推送需要 
推送证书生成。 
生成pem步骤: 
1.下载下来你配置好的推送证书aps_developer_identity.cer 文件。 
2.转换 .cer 文件到 .pem 文件: 
openssl x509 -in aps_developer_identity.cer -inform der -out PushChatCert.pem 
3.在把你“钥匙”推送证书导出成的.p12到.pem文件: 
openssl pkcs12 -nocerts -out PushChatKey.pem -in PushChatKey.p12 
Enter Import Password:  
MAC verified OK 
Enter PEM pass phrase:  
Verifying - Enter PEM pass phrase:  
4.合并两个pem文件:cat PushChatCert.pem PushChatKey.pem > ck.pem 
5.测试证书是否可用: 
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushChatCert.pem -key PushChatKey.pem

****************客户端推送处理,以下在AppDelegate文添加**************** 
  //是否支持注册推送 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    /*............... */ 
   //消息推送支持的类型 
    UIRemoteNotificationType types =(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert); 
    //注册消息推送 
    [[UIApplication sharedApplication]registerForRemoteNotificationTypes:types]; 
}

发送推送的思路:先获取手机的 deviceToken ,然后发送到我们的服务器 
 //获取DeviceToken成功 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 

    NSLog(@"注册device token"); 
    NSLog(@"DeviceToken: {%@}",deviceToken); 
    //这里进行的操作,是将Device Token发送到服务端 
    NSString *tokenStr = [deviceToken description]; 
    NSString *pushToken = [[[tokenStr stringByReplacingOccurrencesOfString:@"<" withString:@""] 
                            stringByReplacingOccurrencesOfString:@">" withString:@""] 
                           stringByReplacingOccurrencesOfString:@" " withString:@""];

//注册到我们的服务器 
    NSString *url = @"http://api.xxxxxxcom/index.php?r=site/saveIphoneDeviceToken&device_token="; 
    url = [url stringByAppendingFormat:@"%@", pushToken]; 
    [[AFOSCClient sharedClient]getPath:url parameters:Nil 
                                success:^(AFHTTPRequestOperation *operation, id responseObject) { 
                                    NSLog(@"注册成功"); 
                               } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
                                   NSLog(@"注册失败"); 
                               }]; 
}

然后是处理接收推送消息 
 //注册消息推送失败 
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 

    NSLog(@"注册消息推送失败"); 
    NSLog(@"Register Remote Notifications error:{%@}",[error localizedDescription]); 
}

//处理收到的消息推送 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 

//    NSLog(@"Receive remote notification : %@",userInfo); 
//    NSLog(@"%@",[userInfo objectForKey:@"aps"]); 
    NSDictionary *aps = [userInfo objectForKey:@"aps"]; 
//    NSLog(@"%@",[aps objectForKey:@"alert"]); 
    NSString *msg = [aps objectForKey:@"alert"]; 
//    NSLog(@"%@",[userInfo objectForKey:@"alert"]); 
    UIAlertView *alert = 
    [[UIAlertView alloc] initWithTitle:@"温馨提示" 
                               message:msg 
                              delegate:nil 
                     cancelButtonTitle:@"确定" 
                     otherButtonTitles:nil]; 
    [alert show]; 
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;//把badge设置为0 
}

****************服务器端push.php****************

<?php 
//手机注册应用返回唯一的deviceToken 
$deviceToken = 'eb87b209 6395a040 a1cf85e5 15b6e972 20ec883f 6b3ac80f 02b92a1a 58cf273e'; 
//ck.pem通关密码 
$pass = '13!@#23';    
//消息内容 
$message = '这是一条推送测试通知1'.time(); 
//badge我也不知是什么 
//$badge = 1; 
//sound我也不知是什么(或许是推送消息到手机时的提示音) 
$sound = 'Duck.wav'; 
//建设的通知有效载荷(即通知包含的一些信息) 
$body = array(); 
$body['aps'] = array('alert' => $message); 
//if ($badge) 
//  $body['aps']['badge'] = $badge; 
if ($sound) 
  $body['aps']['sound'] = $sound; 
//把数组数据转换为json数据 
$payload = json_encode($body); 
echo strlen($payload),"\r\n";

//下边的写法就是死写法了,一般不需要修改, 
//唯一要修改的就是:ssl://gateway.sandbox.push.apple.com:2195这个是沙盒测试地址,ssl://gateway.push.apple.com:2195正式发布地址 
$ctx = stream_context_create(); 
stream_context_set_option($ctx, 'ssl', 'local_cert', dirname(__FILE__) . '\\' . 'dev_ck.pem');   
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass); 
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); 
if (!$fp) { 
    print "Failed to connect $err $errstr\n"; 
    return; 

else { 
   print "Connection OK\n<br/>"; 

// send message 
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload; 
print "Sending message :" . $payload . "\n";   
fwrite($fp, $msg); 
fclose($fp); 
?>

php服务端如果出问题,请检查是否开通ssl模块 
Apache需要开启ssl模块,通过查看Apache的官方文档得知,使用ssl需要Apache开启三个支持模块分别是: 
mod_include 
mod_cgi 
mod_expires 
*注意:ios 的推送消息有256个字符长度限制;超出范围不能发送

最新文章

  1. NDK笔记(二)-在Android Studio中使用ndk-build
  2. 机器学习相关的Awesome系列
  3. 想让你的java代码更漂亮,用枚举吧
  4. Android学习十二:自定义控件学习
  5. 不简单的SQL查询和排序语句
  6. 警惕多iframe下的同名id引起的诡异问题
  7. 11-10 CC150第一章
  8. cocos2dx release note
  9. Delphi 调用 Rest 服务的一些
  10. sqlite中的自增主键
  11. JAVA 正则表达式 (超详细)
  12. openshift django目录结果
  13. C# string.Format格式化时间或货币
  14. 在后台代码中引入XAML的方法
  15. 通过UIImagePickerController完成照相和相片的选取
  16. hibernate-annotation CascadeType.PERSIST不起作用的解决方法
  17. ORM全集
  18. spring扩展点总结
  19. shell编程之测试和判断
  20. [django]主次表如何取出对方数据[主表obj.子表__set()]

热门文章

  1. 设计模式之模板模式(PHP实现)
  2. j2ee、mvn、eclipse、Tomcat等中文乱码问题解决方法
  3. k8s restful API 结构分析
  4. devops流程
  5. Resin服务器部署web项目
  6. WinSock基本知识
  7. JAVA反射机制--静态加载与动态加载
  8. perl学习笔记一
  9. Dephi泛型generic的应用
  10. css 温故而知新 select-option 文字方向居右