kreait / firebase-php

Unofficial Firebase Admin SDK for PHP
https://firebase-php.readthedocs.io/
MIT License
2.23k stars 425 forks source link

Retrieve message_id after successful message send #925

Closed rolinger closed 1 week ago

rolinger commented 1 week ago

Describe the feature you would like to see

Sorry if I missed this in the docs somewhere, but I don't see a method to retrieve the message_id of a successfully sent message.

In Legacy FCM it was an object key value returned but I don't see the method defined in the here:

Legacy FCM:

    $response = $client->send($notification);
    if (isset($response['message_id'])) {
        ... 
        ...
    }
jeromegamez commented 1 week ago

When you use the Messaging::send() method, you will get back the response returned by the Firebase FCM API:

[
  "name" => "projects/my-project/messages/6810356097230477954"
]

When you use sendMulticast() or sendAll() methods, you'll get an instance of MulticastSendReport (Docs) that you can iterate to get the results:

use Kreait\Firebase\Messaging\SendReport;

$report = $messaging->sendAll($messages);

$names = $report->map(
      fn (SendReport $report) => $report->result)
);

// or

foreach ($report->getItems() as $item) {
    // https://github.com/kreait/firebase-php/blob/7.14.0/src/Firebase/Messaging/SendReport.php
    // ...
}

I admit this isn't well documented (or at all), but you're also the first person I remember asking about this - I always assumed this to be not super useful information ๐Ÿ™ˆ.

Until now, that is - could you share what one can do with this message ID?

(MulticastSendReport as a name and the handling in general is because ofโ€ฆ legacy ๐Ÿ˜…)

jeromegamez commented 1 week ago

If you don't really care about the message ID, only if the message has been successfully sent or not, you can use a try catch block

use Kreait\Firebase\Exception\MessagingException;

try {
    $messaging->send($message);
} catch (MessagingException $e) {
    echo $e->getMessage();
    print_r($e->errors());
}

or, in the case of batch sends as described here in the Multicast Docs

rolinger commented 1 week ago

Thanks for the quick responses. The above responses are exactly what we are looking for.

Our platform allows clients to do push_notifications to their member base, we use the message_id to track various analytics related to client usage, client -> user interaction, user engagement per message. Its something we implemented with legacy FCM and to keep things humming we need the message ID so we don't have to rethink our methodology. :-)

jeromegamez commented 1 week ago

Got it, thanks! I took the opportunity to slightly improve the docs on batch sends, it should be visible in the docs with the next release ๐Ÿคž๐Ÿป

Having written "our platform allows clients", you probably know what's coming next ๐Ÿ˜…: If the SDK and my work are useful to you (also see the intro in the README), please consider becoming a sponsor! There's a Sponsors-only discord server with a private category just for your company/team waiting for you as well ๐Ÿ˜‡