{"id":468142,"date":"2022-06-27T21:07:43","date_gmt":"2022-06-27T18:07:43","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-send-mobile-push-notifications-with-php-and-firebase\/"},"modified":"2022-06-27T21:07:43","modified_gmt":"2022-06-27T18:07:43","slug":"how-to-send-mobile-push-notifications-with-php-and-firebase","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/","title":{"rendered":"#How to Send Mobile Push Notifications With PHP and Firebase"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a256faecb25c\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a256faecb25c\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#%E2%80%9CHow_to_Send_Mobile_Push_Notifications_With_PHP_and_Firebase%E2%80%9D\" >&#8220;How to Send Mobile Push Notifications With PHP and Firebase&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Outlining_the_Architecture\" >Outlining the Architecture<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Creating_Your_Firebase_Project\" >Creating Your Firebase Project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Preparing_Your_PHP_Application\" >Preparing Your PHP Application<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Registering_Client_Tokens\" >Registering Client Tokens<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Sending_Notifications\" >Sending Notifications<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Handling_FCM_Response_Data\" >Handling FCM Response Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Adding_Arbitrary_Data_to_Notifications\" >Adding Arbitrary Data to Notifications<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Setting_Message_Priorities\" >Setting Message Priorities<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Time_to_Live\" >Time to Live<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Badges_on_iOS\" >Badges on iOS<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-send-mobile-push-notifications-with-php-and-firebase\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_to_Send_Mobile_Push_Notifications_With_PHP_and_Firebase%E2%80%9D\"><\/span>&#8220;How to Send Mobile Push Notifications With PHP and Firebase&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-15942\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/2aecdc0c.jpg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Photo of a smartphone showing push notifications\" width=\"1200\" height=\"675\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-photo\/man-hand-holding-notifications-smartphone-all-450493399\">McLittle Stock\/Shutterstock.com<\/a><\/span><\/figcaption><\/figure>\n<p>Google\u2019s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/firebase.google.com\/products\/cloud-messaging\">Firebase Cloud Messaging<\/a> (FCM) service is a free and convenient way to distribute push notifications to mobile devices. It works with iOS, Android, and web targets, abstracting away the differences between platforms. You send your payload once to Firebase\u2019s API and get real-time delivery to all your users.<\/p>\n<p>In this article, we\u2019ll show how to use Firebase to send push notifications from your server-side PHP code. We\u2019re using the third-party <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/sngrl\/php-firebase-cloud-messaging\"><code>php-firebase-cloud-messaging<\/code><\/a> (PHP-FCM) library to further simplify the Firebase integration.<\/p>\n<h2 id=\"outlining-the-architecture\"><span class=\"ez-toc-section\" id=\"Outlining_the_Architecture\"><\/span>Outlining the Architecture<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Successfully sending a push notification requires several components to be working together. First you need an active Firebase account with a project that\u2019s got FCM enabled. We\u2019ll set this up in the following steps. You\u2019ll be issued a server key that your PHP backend must include with its Firebase requests.<\/p>\n<p>You\u2019ll also need an <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a> that uses the Firebase SDK to produce a client registration token. This token should be sent to your PHP backend. Persist it in your database alongside information identifying the client, such as its logged-in user ID within your application.<\/p>\n<p>As this article focuses on the backend integration, we\u2019ll assume you\u2019ve already got a Firebase client app that subscribes to notifications and retrieves a registration token. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/android\/client#sample-register\">You can follow the documentation<\/a> to create a basic Android app if you need a sample project. Within your client-side code, send the Firebase token to an API endpoint you\u2019ll create in your PHP service.<\/p>\n<p>Once you\u2019ve got some client tokens available on your server, you can send push notifications by making HTTP requests to the FCM API. Firebase will <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>te with the individual notification delivery platforms, pushing your alert to the specified devices. FCM internally maps each client token to the correct platform, such as Google Play Services for Android and Apple Push Notification Service (APNS) for iOS.<\/p>\n<h2 id=\"creating-your-firebase-project\"><span class=\"ez-toc-section\" id=\"Creating_Your_Firebase_Project\"><\/span>Creating Your Firebase Project<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15949\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/72699066.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"image of the Firebase console homepage\" width=\"1268\" height=\"648\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Head to the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/console.firebase.com\">Firebase Console<\/a>, login, and click \u201cAdd project\u201d to start setting up your integration. Give your project a name and click through the initial configuration prompts. Click the settings cog in the top-left when you reach the dashboard. Choose \u201cProject settings\u201d from the menu that appears.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15948\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/ad23f5de.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"image of setting up FCM in the Firebase console\" width=\"1268\" height=\"648\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Head to the \u201cCloud Messaging\u201d tab and note down your Server Key. Your PHP service will use this credential to send notifications to the Firebase API.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15947\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/61132f1f.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"image of the Firebase console homepage\" width=\"1268\" height=\"648\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You need to register your mobile applications within the Firebase console. Back on the homepage, use the \u201cAdd an app\u201d buttons to add your iOS and Android components. Follow the setup wizard to provide your app\u2019s data and download its Firebase config file. This should be referenced when you initialize Firebase in your client-side code.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-15946\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/1766a8c0.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"image of setting up FCM in the Firebase console\" width=\"1268\" height=\"648\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you\u2019re building an iOS app, you need to manually link your APNS key to Firebase. Click the settings cog in the top-left, choose \u201cProject settings,\u201d and navigate back to \u201cCloud Messaging.\u201d An \u201cApple apps\u201d section will appear when you\u2019ve got an iOS component in your project. Add an <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.apple.com\/documentation\/usernotifications\/setting_up_a_remote_notification_server\/establishing_a_token-based_connection_to_apns\">APNS key<\/a> or certificate from your Apple Developer Account to complete the integration. This allows FCM to send notifications to APNS on your behalf.<\/p>\n<h2 id=\"preparing-your-php-application\"><span class=\"ez-toc-section\" id=\"Preparing_Your_PHP_Application\"><\/span>Preparing Your PHP Application<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Begin your PHP project by adding the PHP-FCM library using Composer:<\/p>\n<pre>composer require sngrl\/php-firebase-cloud-messaging<\/pre>\n<p>Within your code, create an instance of PHP-FCM\u2019s <code>Client<\/code> class:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">use<\/span> sngrlPhpFirebaseCloudMessagingClientClient<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"re0\">$client<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Client<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$client<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setApiKey<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"FCM-SERVER-KEY\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$client<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">injectGuzzleHttpClient<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> GuzzleHttpClient<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Pass the <code>setApiKey()<\/code> method the Server Key you copied from your Firebase API console. In a real application, this should be stored securely and treated as a confidential secret.<\/p>\n<p>PHP-FCM relies on an injected <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/packagist.org\/packages\/guzzlehttp\/guzzle\">Guzzle<\/a> instance to make its HTTP requests. Guzzle is automatically included as a dependency so you don\u2019t need to manually install it. We\u2019re constructing a new Guzzle client in the example above; you could reuse an existing instance if you\u2019ve already got Guzzle in your application.<\/p>\n<p>The FCM <code>Client<\/code> instance is now ready to send notifications to your FCM account.<\/p>\n<h2 id=\"registering-client-tokens\"><span class=\"ez-toc-section\" id=\"Registering_Client_Tokens\"><\/span>Registering Client Tokens<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Notifications are distributed to client tokens that represent your users\u2019 devices. As explained above, you\u2019ll need to expose an API endpoint in your application that lets your client apps send their FCM token after the user logs in.<\/p>\n<p>Here\u2019s a basic example of what this could look like:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$token<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$_POST<\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"fcmToken\"<\/span><span class=\"br0\">]<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$userId<\/span> <span class=\"sy0\">=<\/span> <span class=\"br0\">(<\/span><span class=\"br0\">(<\/span>int<span class=\"br0\">)<\/span> <span class=\"re0\">$_POST<\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"userId\"<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"co4\">\/**\n * Call a function which persists a user\/token \n * association to your database\n *\/<\/span>\nsaveUserFcmToken<span class=\"br0\">(<\/span><span class=\"re0\">$userId<\/span><span class=\"sy0\">,<\/span> <span class=\"re0\">$token<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>To send a push notification to every registered device, select all the tokens in your data store. You can send to a specific user by retrieving the tokens associated with their ID. This would display the notification on all the devices they\u2019ve logged into, which is usually the intended behavior.<\/p>\n<h2 id=\"sending-notifications\"><span class=\"ez-toc-section\" id=\"Sending_Notifications\"><\/span>Sending Notifications<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>PHP-FCM abstracts each notification delivery into a <code>Message<\/code> object. This wraps a <code>Notification<\/code> \u2013 containing the text that\u2019s shown to the user \u2013 and any delivery options which you supply.<\/p>\n<p>Prepare your <code>Notification<\/code> first:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">use<\/span> sngrlPhpFirebaseCloudMessagingClientNotification<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"re0\">$notification<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Notification<span class=\"br0\">(<\/span>\n    <span class=\"st0\">\"Notification Title\"<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"The longer text of the notification, displayed below the title.\"<\/span>\n<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Next create a <code>Message<\/code> to represent the notification delivery:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">use<\/span> sngrlPhpFirebaseCloudMessagingClientMessage<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Message<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setNotification<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$notification<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Before sending your message, add one or more recipients. The <code>addRecipient()<\/code> method takes a <code>Device<\/code> instance; this class needs one of your FCM client tokens as its constructor parameter:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">use<\/span> sngrlPhpFirebaseCloudMessagingClientRecipientDevice<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">addReceipient<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> Device<span class=\"br0\">(<\/span><span class=\"st0\">\"FCM-CLIENT-TOKEN-USER-1\"<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">addReceipient<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> Device<span class=\"br0\">(<\/span><span class=\"st0\">\"FCM-CLIENT-TOKEN-USER-2\"<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Now you\u2019re ready to send the message using the <code>Client<\/code> created earlier:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$client<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">send<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$message<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The notification will be delivered to the devices you added as recipients.<\/p>\n<p>Here\u2019s a complete example that wraps the notification handling code into a convenient function:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"kw2\">use<\/span> sngrlPhpFirebaseCloudMessagingClientClient<span class=\"sy0\">;<\/span>\n<span class=\"kw2\">use<\/span> sngrlPhpFirebaseCloudMessagingClientMessage<span class=\"sy0\">;<\/span>\n<span class=\"kw2\">use<\/span> sngrlPhpFirebaseCloudMessagingClientNotification<span class=\"sy0\">;<\/span>\n<span class=\"kw2\">use<\/span> sngrlPhpFirebaseCloudMessagingClientRecipientDevice<span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"re0\">$client<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Client<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$client<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setApiKey<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"FCM-SERVER-KEY\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$client<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">injectGuzzleHttpClient<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> GuzzleHttpClient<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"kw2\">function<\/span> sendNotification<span class=\"br0\">(<\/span>\n    Client <span class=\"re0\">$client<\/span><span class=\"sy0\">,<\/span>\n    string <span class=\"re0\">$title<\/span><span class=\"sy0\">,<\/span>\n    string <span class=\"re0\">$body<\/span><span class=\"sy0\">,<\/span>\n    string <span class=\"sy0\">...<\/span><span class=\"re0\">$clientTokens<\/span><span class=\"br0\">)<\/span> <span class=\"sy0\">:<\/span> void <span class=\"br0\">{<\/span>\n\u00a0\n    <span class=\"re0\">$message<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Message<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setNotification<\/span><span class=\"br0\">(<\/span>\n        <span class=\"kw2\">new<\/span> Notification<span class=\"br0\">(<\/span>\n            <span class=\"re0\">$title<\/span><span class=\"sy0\">,<\/span>\n            <span class=\"re0\">$body<\/span>\n        <span class=\"br0\">)<\/span>\n    <span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n    <span class=\"kw1\">foreach<\/span> <span class=\"br0\">(<\/span><span class=\"re0\">$clientTokens<\/span> <span class=\"kw1\">as<\/span> <span class=\"re0\">$clientToken<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        <span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">addRecipient<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> Device<span class=\"br0\">(<\/span><span class=\"re0\">$clientToken<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n\u00a0\n    <span class=\"re0\">$client<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">send<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$message<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"br0\">}<\/span>\n\u00a0\nsendNotification<span class=\"br0\">(<\/span><span class=\"re0\">$client<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"Hello World\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"Test Notification\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"FCM-CLIENT-TOKEN-1\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"handling-fcm-response-data\"><span class=\"ez-toc-section\" id=\"Handling_FCM_Response_Data\"><\/span>Handling FCM Response Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>Client::send()<\/code> method returns the HTTP response object for the notification request. You can inspect the JSON-encoded response data to determine whether your notifications were delivered successfully.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$message<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Message<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setNotification<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> Notification<span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">addReceipient<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> Device<span class=\"br0\">(<\/span><span class=\"st0\">\"FCM-CLIENT-TOKEN-USER-1\"<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">addReceipient<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> Device<span class=\"br0\">(<\/span><span class=\"st0\">\"FCM-CLIENT-TOKEN-USER-2\"<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"re0\">$response<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$client<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">send<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$message<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$responseData<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$response<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">json<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The response data array has a structure similar to the following:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"json\">\n<pre class=\"de1\">{&#13;\n    \"success\": 1,&#13;\n    \"failure\": 1,&#13;\n    \"results\": [&#13;\n        {&#13;\n            \"message_id\": 100&#13;\n        },&#13;\n        {&#13;\n            \"error\": \"InvalidRegistration\"&#13;\n        }&#13;\n    ]&#13;\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <code>results<\/code> array contains an object representing the delivery status of each of the devices you tried to send to. This will match the order of recipients added to the <code>Message<\/code> via the <code>addRecipient()<\/code> method. The sample JSON above indicates that only the first device received the notification. The second delivery failed so you should remove the device token from your database.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$recipients<\/span> <span class=\"sy0\">=<\/span> <span class=\"br0\">[<\/span>\n    <span class=\"st0\">\"FCM-CLIENT-TOKEN-USER-1\"<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"FCM-CLIENT-TOKEN-USER-2\"<\/span>\n<span class=\"br0\">]<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Message<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setNotification<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> Notification<span class=\"br0\">(<\/span><span class=\"st0\">\"Test\"<\/span><span class=\"sy0\">,<\/span> <span class=\"st0\">\"Test\"<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"kw1\">foreach<\/span> <span class=\"br0\">(<\/span><span class=\"re0\">$recipients<\/span> <span class=\"kw1\">as<\/span> <span class=\"re0\">$recipient<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n    <span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">addReceipient<\/span><span class=\"br0\">(<\/span><span class=\"kw2\">new<\/span> Device<span class=\"br0\">(<\/span><span class=\"re0\">$recipient<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"br0\">}<\/span>\n\u00a0\n<span class=\"re0\">$response<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$client<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">send<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$message<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$responseData<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$response<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">json<\/span><span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n\u00a0\n<span class=\"kw1\">foreach<\/span> <span class=\"br0\">(<\/span><span class=\"re0\">$responseData<\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"results\"<\/span><span class=\"br0\">]<\/span> <span class=\"kw1\">as<\/span> <span class=\"re0\">$i<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"re0\">$result<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n    <span class=\"kw1\">if<\/span> <span class=\"br0\">(<\/span><span class=\"kw3\">isset<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$result<\/span><span class=\"br0\">[<\/span><span class=\"st0\">\"error\"<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><span class=\"br0\">)<\/span> <span class=\"br0\">{<\/span>\n        deleteUserFcmToken<span class=\"br0\">(<\/span><span class=\"re0\">$recipients<\/span><span class=\"br0\">[<\/span><span class=\"re0\">$i<\/span><span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n    <span class=\"br0\">}<\/span>\n<span class=\"br0\">}<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"adding-arbitrary-data-to-notifications\"><span class=\"ez-toc-section\" id=\"Adding_Arbitrary_Data_to_Notifications\"><\/span>Adding Arbitrary Data to Notifications<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Messages can include arbitrary data which needs to be communicated to the client application:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$message<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Message<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setNotification<\/span><span class=\"br0\">(<\/span>\n    <span class=\"kw2\">new<\/span> Notification<span class=\"br0\">(<\/span>\n        <span class=\"st0\">\"Breaking <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/news\/\" data-internallinksmanager029f6b8e52c=\"2\" title=\"News\" target=\"_blank\" rel=\"noopener\">News<\/a>!\"<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"st0\">\"A breaking news story is available.\"<\/span>\n    <span class=\"br0\">)<\/span>\n<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setData<\/span><span class=\"br0\">(<\/span><span class=\"br0\">[<\/span>\n    <span class=\"st0\">\"uri\"<\/span> <span class=\"sy0\">=&gt;<\/span> <span class=\"st0\">\"\/news\/latest-stories\"<\/span>\n<span class=\"br0\">]<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Client-side code <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/android\/receive\">can access this data<\/a> to take different actions when a notification is received.<\/p>\n<h2 id=\"setting-message-priorities\"><span class=\"ez-toc-section\" id=\"Setting_Message_Priorities\"><\/span>Setting Message Priorities<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>FCM supports a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/concept-options#setting-the-priority-of-a-message\">message priority system<\/a> that lets you request an immediate delivery to the target device. When high priority mode is used, FCM attempts to wake-up sleeping Android devices to handle the notification, even if background activity is being suppressed.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"co1\">\/\/ Indicate a high-priority message<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setPriority<\/span><span class=\"br0\">(<\/span><span class=\"st0\">\"high\"<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>This attribute must be used carefully. Sending too many priority messages that don\u2019t result in user interactions will cause your deliveries to be deprioritized instead. The mechanism is intended for genuinely important payloads that need to break through a device\u2019s battery saving, network throttling, and background activity restrictions.<\/p>\n<p>iOS handles priorities differently. You\u2019ll get an error if you try to send a <code>high<\/code> priority message to an iOS device. You can use the values <code>normal<\/code> or <code>5<\/code>, the latter indicating a high priority delivery is preferred.<\/p>\n<h2 id=\"time-to-live\"><span class=\"ez-toc-section\" id=\"Time_to_Live\"><\/span>Time to Live<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A <code>Message<\/code> instance\u2019s Time to Live (TTL) determines how long it remains relevant for. FCM won\u2019t always be able to deliver notifications in a timely manner. The target device could be offline or in a battery saving state. FCM will keep trying to deliver the notification but this isn\u2019t always desirable behavior. Some notifications such as expiration reminders might be irrelevant to the user by the time they receive them.<\/p>\n<p>Use the <code>setTimeToLive()<\/code> method to define the lifespan of your messages. FCM will stop trying to deliver them after the TTL has expired.<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$message<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Message<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setNotification<\/span><span class=\"br0\">(<\/span>\n    <span class=\"kw2\">new<\/span> Notification<span class=\"br0\">(<\/span>\n        <span class=\"st0\">\"Server rotation scheduled for 12pm\"<\/span><span class=\"sy0\">,<\/span>\n        <span class=\"st0\">\"Cancel within the next 10 minutes.\"<\/span>\n    <span class=\"br0\">)<\/span>\n<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setTimeToLive<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">600<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h2 id=\"badges-on-ios\"><span class=\"ez-toc-section\" id=\"Badges_on_iOS\"><\/span>Badges on iOS<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>iOS uses red badges on home screen icons to indicate the number of unread notifications available within the app. You can change your app\u2019s numeric badge with the <code>setBadge()<\/code> method on a <code>Notification<\/code> object:<\/p>\n<div class=\"wp-geshi-highlight-wrap5\">\n<div class=\"wp-geshi-highlight-wrap4\">\n<div class=\"wp-geshi-highlight-wrap3\">\n<div class=\"wp-geshi-highlight-wrap2\">\n<div class=\"wp-geshi-highlight-wrap\">\n<div class=\"wp-geshi-highlight\">\n<div class=\"php\">\n<pre class=\"de1\"><span class=\"re0\">$message<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Message<span class=\"br0\">(<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$notification<\/span> <span class=\"sy0\">=<\/span> <span class=\"kw2\">new<\/span> Notification<span class=\"br0\">(<\/span>\n    <span class=\"st0\">\"Server rotation scheduled for 12pm\"<\/span><span class=\"sy0\">,<\/span>\n    <span class=\"st0\">\"Cancel within the next 10 minutes.\"<\/span>\n<span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$notification<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setBadge<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setNotification<\/span><span class=\"br0\">(<\/span><span class=\"re0\">$notification<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span>\n<span class=\"re0\">$message<\/span> <span class=\"sy0\">-&gt;<\/span> <span class=\"me1\">setTimeToLive<\/span><span class=\"br0\">(<\/span><span class=\"nu0\">600<\/span><span class=\"br0\">)<\/span><span class=\"sy0\">;<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/sngrl\/php-firebase-cloud-messaging\/blob\/master\/src\/Notification.php\"><code>Notification<\/code> class<\/a> has methods for other platform-specific behaviors too. You can change the notification\u2019s icon on Android devices (<code>setIcon()<\/code>), assign a sound to play (<code>setSound()<\/code>), and use tags (<code>setTag()<\/code>) to control whether previous notifications are replaced by the new delivery. These properties and the quirks of their platform implementations are described in the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\/http-server-ref#notification-payload-support\">FCM API documentation<\/a>.<\/p>\n<h2 id=\"conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>FCM is an ideal way to start sending push notifications to mobile devices from a PHP backend. It handles interactions with platform-specific push implementations such as APNS and Google Play Services, reducing the amount of code you need to write.<\/p>\n<p>The PHP Firebase Cloud Messaging library wraps the FCM API in convenient PHP classes and methods. For more advanced control, you can call the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/firebase.google.com\/docs\/cloud-messaging\">FCM API<\/a> directly via a PHP HTTP library such as <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/packagist.org\/packages\/guzzlehttp\/guzzle\">Guzzle<\/a>. You might have to take this approach if you need to use notification options that aren\u2019t exposed by PHP-FCM.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/devops\/how-to-send-mobile-push-notifications-with-php-and-firebase\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How to Send Mobile Push Notifications With PHP and Firebase&#8221; McLittle Stock\/Shutterstock.com Google\u2019s Firebase Cloud Messaging (FCM) service is a free and convenient way to distribute push notifications to mobile devices. It works with iOS, Android, and web targets, abstracting away the differences between platforms. You send your payload once to Firebase\u2019s API and get&#8230;<\/p>\n","protected":false},"author":1,"featured_media":468143,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/2aecdc0c.jpg?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-468142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/468142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=468142"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/468142\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/468143"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=468142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=468142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=468142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}