Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
firebase_php
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
firebase_php
Commits
a8685666
Commit
a8685666
authored
1 year ago
by
Patrick Kenny
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3446620
by ptmkenny: Fix sending to multiple devices, badges
parent
594942c4
Branches
3.x
No related tags found
1 merge request
!14
Fix sending to multiple devices, badges
Pipeline
#170373
passed with warnings
1 year ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Enum/SendingDestinationType.php
+13
-0
13 additions, 0 deletions
src/Enum/SendingDestinationType.php
src/Service/FirebasePhpMessagingService.php
+18
-17
18 additions, 17 deletions
src/Service/FirebasePhpMessagingService.php
with
31 additions
and
17 deletions
src/Enum/SendingDestinationType.php
0 → 100644
+
13
−
0
View file @
a8685666
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\firebase_php\Enum
;
/**
* CloudMessage destinations.
*/
enum
SendingDestinationType
:
string
{
case
DeviceToken
=
'token'
;
case
Topic
=
'topic'
;
}
This diff is collapsed.
Click to expand it.
src/Service/FirebasePhpMessagingService.php
+
18
−
17
View file @
a8685666
...
...
@@ -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
));
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment