{"id":178,"date":"2020-12-25T14:02:26","date_gmt":"2020-12-25T14:02:26","guid":{"rendered":"https:\/\/barahasoft.com.np\/blog\/?post_type=codings&#038;p=178"},"modified":"2023-05-07T13:31:24","modified_gmt":"2023-05-07T13:31:24","slug":"code-to-send-push-notification-php-to-android","status":"publish","type":"codings","link":"https:\/\/barahasoft.com.np\/blog\/codings\/code-to-send-push-notification-php-to-android\/","title":{"rendered":"Code to send Push Notification (PHP to Android)"},"content":{"rendered":"\n<p>A push notification is a message that is sent from a server to a mobile device through a push notification service. The purpose of a push notification is to alert the user of a mobile app to a specific event or action that requires their attention, even when the app is not actively in use.<\/p>\n\n\n\n<p>Push notifications can take many forms, such as text messages, images, sounds, and badges, and they can contain various types of information, including news updates, reminders, promotions, and other types of personalized content.<\/p>\n\n\n\n<p>Push notifications are a powerful tool for engaging with mobile app users and can help increase user retention, drive app usage, and improve user experience. However, it&#8217;s important to use push notifications judiciously and ensure that they provide value to the user, as too many notifications or irrelevant notifications can lead to users disabling push notifications for your app or even uninstalling it altogether.<\/p>\n\n\n\n<p><br><br>We can easily send data payload via push notification. For this we need to have legacy server key.<\/p>\n\n\n\n<p>This function can be used to send push notification to android client.<\/p>\n\n\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\nfunction sendPushNotification($registrationIDs = null, $data) {\n\n    $encodedData = json_encode($data);\n    if (!$encodedData) {return;}\n\n    \/\/$toKey = 'registration_ids';\n    $toKey = 'to';\n    \/\/$toValue = $registrationIDs;\/\/[&quot;id1&quot;,&quot;id2&quot;]\n    $toValue = '\/topics\/all';\n    $fields = array(\n        $to =&gt; $toValue,\n        'data' =&gt; array('data' =&gt; $encodedData),\n    );\n    $encodedFields = json_encode($fields, JSON_UNESCAPED_UNICODE);\n    \/\/ legacy key from https:\/\/console.firebase.google.com\/project\/mproject\/settings\/cloudmessaging\/android:package_name\n    $serverKey =oCQ:APA91bEAo8eK3tox4g_qqocGy7ddvEy60rtqoUHqczOguq3YtZGHO2msvDWSKwjAY49FdtSTQztnAgdls1Ho4CQVK_gWIC7cmF;\n    $path_to_firebase_cm = 'https:\/\/fcm.googleapis.com\/fcm\/send';\n\n    $headers = array(\n        'Authorization:key=' . $serverKey,\n        'Content-Type:application\/json',\n    );\n\n    $ch = curl_init();\n    curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm);\n    curl_setopt($ch, CURLOPT_POST, true);\n    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);\n    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);\n    curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedFields);\n\n    $result = curl_exec($ch);\n    curl_close($ch);\n    return $result;\n}\n<\/pre>","protected":false},"featured_media":0,"parent":0,"template":"","tags":[10,66,67],"language":[37],"ctype":[40],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Code to send Push Notification (PHP to Android) - BS Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/barahasoft.com.np\/blog\/codings\/code-to-send-push-notification-php-to-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Code to send Push Notification (PHP to Android) - BS Blog\" \/>\n<meta property=\"og:description\" content=\"A push notification is a message that is sent from a server to a mobile device through a push notification service. The purpose of a push notification is to alert the user of a mobile app to a specific event or action that requires their attention, even when the app is not actively in use. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/barahasoft.com.np\/blog\/codings\/code-to-send-push-notification-php-to-android\/\" \/>\n<meta property=\"og:site_name\" content=\"BS Blog\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-07T13:31:24+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/barahasoft.com.np\/blog\/#website\",\"url\":\"https:\/\/barahasoft.com.np\/blog\/\",\"name\":\"BS Blog\",\"description\":\"Baraha Soft Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/barahasoft.com.np\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/barahasoft.com.np\/blog\/codings\/code-to-send-push-notification-php-to-android\/#webpage\",\"url\":\"https:\/\/barahasoft.com.np\/blog\/codings\/code-to-send-push-notification-php-to-android\/\",\"name\":\"Code to send Push Notification (PHP to Android) - BS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/barahasoft.com.np\/blog\/#website\"},\"datePublished\":\"2020-12-25T14:02:26+00:00\",\"dateModified\":\"2023-05-07T13:31:24+00:00\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/barahasoft.com.np\/blog\/codings\/code-to-send-push-notification-php-to-android\/\"]}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/codings\/178"}],"collection":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/codings"}],"about":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/types\/codings"}],"wp:attachment":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/tags?post=178"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/language?post=178"},{"taxonomy":"ctype","embeddable":true,"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/ctype?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}