benwilkins / laravel-fcm-notification

Laravel FCM (Firebase Cloud Messaging) Notification Channel
MIT License
213 stars 91 forks source link

Message to multiple devices #2

Open ghost opened 7 years ago

ghost commented 7 years ago

How to send message to multiple devices?

exzib commented 6 years ago

How do you do this? Anyone solved?

VladimirBerdnik commented 6 years ago

Did you tried

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

?

junaid-A-khan commented 6 years ago

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

/**

chimit commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

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

benwilkins commented 6 years ago

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

Younesi commented 3 years ago

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

kuatek commented 2 years ago

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