Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message to multiple devices #2

Open
ghost opened this issue Mar 3, 2017 · 10 comments
Open

Message to multiple devices #2

ghost opened this issue Mar 3, 2017 · 10 comments

Comments

@ghost
Copy link

ghost commented Mar 3, 2017

How to send message to multiple devices?

@exzib
Copy link

exzib commented Jan 18, 2018

How do you do this? Anyone solved?

@VladimirBerdnik
Copy link

Did you tried

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

?

@junaid-A-khan
Copy link

junaid-A-khan commented Sep 11, 2018

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

@chimit
Copy link
Contributor

chimit commented Oct 11, 2018

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

@benwilkins
Copy link
Owner

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
Copy link
Owner

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

@nicoqh
Copy link
Contributor

nicoqh commented Oct 11, 2018

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:

if (is_array($this->to)) {
$payload['registration_ids'] = $this->to;
} elseif (!empty($this->to) ) {
$payload['to'] = $this->to;
}

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:

if (is_null($message->getTo()) && is_null($message->getCondition())) {
if (! $to = $notifiable->routeNotificationFor('fcm')) {
return;
}
$message->to($to);
}

@benwilkins
Copy link
Owner

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

@Younesi
Copy link

Younesi commented Dec 27, 2020

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

@kuatek
Copy link

kuatek commented Dec 8, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants