标签:
本文转载至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个字符长度限制;超出范围不能发送
ios推送服务,php服务端
标签:
原文地址:http://www.cnblogs.com/Camier-myNiuer/p/4173679.html