Reputation: 4245
I have a table like this
|------------|-----------|
|device_token|badge_count|
|------------|-----------|
|123456789 |3 |
|------------|-----------|
|987654321 |2 |
|------------|-----------|
I have the php to create two arrays:
$query = mysql_query("SELECT * FROM push");
$deviceToken = array();
while($row = mysql_fetch_assoc($query)){
$deviceToken[] = $row['device_token'];
$badgeCount[] = $row['badge_count'];
}
I then insert them like this:
foreach($deviceToken as $key=>$value){
echo $badgeCount[$key];
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => $badgeCount[$key],
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $value) . pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result){
die( 'Message not delivered' . PHP_EOL);
}
}
This returns the echo 3
2
. But the $payload
is not successfully giving the correct "badge number"
and in the push notification no badge number is being showed. And if I replace 'badge' => $badgeCount[$key]
with 'badge' => 2
the badge number shows 2 ?
Any ideas what I am doing wrong?
Upvotes: 1
Views: 158
Reputation: 45490
Return the associative array:
$query = mysql_query("SELECT device_token, badge_count FROM push");
$devices = array();
while($row = mysql_fetch_assoc($query)){
$devices[] = $row;
}
Loop through the devices:
foreach($devices as $device){
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => (int)$device['badge_count'],//very important cast !!!
);
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $device['device_token'])
. pack('n', strlen($payload)) . $payload;
$result = fwrite($fp, $msg, strlen($msg));
if (!$result){
die( 'Message not delivered' . PHP_EOL);
}
}
Upvotes: 2