ionic-frameworkionic4lumencordova-plugin-fcm

Is there a way to send FCM push notification to all the email address i have in my local database


Well I'm setting up a database where user details would be stored using ionic as front end and lumen to make API calls. My concern is that can i send a push notification to all the people who are using my application using the email information that is in my database. Is it possible

Following is the code that i am trying to do :

public function send_notification_all(Request $request){

    $this->validate($request, [
        'title' => 'required|max:30|min:20',
        'message' => 'required'
        ]);

    $noti_title = $request->input('title');
    $noti_body = $request->input('message');

    $optionBuilder = new OptionsBuilder();
    $optionBuilder->setTimeToLive(60*20);

    $notificationBuilder = new PayloadNotificationBuilder($noti_title);
    $notificationBuilder->setBody($noti_body)
                        ->setSound('default');

    $dataBuilder = new PayloadDataBuilder();
    $dataBuilder->addData(['a_data' => 'my_data']);

    $option = $optionBuilder->build();
    $notification = $notificationBuilder->build();
    $data = $dataBuilder->build();
    $email = User::pluck('email')->toArray();
    $downstreamResponse = FCM::sendTo($email, $option, $notification, $data);

    $downstreamResponse->numberSuccess();
    $downstreamResponse->numberFailure();
    $downstreamResponse->numberModification();

    if($downstreamResponse->numberSuccess() == 0 ){
        return $this->errorResponse("unable to send notificaton to all",401);
    }else {
        return $this->successResponse(['msg' => 'message send successfully','successfull' => $downstreamResponse->numberSuccess(),'failures' => $downstreamResponse->numberFailure()]);
    }
}

Just need the opinion that is it possible to do it in this way and do let me know if there is any mistake or the changes that i have to make in my code. Waiting for the Positive response and Guidance


Solution

  • You can't send notifications using their email addresses.

    You should save in your Database the fcm_token of every user who registers. I suggest using the following package and reading the docs in it: Laravel-FCM

    You should also take care of refreshing the token if the device request so.