<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: mohamed mostafa</title>
    <description>The latest articles on DEV Community by mohamed mostafa (@upkareno).</description>
    <link>https://dev.to/upkareno</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F759633%2F8a188bf7-7652-4397-a462-51e211503f5e.jpeg</url>
      <title>DEV Community: mohamed mostafa</title>
      <link>https://dev.to/upkareno</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/upkareno"/>
    <language>en</language>
    <item>
      <title>Send notifications from Laravel to Mobile in FMC</title>
      <dc:creator>mohamed mostafa</dc:creator>
      <pubDate>Thu, 24 Mar 2022 15:13:57 +0000</pubDate>
      <link>https://dev.to/upkareno/send-notifications-from-laravel-to-mobile-in-fmc-1j59</link>
      <guid>https://dev.to/upkareno/send-notifications-from-laravel-to-mobile-in-fmc-1j59</guid>
      <description>&lt;p&gt;First create a new laravel project and add this var to .env file in your project &lt;br&gt;
&lt;code&gt;FCM_SERVER_KEY=FIRE_BASE_SERVER_KEY&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;create a new controller &lt;code&gt;NotificationController&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  public function send_message(Request $request)
   {
     // Call Your FCM Service Class
   }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now create a new service in laravel at &lt;code&gt;app/Services/FCMService.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;create a new function  :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;?php
 namespace App\Services;

 use App\Models\User;

 class FCMService
 {

    // send notification to user using firebase cloud messaging
    public static function send_notification($user, $title, $body)
    {
        $user = User::where('id', $user)-&amp;gt;first();
        $url = "https://fcm.googleapis.com/fcm/send";
        $server_key = env('FCM_SERVER_KEY');
        $headers = [
            'Authorization' =&amp;gt; 'key=' . $server_key,
            'Content-Type' =&amp;gt; 'application/json',
        ];
        if (!$user) {
            return response()-&amp;gt;json([
                'status' =&amp;gt; 'error',
                'message' =&amp;gt; 'User not found',
            ], 404);
        }

        // send via GuzzleHttp
        $client = new \GuzzleHttp\Client();
        $response = $client-&amp;gt;post($url, [
            'headers' =&amp;gt; $headers,
            'json' =&amp;gt; [
                'to' =&amp;gt; $user-&amp;gt;device_token, // user's device token for fcm
                'data' =&amp;gt; [
                    'title' =&amp;gt; $title,
                    'body' =&amp;gt; $body,
                ],
            ],
        ]);

        return $response-&amp;gt;getBody();

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;add new column to users table called &lt;code&gt;device_token&lt;/code&gt; store any new device token when user create a new account from mobile and use it to send notifications to user device from users table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now we can send request to the FCMService&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function send_message(Request $request)
{
  // Call Your FCM Service Class
  \App\Services\FCMService::send_notification($request-&amp;gt;user_id, $request-&amp;gt;title, $request-&amp;gt;body);

return redirect()-&amp;gt;back();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
