Open ghost opened 7 years ago
How do you do this? Anyone solved?
Did you tried
$message->to([$deviceToken1, $deviceToken2])
?
I have done it like this and it works... My admin users can have subscription enabled on multiple devices...
/**
Apparently, the problem is in the Notifications concept in Laravel itself. When you send a notification you send it to one recipient (e.g. Device
), but this recipient may be a collection of recipients (e.g. GroupOfDevices
). In this case, only one notification would be sent, because technically we still have only one recipient (collection). routeNotificationForFcm()
should return an array in this case.
It's the same idea as @junaid-A-khan mentioned. But it's not a really convenient approach to define a special model for that. @benwilkins could you, please, comment this? Is there any way to send Notifications to multiple tokens without defining additional models and use a simple code like:
Notification::send($devices, new SomeNotification(...));
That's a good question. The way I've done it is by creating device groups in FCM and sending to a group ID.
@VladimirBerdnik The FcmMessage is designed to take only one recipient. This can be a device token, a device group token, or a topic. Passing an array will not work.
If you're wanting to send to multiple recipients in one message, the best way to do it is to use FCM's topics or device groups.
The FcmMessage is designed to take only one recipient. This can be a device token, a device group token, or a topic. Passing an array will not work.
@benwilkins, the legacy FCM protocol is able to send a message to an array of device tokens, and this library seems to accommodate that functionality:
For this to work you need to return an array of device tokens from routeNotificationsForFcm()
:
// User.php
public function fcmTokens()
{
return $this->hasMany(FcmToken::class);
// The FcmToken model uses the table `fcm_tokens` which looks like this:
//
// fcm_tokens
// --------------------------
// user_id (foreign key)
// token
}
public function routeNotificationForFcm()
{
// Return the tokens as an array
return $this->fcmTokens->pluck('token')->toArray();
}
And this array is used by the FCM channel:
@nicoqh Ah you're right! I forgot about that. That was a PR submitted a while back.
Can anyone give me a good explanation of how to send to bulk notification ?
@VladimirBerdnik The FcmMessage is designed to take only one recipient. This can be a device token, a device group token, or a topic. Passing an array will not work.
If you're wanting to send to multiple recipients in one message, the best way to do it is to use FCM's topics or device groups.
How to create a device group ? Is this package able to send notification to a device group?
How to send message to multiple devices?