laravel-fcm-notification icon indicating copy to clipboard operation
laravel-fcm-notification copied to clipboard

Message to multiple devices

Open ghost opened this issue 7 years ago • 10 comments

How to send message to multiple devices?

ghost avatar Mar 03 '17 14:03 ghost

How do you do this? Anyone solved?

exzib avatar Jan 18 '18 21:01 exzib

Did you tried

$message->to([$deviceToken1, $deviceToken2])

?

VladimirBerdnik avatar Jan 19 '18 02:01 VladimirBerdnik

I have done it like this and it works... My admin users can have subscription enabled on multiple devices...

/** * Route notifications for the FCM channel. * * @return string */ public function routeNotificationForFcm() { // return array of tokens return $this->fcmSubscriptions->pluck('device_token')->toArray(); }

junaid-A-khan avatar Sep 11 '18 13:09 junaid-A-khan

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(...));

chimit avatar Oct 11 '18 05:10 chimit

That's a good question. The way I've done it is by creating device groups in FCM and sending to a group ID.

benwilkins avatar Oct 11 '18 14:10 benwilkins

@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.

benwilkins avatar Oct 11 '18 14:10 benwilkins

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:

https://github.com/benwilkins/laravel-fcm-notification/blob/27c5a9792195945afdf389037da5b59f26e35450/src/FcmMessage.php#L265-L269

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:

https://github.com/benwilkins/laravel-fcm-notification/blob/27c5a9792195945afdf389037da5b59f26e35450/src/FcmChannel.php#L46-L52

nicoqh avatar Oct 11 '18 15:10 nicoqh

@nicoqh Ah you're right! I forgot about that. That was a PR submitted a while back.

benwilkins avatar Oct 11 '18 16:10 benwilkins

Can anyone give me a good explanation of how to send to bulk notification ?

Younesi avatar Dec 27 '20 18:12 Younesi

@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?

kuatek avatar Dec 08 '21 10:12 kuatek