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

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

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

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

我有一个 PHP 代码可以接收 deviceToken 和要推送的消息。 该代码基于 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 的推送服务器和向他们发送相同的二进制通知。

最好的祝福,
加布里埃尔富冢

关于php - 是否可以将 iOS 推送通知发送到两个不同的应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27637299/

相关文章:

python - 是否有一个工具可以在没有实际移动应用程序的情况下测试移动推送通知?

php - 从 JSON.parse 获取元素

ios - 如何从推送通知警报将用户重定向到 App Store 上的应用程序?

iphone - 如何创建真实的 APN 服务器以及如何使用 APN 服务器 iPhone 测试应用程序?

ios - 在 viewDidLoad 之前编写 init 方法将模型传递给 View Controller

iphone - 带标签的 Xcode 链接

ios - 从NSStrings中提取数字

php - 在 Woocommerce 产品简短描述中动态替换自定义标签

php - Composer 找不到composer.json

php - Zend 重定向 这两种方式有什么区别