iOS 推送,服务器。证书。

两个证书 ios_development.cer    push.pl2 

到当前目录:把.cer文件转换成.pem文件:

$ openssl x509 -in ios_develoment.cer -inform der

-out PushChatCert.pem

把私钥.p12文件转换成.pem文件:

$ openssl pkcs12 -nocerts -out PushChatKey.pem -in push.p12

Enter Import Password:

MAC verified OK

Enter PEM pass phrase:          //两次输入密文,用于验证 server

Verifying – Enter PEM pass phrase:

你首先需要为.p12文件输入passphrase密码短语,这样OpenSSL可以读它。然后你需要键入一个新的密码短语来加密PEM文件。还是使用”pushchat”来作为PEM的密码短语。你需要选择一些更安全的密码短语。

注意:如果你没有键入一个PEM passphrase,OpenSSL将不会返回一个错误信息,但是产生的.pem文件里面将不会含有私钥。

最后。把私钥和证书整合到一个.pem文件里:

 cat PushChatCert.pem PushChatKey.pem > ck.pem

测试证书

$ telnet gateway.sandbox.push.apple.com 2195

Trying 17.172.232.226…

Connected to gateway.sandbox.push-apple.com.akadns.net.

Escape character is ‘^]’.

成功。

<?php

 

// 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格

$deviceToken = '740f4707bebcf74f 9b7c25d4 8e3358945f6aa01da5ddb387462c7eaf 61bb78ad';

 

// Put your private key's passphrase here:

$passphrase = 'abc123456';

 

// Put your alert message here:

$message = 'My first push test!';

 

////////////////////////////////////////////////////////////////////////////////

 

$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);

?>

 

接下来我们访问http://localhost/push/push.php

 

iphone就会接收到一条推送消息了,如果有问题的话就检查上面的操作步骤,特别是加红的部分

 

另外去除标记的方法为,在viewDidApper中加入

 

int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;

    if(badge > 0)

    {

        badge--;

        [UIApplication sharedApplication].applicationIconBadgeNumber = badge;

    }

原文地址:https://www.cnblogs.com/qingjoin/p/2980963.html