好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php如何实现ios推送

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 
[self.window makeKeyAndVisible];
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
    return YES;
 
}
 
 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {
    NSLog(@"regisger success:%@", pToken);
    
    //注册成功,将deviceToken保存到应用服务器数据库中
    
}
 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    // 处理推送消息
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"通知" message:@"我的信息" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
NSLog(@"%@", userInfo);
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Regist fail%@",error); 
    
  
}

到这里一切顺利的话我们就可以在真机运行了,注册成功我们会得到iphone 的deviceToken,

My token is:

<740f4707 bebcf74f 9b7c25d4 8e335894 5f6aa01d a5ddb387 462c7eaf 61bb78ad>

四、在应用服务器采用php的方式将消息推送给APNS,

1、php连接APNS也是需要证书的,还记得我们上面获得的几个证书吗?打开终端,对上面的证书做如下处理,

cd 进入证书所在目录

把.cer文件转换成.pem文件:

$ openssl x509 -in aps_developer_identity.cer -inform der
-out PushChatCert.pem

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

$ openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
Enter Import Password:
MAC verified OK
Enter PEM pass phrase:
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测试数据 2195
Trying 17.172.232.226…
Connected to gateway.sandbox.push-apple测试数据.akadns.net.
Escape character is ‘^]’.

它将尝试发送一个规则的,不加密的连接到APNS服务。如果你看到上面的反馈,那说明你的MAC能够到达APNS。按下Ctrl+C 关闭连接。如果得到一个错误信息,那么你需要确保你的防火墙允许2195端口。

然后再次连接,这次用我们的SSL证书和私钥来设置一个安全的连接:

$ openssl s_client -connect gateway.sandbox.push.apple测试数据:2195
-cert PushChatCert.pem -key PushChatKey.pem
Enter pass phrase for PushChatKey.pem:

你会看到一个完整的输出,让你明白OpenSSL在后台做什么。如果连接是成功的,你可以键入一些字符。当你按下回车后,服务就会断开连接。如果在建立连接时有问题,OpenSSL将会给你一个错误消息

ck.pem文件就是我们需要得到php连接APNS 的文件,将ck.pem和push.php放入同一目录上传到服务器,push.php的代码如下:

<?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测试数据:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);
//这个是沙盒测试地址,发布到appstore后记得修改哦
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple测试数据: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;
    }

以上就是php如何实现ios推送的详细内容!

查看更多关于php如何实现ios推送的详细内容...

  阅读:47次