简体   繁体   English

向群组发送推送通知

[英]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. 在我的脚本中,似乎只向一个发送推送通知...按照Apple的说法,最好有一个打开的连接并保持打开状态并在需要时发送消息。 Not sure how to do it on my php server... 不知道如何在我的PHP服务器上执行此操作...

$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 $ 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. 当编译为Debug时,使用DEV认证时,它将仅在第一台设备上发送。 If I use Deployment like Ad-Hoc I have to use the Prod Servers and Prod Certificates. 如果我像Ad-Hoc这样使用Deployment,则必须使用产品服务器和产品证书。

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. 然后从索引0获取一个设备令牌并运行此脚本。 Then selcect another token at index 1 and then run again this script. 然后在索引1处选择另一个令牌,然后再次运行此脚本。

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. 要将推送通知发送到多个设备,您必须每次都连接到Apple服务器并关闭连接,然后再次打开并发送。此过程。

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? 如果您需要其他帮助,请查看以下问题: 如何将推送通知发送到多个设备?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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