发送APNS消息数组而不是发送单个消息

foreach ($users AS $userToken) {
    $msg = chr(0) . pack('n', 32) . pack('H*', $userToken) . pack('n', strlen($payload)) . $payload;

    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));

    if (!$result)
        echo 'Message not delivered' . PHP_EOL;
    else
        echo 'Message successfully delivered' . PHP_EOL;
}

For reference, $fp is a 'PHP Stream Context'. $users is an array of user tokens that have registered for push notifications.

Is there a better way of writing the above code so that fwrite can somehow send an array of messages to APNS rather than writing to the socket for each individual user?

Notification payload

Push notifications are not intended to contain the actual payload (in your case the list of messages) but to notify the subscriber about new content. When receiving the notification the application retrieves the actual content from a web service.

From the docs:

Important: Delivery of notifications is a “best effort”, not guaranteed. It is not intended to deliver data to your app, only to notify the user that there is new data available.

(Source: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html)

Additionally, the size of the notification payload is limited to 256 bytes.

The maximum size allowed for a notification payload is 256 bytes

(Source: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html)

As a result you are supposed to provide a web service for your app to retrieve the actual list of messages. The notification payload only contains the number of new messages.

Communitcation with APNS

Your concept of sending individual requests for every user is correct, this is the way the APNS is designed. To increase the overall performance use multiple connections (using multiple threads) and ensure the underlying TCP connection is reused.

From the docs:

You may establish multiple connections to the same gateway or to multiple gateway instances. If you need to send a large number of push notifications, spread them out over connections to several different gateways. This improves performance compared to using a single connection: it lets you send the push notifications faster, and it lets APNs deliver them faster.

(Source: https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW6)

Keep in mind that it is your job to use the Feedback Service to remove unsubscribed device IDs from your internal list of subscribers. This also helps you in increasing the overall performance.