Laravel - One notification for all users
Asked Answered
D

4

5

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

Damselfish answered 21/10, 2017 at 7:15 Comment(1)
Some code on how your current notification system works could help. It might just be a matter of determining a way to flag a notification as a broadcast rather than one that's meant to reach a specific userWast
K
6

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;
Kickoff answered 25/4, 2019 at 7:20 Comment(1)
But selecting all users like that can make things pretty horrible if you have a lot of users :)Cohlier
K
2

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

Khalif answered 21/10, 2017 at 7:19 Comment(6)
I don't want email notification for that, it is a database notification which simplifies the situation I guessDamselfish
@ModySharf what exactly do you mean by database notification?Khalif
I mean it is not gonna be sent via email, just will be stored in the DBDamselfish
@ModySharf so, you can just create a record for every user by using insert() method. It can insert thousands of records by just one query.Khalif
Won't that be waste of memory, I mean the exact same notification to like million user! I believe I it can be just oneDamselfish
Actually you need to insert a record per user even that is same, because there will be no way to determine when to hide the notification for a given user but if the notification is just a simple broadcast without database then just broadcast it @ModySharf.Skippie
L
1

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)

Laveta answered 29/3, 2018 at 12:28 Comment(0)
A
0

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; 
}
Adenoid answered 3/4, 2021 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.