0

我想向所有设备发送推送通知。我通过将所有设备令牌保存在我的数据库中来做到这一点。然后loop将我的消息发送到所有设备。

这是我将设备令牌发送到服务器的代码:

NSString *deviceTokenString = [[[deviceToken description]
                           stringByReplacingOccurrencesOfString: @"<" withString: @""]
                          stringByReplacingOccurrencesOfString: @">" withString: @""];


NSString *devicedeviceTokenForRealNow = deviceTokenString;

deviceTokenString = [deviceTokenString stringByReplacingOccurrencesOfString:@" " withString:@"+"];

NSString *strURL = [NSString stringWithFormat:@"http://website/DeviceToken.php?devicetoken=%@",deviceTokenString];
NSLog(@"%@", strURL);
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

NSLog(@"%@", strResult);

NSLog(@"%@", devicedeviceTokenForRealNow);

这是我的PHP代码:

<?PHP

if($_POST['message']){

///COUNT THE NUMBER OF DEVICES THAT IS IN THE DATABASE TO USE IN LOOP

    $con = mysql_connect(localhost,load2unet_root,hzXC3rUm) or die(mysql_error());
    mysql_select_db(load2unet_db,$con) or die(mysql_error());

    $sql = "SELECT COUNT(DeviceID) FROM DeviceToken";
    $res = mysql_query($sql,$con) or die(mysql_error());

    $row = mysql_fetch_array($res);

    $hello .= "\"" . $row["COUNT(DeviceID)"] . "\"";

    while($row = mysql_fetch_array($res)){
        $hello .= " , \"" . $row["COUNT(DeviceID)"] . "\"";

    }


/// USE LOOP TO SEND THE PUSH NOTIFICATION TO ALL DEVICES

for ($i=1; $i<=$hello; $i++){


    $con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error());
    mysql_select_db($DB_Name,$con) or die(mysql_error());

    $sql = "SELECT DeviceToken FROM $DB_Table WHERE DeviceID = $i;";
    $res = mysql_query($sql,$con) or die(mysql_error());

    $row = mysql_fetch_array($res);

    //$result .= "\"" . $row["DeviceToken"] . "\"";
    $deviceToken .= "" . $row["DeviceToken"] . "";
    while($row = mysql_fetch_array($res)){
        $deviceToken .= "" . $row["DeviceToken"] . "";

    }


$message = stripslashes($_POST['message']);

$payload = '{
                "aps" : 

                    { "alert" : "'.$message.'",
                      "badge" : 1,
                      "sound" : "bingbong.aiff"
                    } 
            }';

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'no');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'no');
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if(!$fp){
    //print "Failed to connect $err $errstrn";
    return;
} else {
    //print "Notifications sent!";
}

$devArray = array();
$devArray[] = $deviceToken;

foreach($devArray as $deviceToken){
    $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack        ("n",strlen($payload)) . $payload;
    //print "sending message :" . $payload . "n";
    fwrite($fp, $msg);
}
fclose($fp);
print "This is the Device Token Sent Each Time".$deviceToken."\n";

}

}

使用此代码,我已经尝试过 for ($i=1; $i<=1; $i++)并且有效。但不是为了for ($i=1; $i<=6; $i++)

有人可以帮忙吗?任何帮助将不胜感激!:D

4

1 回答 1

0

试试ApnsPHP库,

这里还有一个 push many 演示 sample_push_many.php

希望这可以帮到你。

谢谢

于 2013-03-19T07:29:24.010 回答