Skip to content
Snippets Groups Projects
Commit a8685666 authored by Patrick Kenny's avatar Patrick Kenny
Browse files

Issue #3446620 by ptmkenny: Fix sending to multiple devices, badges

parent 594942c4
Branches 3.x
No related tags found
1 merge request!14Fix sending to multiple devices, badges
Pipeline #170373 passed with warnings
<?php
declare(strict_types=1);
namespace Drupal\firebase_php\Enum;
/**
* CloudMessage destinations.
*/
enum SendingDestinationType: string {
case DeviceToken = 'token';
case Topic = 'topic';
}
......@@ -9,6 +9,7 @@ use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Session\AccountInterface;
use Drupal\firebase_php\Enum\FirebasePhpSettings;
use Drupal\firebase_php\Enum\LoggingLevel;
use Drupal\firebase_php\Enum\SendingDestinationType;
use Drupal\firebase_php\Exception\FirebasePhpCredentialsNotFoundException;
use Drupal\firebase_php\Exception\FirebasePhpInvalidArgumentException;
use Drupal\firebase_php\FirebasePhpMessagingServiceInterface;
......@@ -81,7 +82,7 @@ class FirebasePhpMessagingService implements FirebasePhpMessagingServiceInterfac
* {@inheritdoc}
*/
public function sendMessageSingleDevice(string $deviceToken, Notification $notification, ?int $badge_count = NULL, array $data = [], array $apns_data = []): array {
$message = $this->createMessageFromNotificationAndAddBadge($notification, $badge_count, $deviceToken, $data, $apns_data);
$message = $this->createMessageFromNotificationAndAddBadge($notification, SendingDestinationType::DeviceToken, $deviceToken, $badge_count, $data, $apns_data);
return $this->messaging->send($message);
}
......@@ -90,7 +91,7 @@ class FirebasePhpMessagingService implements FirebasePhpMessagingServiceInterfac
* {@inheritdoc}
*/
public function sendMessageTopic(string $topic, Notification $notification, ?int $badge_count = NULL, array $data = [], array $apns_data = []): array {
$message = $this->createMessageFromNotificationAndAddBadge($notification, $badge_count, NULL, $data, $apns_data, $topic);
$message = $this->createMessageFromNotificationAndAddBadge($notification, SendingDestinationType::Topic, $topic, $badge_count, $data, $apns_data);
return $this->messaging->send($message);
}
......@@ -99,7 +100,9 @@ class FirebasePhpMessagingService implements FirebasePhpMessagingServiceInterfac
* {@inheritdoc}
*/
public function sendMessageMultipleDevices(array $deviceTokens, Notification $notification, ?int $badge_count = NULL, array $data = [], array $apns_data = []): MulticastSendReport {
$message = $this->createMessageFromNotificationAndAddBadge($notification, $badge_count, NULL, $data, $apns_data);
// For multicast, create a message to the first device token.
// Then send that message to all the device tokens.
$message = $this->createMessageFromNotificationAndAddBadge($notification, SendingDestinationType::DeviceToken, $deviceTokens[0], $badge_count, $data, $apns_data);
return $this->messaging->sendMulticast($message, $deviceTokens);
}
......@@ -108,27 +111,27 @@ class FirebasePhpMessagingService implements FirebasePhpMessagingServiceInterfac
*
* @param \Kreait\Firebase\Messaging\Notification $notification
* The notification to be sent.
* @param \Drupal\firebase_php\Enum\SendingDestinationType $destination_type
* The type of destination to send the message to. Used for validation.
* @param string|null $destination
* The destination (deviceToken or topic). Optional for multicast.
* @param int|null $badge_count
* The optional badge count.
* @param string|null $deviceToken
* The optional device token.
* @param array|null $data
* The optional message data.
* @param array|null $apns_data
* The optional APNS data.
* @param string|null $topic
* The optional topic.
*
* @return \Kreait\Firebase\Messaging\Message
* The message to be sent.
*/
private function createMessageFromNotificationAndAddBadge(
Notification $notification,
SendingDestinationType $destination_type,
?string $destination,
?int $badge_count = NULL,
?string $deviceToken = NULL,
?array $data = NULL,
?array $apns_data = NULL,
?string $topic = NULL,
): Message {
$message_inputs = [
'notification' => $notification,
......@@ -137,24 +140,22 @@ class FirebasePhpMessagingService implements FirebasePhpMessagingServiceInterfac
$message_inputs['data'] = $data;
}
if (is_string($deviceToken) && trim($deviceToken) !== '') {
/** @var non-empty-string $deviceToken */
$message_inputs['token'] = $deviceToken;
}
elseif (is_string($topic) && trim($topic) !== '') {
/** @var non-empty-string $topic */
$message_inputs['topic'] = $topic;
if (is_string($destination) && trim($destination) !== '') {
/** @var non-empty-string $destination */
$message_inputs[$destination_type->value] = $destination;
}
else {
throw new FirebasePhpInvalidArgumentException('Either device token or topic must be provided.');
}
// @phpstan-ignore-next-line Phpstan cannot read $destination_type->value.
$message = CloudMessage::fromArray($message_inputs);
if (is_array($apns_data)) {
$message = $message->withApnsConfig($apns_data);
}
elseif (is_int($badge_count) && $badge_count >= 0) {
if (is_int($badge_count) && $badge_count >= 0) {
$message->withApnsConfig(ApnsConfig::new()->withBadge($badge_count));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment