private function messageFactory(array $message, ConnectionInterface $connection, $projectId, $encode)
    {
        if (!isset($message['message'])) {
            throw new GoogleException('Invalid message data.');
        }

        if (isset($message['message']['data']) && $encode) {
            $message['message']['data'] = base64_decode($message['message']['data']);
        }

        $subscription = null;
        if (isset($message['subscription'])) {
            $subscription = new Subscription(
                $connection,
                $projectId,
                $message['subscription'],
                null,
                $encode
            );
        }

        return new Message($message['message'], [
            'ackId' => (isset($message['ackId'])) ? $message['ackId'] : null,
            'subscription' => $subscription
        ]);
    }