Reputation: 1890
The short question: How do I structure the delivery of multiple push notifications in PHP? Specifically, how do I "pack" multiple push notification messages into a single fwrite() call?
Can I simply continue to append more messages/payloads to the $apnsMessage string?:
// [connect to service]
// Packing the payload (for a single message)
$apnsMessage = chr(0) . chr(0) . chr(32);
$apnsMessage .= pack('H*', str_replace(' ', '', $recipientToken));
$apnsMessage .= chr(0) . chr(strlen($payload)) . $payload;
// Write the payload to the APNS
fwrite($apns, $apnsMessage);
// [close connection to service]
The long version:
Because Apple requires applications to batch-process push notifications (minimizing several successive connections to their APNS), I'm trying to build something in PHP that can do the job (without using the php-apns lib & memcache). Because 99% of the resources I can find are concerning a single push notification, I was hoping I could find some guidance here. I'm simply adding each message into a mysql table (queue), then every x minutes, iterating through them and sending all the unsent messages.
Does anyone have any examples / links that might help with this approach?
Thanks in advance.
Upvotes: 0
Views: 2239
Reputation: 522016
Yes, you can simply keep appending messages. The binary protocol format specifically requires strict message lengths so one message can be distinguished from the next in line. Ideally you'd be pushing one long binary string to Apple's servers all day long.
Batch processing is not ideal, you'd usually want to implement this as a daemon that keeps an open connection to the APNs servers and writes new messages to the connection as required.
Upvotes: 2