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


Is it possible to send iOS push notifications to two different apps?

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

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

我有一个接收设备令牌的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);
我不知道

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

此致敬意
加布里埃尔·富冢