<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PUSH notification Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<atom:link href="https://atcodex.com/tag/push-notification/feed/" rel="self" type="application/rss+xml" />
	<link>https://atcodex.com/tag/push-notification/</link>
	<description>Achieve financial success without sacrificing well-being! AtCodex provides actionable strategies for managing money, time, and personal growth—all in one place.</description>
	<lastBuildDate>Sat, 28 Aug 2021 17:15:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://atcodex.com/wp-content/uploads/2020/05/cropped-New-Project-1-32x32.png</url>
	<title>PUSH notification Archives - AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</title>
	<link>https://atcodex.com/tag/push-notification/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to send PUSH notification from the website using Google FCM and PHP in chunks</title>
		<link>https://atcodex.com/php/how-to-split-long-data-and-send-it-as-chunks-in-php-mysql/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Tue, 28 Jul 2020 18:34:53 +0000</pubDate>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[FCM]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PUSH notification]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=553</guid>

					<description><![CDATA[<p>We have already discussed How to send PUSH notification from the website using Google FCM and PHP you can check that post. But most common problems occur when we have &#8230; </p>
<p>The post <a href="https://atcodex.com/php/how-to-split-long-data-and-send-it-as-chunks-in-php-mysql/">How to send PUSH notification from the website using Google FCM and PHP in chunks</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>We have already discussed <a href="https://atcodex.com/php/how-to-send-push-notification-from-the-website-using-google-fcm-and-php/">How to send PUSH notification from the website using Google FCM and PHP</a> you can check that post.</p>



<p>But most common problems occur when we have to send push notification to the thousand of devices. FCM only allows the user to send 1k notification at a time. Then what if we have thousands of users. what if we have to send all of them an instant notification. if you try to send the notification to all the users at once then it won&#8217;t work. </p>



<p>In that case, we have to send the data in the chunk to the FCM server. </p>



<p>Let&#8217;s suppose we have 10k users and we have to send them a notification, then we will divide 10k users into 10 small array containing 1k users. </p>



<p> In this tutorial, we will explain how you can <strong>send PUSH notification from the website using Google FCM and PHP in chunks</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">  //post data
$notification_text = $_POST['notification_text'];
$title = $_POST['title'];

//you can insert post data into db for record here

$get_device_id = $object->get_device_id(); // get device id from database, i am fetching data using PDO you can use Msqli also(we did not mention the class file here) 

$loop =  count(array_chunk($get_device_id,1000));  // count array chunk 1000
$arrayChunk = array_chunk($get_device_id,1000);   // devide array in 1000 chunk

$device_id = array();
for($i=0; $i&lt;$loop;$i++)
{
    foreach($arrayChunk[$i] as $all_device_id)
    {       
           
                $device_id[] =  $all_device_id->firebase_id;
    }

    $body=$notification_text;
    $customData=$url;

    $json_data = 
        [
            "registration_ids" => $device_id,
            "notification" => [
                "body" => $body,
                "title" => $title,
            ],
            "data" => [
                "extra" => $customData
            ]
        ];

 $data = json_encode($json_data); 

$url = 'https://fcm.googleapis.com/fcm/send';
//api_key in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'AAAAJqUz63o:APA91bH9dBz6RihgEdvKhi5dXJyqEl234234dfdf25--0ylOoQswedsseWgpbR-AIkn7TVP7nP5kpPFxXJTRaAhv-d_It2vC9ejNhHeXOylo8oUm10yHm5h64yzm_PlsPuR';
//header with content_type api key
$headers = array(
    'Content-Type:application/json',
    'Authorization:key='.$server_key
);
// CURL request to route notification to FCM connection server (provided by Google)
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Oops! FCM Send Error: ' . curl_error($ch));
    }
    curl_close($ch);
	unset($device_id); // unset the array value 
}</pre>



<p></p>



<p>In the above code, you will be able to send data in chunks into the FCM server and can send thousand of notification at once.</p>



<p>Sending data in the chunk is useful in many scenarios. Here just because FCM has some limitations,  we are sending the data in chunk. </p>



<p>For example, if we are trying to upload and insert all data at once then definitely that process is going to consume lots of memory of server which may sometime down the website, So it&#8217;s always recommend doing the bulk data activity in chunks.</p>



<p></p>
<p>The post <a href="https://atcodex.com/php/how-to-split-long-data-and-send-it-as-chunks-in-php-mysql/">How to send PUSH notification from the website using Google FCM and PHP in chunks</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>How to send PUSH notification from the website using Google FCM and PHP</title>
		<link>https://atcodex.com/php/how-to-send-push-notification-from-the-website-using-google-fcm-and-php/</link>
		
		<dc:creator><![CDATA[atcodex]]></dc:creator>
		<pubDate>Wed, 08 Jul 2020 07:32:28 +0000</pubDate>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Google FCM]]></category>
		<category><![CDATA[PUSH notification]]></category>
		<guid isPermaLink="false">https://atcodex.com/?p=352</guid>

					<description><![CDATA[<p>In this article, we are going to discuss, how we can send push notification from the website using Firebase Cloud Messaging (Google FCM) and PHP. In this, We are only &#8230; </p>
<p>The post <a href="https://atcodex.com/php/how-to-send-push-notification-from-the-website-using-google-fcm-and-php/">How to send PUSH notification from the website using Google FCM and PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this article, we are going to discuss, how we can send push notification from the website using Firebase Cloud Messaging (Google FCM) and PHP.  In this, We are only going to discuss the PHP part.</p>



<p></p>



<h3 class="wp-block-heading">What actually is a Push Notification?</h3>



<p>A Push Notification is a message which is sent on the user&#8217;s smartphone. it is basically used to increase the engagement of users in-app. A push notification can be about anything whose main purpose is to let the user know about some events, offers, or anything, which reminds the user to open the app for that particular event or offer.  By doing so the company increases the user engagement in their app by luring them with different offers sent by push notification(message).</p>



<p></p>



<h3 class="wp-block-heading">Google Cloud Messaging</h3>



<p>Google Cloud Messaging (GCM) is a free service provided by Google to enable developers to send messages from google server to app.</p>



<h3 class="wp-block-heading">Google FCM</h3>



<p>Later on, Google acquired Firebase in 2014 and then announced Firebase cloud messaging (FCM) to send notification and messages on different mobile OS even on iOS. FCM has lots of rich features which forces  user to migrate from GCM to FCM. </p>



<p>Following are the steps you can configure PUSH notification from the website using Google FCM and PHP.</p>



<p></p>



<h4 class="wp-block-heading">1 &#8211; Login/sign-up to your firebase account. https://console.firebase.google.com/</h4>



<p>Select the project from the list or make a new one if you do not have any project there, It just asks some basic details about the app and can easily be created.  If you are a backend developer then ask an android developer to create the same and provide you the necessary details.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1007" height="325" src="https://atcodex.com/wp-content/uploads/2020/07/1.png" alt="" class="wp-image-355" srcset="https://atcodex.com/wp-content/uploads/2020/07/1.png 1007w, https://atcodex.com/wp-content/uploads/2020/07/1-300x97.png 300w, https://atcodex.com/wp-content/uploads/2020/07/1-768x248.png 768w" sizes="(max-width: 1007px) 100vw, 1007px" /></figure>



<p></p>



<h4 class="wp-block-heading">2 &#8211; Click on the gear icon on the left top side and then click ‘project settings.’</h4>



<figure class="wp-block-image size-large"><img decoding="async" width="743" height="377" src="https://atcodex.com/wp-content/uploads/2020/07/2.png" alt="" class="wp-image-356" srcset="https://atcodex.com/wp-content/uploads/2020/07/2.png 743w, https://atcodex.com/wp-content/uploads/2020/07/2-300x152.png 300w" sizes="(max-width: 743px) 100vw, 743px" /></figure>



<p></p>



<h4 class="wp-block-heading">3  &#8211; Get the server key</h4>



<p>Server key used for authentication when we send requests from our website to GCM server to send messages on mobile devices. Without the server key, you can not send the message.</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1007" height="435" src="https://atcodex.com/wp-content/uploads/2020/07/3.png" alt="" class="wp-image-357" srcset="https://atcodex.com/wp-content/uploads/2020/07/3.png 1007w, https://atcodex.com/wp-content/uploads/2020/07/3-300x130.png 300w, https://atcodex.com/wp-content/uploads/2020/07/3-768x332.png 768w" sizes="(max-width: 1007px) 100vw, 1007px" /></figure>



<p></p>



<h4 class="wp-block-heading">4 &#8211; Create notification.php</h4>



<p>Create notification.php file and  put the below code in it with valid credentials.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php
  //post data
$notification_text = $_POST['notification_text'];
$title = $_POST['title'];

//you can insert post data into db for record

// if you want to send data to single device
$device_id ='fsdfsdfsdlj324j3l4j32l4jk2k4jldslkjflksdjfll23lk4j23';

// if you want to send data to multiple device 
$device_id = ['fsdfsdfsdlj324j3l4j32l4jk2k4jldslkjflksdjfll23lk4j23','fs423423dlj324j3l4j32l4jk2k4jldslkjflksdjfll23lk4j23'];

// You can also fetch the data from database and put in array using foreach
foreach($get_device_id as $all_device_id)
{       
   $device_id[] =  $all_device_id->$device_id; // when data comes as object from DB
}

// FCM Payload data format
    $json_data = 
        [
            "registration_ids" => $device_id,
            "notification" => [
                "body" => $notification_text,
                "title" => $title,
            ],
            "data" => [
                "extra" => $customData // if you want for send extra data
            ]
        ];
 $data = json_encode($json_data);   // encode data into json format

//FCM API end-point
$url = 'https://fcm.googleapis.com/fcm/send';

//api_key in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = 'AAAAJqUz63o:APA91bH9dBz6RihgEdvKhi5dXJyqEl234234dfdf25--0ylOoQswedsseWgpbR-AIkn7TVP7nP5kpPFxXJTRaAhv-d_It2vC9ejNhHeXOylo8oUm10yHm5h64yzm_PlsPuR';

//header with content_type server key
$headers = array(
    'Content-Type:application/json',
    'Authorization:key='.$server_key
);

//CURL request to route notification to FCM connection server (provided by Google)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if ($result === FALSE) {
    die('Oops! FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
?>

  
</pre>



<p>Hope it will help.</p>



<p>You can check <a href="https://atcodex.com/php/how-to-split-long-data-and-send-it-as-chunks-in-php-mysql/">How to send PUSH notification from the website using Google FCM and PHP in chunks</a>. it is helpful when you need to send PUSH notification to more than 1k users, because FCM has limitation to send only 1k PUSH notification at a time.</p>
<p>The post <a href="https://atcodex.com/php/how-to-send-push-notification-from-the-website-using-google-fcm-and-php/">How to send PUSH notification from the website using Google FCM and PHP</a> appeared first on <a href="https://atcodex.com">AtCodex: Empowering Your Financial Journey &amp; Personal Well-Being</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
