duangong0690
2014-12-24 12:55
浏览 68
已采纳

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

Im in a situation where i have one server that needs to send push notification to two different client apps without knowing which app is it.

I have two different iOS apps (2 different bundle identifiers) and i have 2 different sets of all the necessary certifications, one for each app.

I have a PHP code that receives the deviceToken and a the message to be pushed. the code is based on reywenderlich's SimplePush that can be found here: http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

The only part that need to change is the ck.pem file that will be different for each app.

One solution i can think of would be to try the two different ck.pem files, if one fails try the other one.

Can any one help me with implementing that in this PHP code ? or if there are any better solution suggestions ?

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

UPDATE:

the solution is to add another piece of code at the end, to send the same payload to the second server:

//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条回答 默认 最新

  • duanliang789262 2014-12-24 13:49
    已采纳

    I don't know how to do this in PHP, but you should simply create the payload body + binary notification before opening the first connection and then create 2 connections(or loop a connection, if possible) to Apple's Push Server and send the same binary notification to both of them.

    Best regards,
    Gabriel Tomitsuka

    打赏 评论

相关推荐 更多相似问题