是否可以将iOS推送通知发送到两个不同的应用程序?

时间:2014-12-24 12:55:05

标签: php ios apple-push-notifications apns-php

我的情况是,我有一台服务器需要向两个不同的客户端应用程序发送推送通知,而不知道它是哪个应用程序。

我有两个不同的iOS应用程序(2个不同的包标识符),我有2个不同的所有必要认证集,每个应用程序一个。

我有一个接收deviceToken的PHP代码和一个要推送的消息。 该代码基于reywenderlich的SimplePush,可在此处找到:http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

唯一需要更改的部分是ck.pem文件,每个应用程序都会有所不同。

我能想到的一个解决方案是尝试两个不同的ck.pem文件,如果一个失败则尝试另一个。

任何人都可以帮助我在这个PHP代码中实现它吗?或者是否有更好的解决方案建议?

<?php

// Put your device token here (without spaces):
//$deviceToken = 'a6a543b5b19ef7b997b2328'; 

$deviceToken = $_GET["device_token"];
$message = $_GET["message"];
$elementID = $_GET["element_ID"];

// Put your private key's passphrase here:
$passphrase = '123456';

// Put your alert message here:
//$message = 'My first push notification! yay';

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

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

// Create the extra data
$body['extra'] = array(
    'element_id' => $elementID
    );


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

更新

解决方案是在末尾添加另一段代码,将相同的有效负载发送到第二台服务器:

//connecting to second server

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'SecondCk.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|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp) 
    exit("Failed to connect to note server: $err $errstr" . PHP_EOL);

// connected to server sending note msg
$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);

1 个答案:

答案 0 :(得分:1)

我不知道如何在PHP中执行此操作,但您应该在打开第一个连接之前创建有效负载主体+二进制通知,然后创建2个连接(或者如果可能的话,将连接循环)到Apple的推送服务器和向它们发送相同的二进制通知。

最好的祝福,
Gabriel Tomitsuka

相关问题