这个是上接入门11 ,当不使用第三方推送的时候,ios 中要自己调用 苹果的 APNs服务器,如果使用了信鸽,或者其他推送,那么请略过
开始使用 .net (JdSoft.Apns.Notifications) 并提供了正确的 p12文件,后来好好的就不能发送了,好奇怪,折腾了两天没有找到答案
于是使用 php版 并提供正确的 pem文件
前提文件
1-----请求文件 也就是制作证书最早需要的 csr 文件
2-----下载推送证书 aps_production.cer 我这里使用的是正式发布版
3-----导出发布证书或者开发证书下对应的私钥 命名为 Push.p12文件,并设置密码
pem 制作步骤如下
1、把.cer的SSL证书转换为.pem文件,执行命令:
openssl x509 -in aps_production.cer -inform der -out PushChatCert.pem
2、把私钥Push.p12文件转化为.pem文件:
openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
3、对生成的这两个pem文件再生成一个pem文件,来把证书和私钥整合到一个文件里:
cat PushChatCert.pem PushChatKey.pem > ck.pem
最后贴上php provider 代码
<?php
// 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)
$deviceToken = 'd784d0f9b8b1fe1bd3b25437ee20202554572bfcea005bb8b355ef9a4c3127e1';
// Put your private key's passphrase here:
$passphrase = '123123';
// Put your alert message here:
$message = 'Hello!';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
//这个为正是的发布地址
$fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
//这个是沙盒测试地址,发布到appstore后记得修改哦
//$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;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result){
echo 'Message not delivered' . PHP_EOL;
}
else{
echo 'Message successfully delivered' . PHP_EOL;
}
// Close the connection to the server
fclose($fp);
?>
对应的app里面获取 deviceToken 的代码
- (void) application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"1: %@", deviceToken);
NSString* result = [[[[deviceToken description]
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""] ;
NSLog(@"2: %@", result);
// myDeviceToken =result;
[MySingleton sharedSingleton].myDeveiceToken = result;//我这里是混合开发,所以获取之后传递给js 端调用,再保存到服务器端数据库
NSLog(@"3My token is: %@", [MySingleton sharedSingleton].myDeveiceToken);
// re-post ( broadcast )
NSString* token = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
[[NSNotificationCenter defaultCenter] postNotificationName:CDVRemoteNotification object:token];
}
当然,如果你对apns 自主构建推送服务(不包括 使用第三方 比如百度云,jpush,信鸽等)仍然有不清楚的地方,可以联系我,虽然不一定能解决,但可能会帮到你,节约时间是第一位,毕竟对于我们来说时间是腰椎的最大杀手,呵呵
qq :lorinzhang@163.com
email:lorinzhang@163.com