简体   繁体   中英

Send push notification to a group

I am not sure how to send a push notification to multiple tokens. With my script it seems to only send a push notification to one... as per Apple it would be better to have one open connection and keep it open and send messages when needed. Not sure how to do it on my php server...

$payload['aps'] = array('alert' => "New Cave report for ".$caveName,'badge' => 1, 'sound' => 'default');
$payload['condition'] = array('conditionID' => $ccID, 'caveName' => $caveName);
$payload = json_encode($payload);

// Connection Part
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = '/var/www/web543/files/apns-dev.pem';

if (!file_exists($apnsCert) )
{
    echo "Certification file not found!";
} else
{
    $streamContext = stream_context_create();
    stream_context_set_option($streamContext,'ssl','local_cert',$apnsCert);

    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort,$error,$errorString,2,STREAM_CLIENT_CONNECT,$streamContext);

    if ( !$apns )
    {
        echo "Connection Failed!".$errorString;
    } else
    {
        $db->query("SELECT DISTINCT token FROM notifications WHERE userID != '{$userID}'");
        if ($db->num_rows()>0) 
        {
            while ($db->next_record())
            {
                $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $db->f('token'))) . chr(0) . chr(strlen($payload)) . $payload;
                if ( fwrite($apns, $apnsMessage) === FALSE )
                {
                    echo "Can not write";
                }
            }
        }
    }

    fclose($apns);
}

some output of $apnsMessage

Token: 5a984922e19eab54f78fd54e24d5b02a3d30ccdbbeee34aadbdacaa687ee1261 Message: Z˜I"áž«T÷ÕN$Õ°*=0ÌÛ¾î4ªÛÚʦ‡îa†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: c607acc70bd4885bf56f3b4827523023bf93a1d644626768ab0304bb3b4414dc Message: ƬÇÔˆ[õo;H'R0#¿“¡ÖDbgh«»;D܆{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: 785ec3128972bd3d4c3e6fa1eeead97b73b0696e2361339a2467e6ba775b83ea Message: x^Ér½=L>o¡îêÙ{s°in#a3š$gæºw[ƒê†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: c592487e3c71e921d0b7a825b66ed5e58070fee709131535ac391f14febbcfdc Message: Å’H~Token: 061bc20ba3a0fc17c689e052b42b5789f502a52d43180ea114e3212077045315 Message: £ üƉàR´+W‰õ¥-C¡ã! wS†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}
Token: 26fb66fef67a122ca456f106363115285d4d7156e7c8ab6e51bd5bfa9bab2d03 Message: &ûfþöz,¤Vñ61(]MqVçÈ«nQ½[ú›«-†{"aps":{"alert":"New Cave report for Test Entry","badge":1,"sound":"default"},"condition":{"conditionID":"1","caveName":"Test Entry"}}

Ok. I found the problem.

When compiling as Debug it will send only on the first device when using the DEV certification. If I use Deployment like Ad-Hoc I have to use the Prod Servers and Prod Certificates.

Now everything works.

I think you have to run this script in loop according to the count of token.

Means first get the token array and then according to the count make a loop. Then take one device token from the index 0 and run this script. Then selcect another token at index 1 and then run again this script.

To send push notification to multiple device you have connect each time to apple server and close connection and again open and send.This is the process.

Yeah you should not be making and tearing down the socket connection every time you need to send the push. Once you establish the socket connection, you can write as many as push messages you want to any number of devices. What is the exact problem that you are observing? Is your socket connection getting dropped?

Have a look at this question if you need additional help : How to send Push Notification to multiple devices?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM