Mody Sharf
Mody Sharf

Reputation: 53

Laravel - One notification for all users

I have social netwrok website using laravel. My notification system works well when the notification is set for a specific user. But suppose that a user became moderator and I want to notify all users of that event Is there a way to do so without inserting the same notification to all users?

One Solution that came to my mind is

I am thinking of setting the notification user_id to 0 which indicates that it is for all users, and by the way, I don't want the read_at property in this kind of notifications so no need for another table with a FK, So if this can be the solution so how to insert it and how to retrieve them along user's notifications relationship

Upvotes: 5

Views: 12821

Answers (4)

vishwajit76
vishwajit76

Reputation: 2448

Fetch all user's fcm tokens from mysql table

    $token_ids = Userdevices::pluck('fcm_token')->toArray();
    $this->send_admin_notification($token_ids,"title","body message","image url");

Send notification to all users

public function send_admin_notification($token_ids ,$title, $message,$imageURL){
    $authorization_key = "AAAADnklIkA:APA91...";

    $finalPostArray = array('registration_ids' => $token_ids,
                            'notification' => array('body' => $message,
                                                    'title' => $title,
                                                    "image"=> $imageURL),
                            "data"=> array("click_action"=> "FLUTTER_NOTIFICATION_CLICK",
                                            "sound"=> "default", 
                                            "status"=> "done")); 
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://fcm.googleapis.com/fcm/send");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($finalPostArray));  //Post Fields
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: key='.$authorization_key));
    $server_output = curl_exec ($ch);
    curl_close ($ch);
    //echo $server_output; 
}

Upvotes: 0

vimuth
vimuth

Reputation: 5602

You can simply use Notification Facade. You can take all users and send them notification like this,

$users = Users::all();
Notification::send($users, new MyNotification($param));

And you need to call Notification facade with use like this,

use Illuminate\Support\Facades\Notification;

Upvotes: 6

Wonka
Wonka

Reputation: 8674

I had the same issue as you and used the same approach of user_id 0, to denote it's for all followers. One notification only gets inserted with the data and an extra subnotifications table was setup to hold the reference to the notification id for each follower without repeating the data.

Check out the full solution here: Laravel 5.3 - Single Notification for User Collection (followers)

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

If you want to notify many users, you could iterate over all users an use queues.

In case if you just want to send a bunch of emails at once, use one of the available packages which use Mail service APIs directly to send chunks of emails instead of sending them one by one.

If you want to broadcast an event to all current users who use your app, use Laravel Broadcasting feature for that. What you want is to use public channel.

Events are broadcast over "channels", which may be specified as public or private. Any visitor to your application may subscribe to a public channel without any authentication or authorization.

https://laravel.com/docs/5.5/broadcasting#concept-overview

Upvotes: 2

Related Questions