{"id":41010,"date":"2016-02-11T18:27:55","date_gmt":"2016-02-11T18:27:55","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=41010"},"modified":"2016-02-11T18:27:55","modified_gmt":"2016-02-11T18:27:55","slug":"create-push-notifications-from-your-server","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/","title":{"rendered":"Send push notifications from your server through GCM"},"content":{"rendered":"<p>In this age of smartphones, the android app plays a great role for your e-commerce store. In order to notify the customers about the latest featured products and categories, the notifications seem\u00a0to be a required option. The notifications in the\u00a0android app can send in two ways: pull and push.<\/p>\n<p>&nbsp;<\/p>\n<p>In pull notification, the app hits the server and gathers the data while in push notification, the server sends the data by its own. Although, the pull notification is very easy to implement but pulling consumes a lot of battery and reduces the battery life as it requires constant connection with the server.<\/p>\n<p>&nbsp;<\/p>\n<p>Due to some of these disadvantages, the push notification comes into light. The push notification can be easily implemented using GCM(Google Cloud Messaging). The GCM is a free service that allows developers to send data from third party servers to their applications running on android devices.<\/p>\n<p>&nbsp;<\/p>\n<p>In order to send the notification from the server, we need to generate a GCM authorization key (needed on the server side for sending the messages to google cloud), sender ID\/ project ID (required in the app to getting the token from GCM server and register it with your server.<\/p>\n<p>For these, you have to register your application on the google server<\/p>\n<p>https:\/\/developers.google.com\/cloud-messaging\/android\/client<\/p>\n<p>&nbsp;<\/p>\n<p>You can send the notification to all the apps using the token &#8216;global&#8217;.<\/p>\n<p>The code underneath will describe\u00a0other things very well.<\/p>\n<pre class=\"brush:php\">\/\/ message to be send to the app\r\n$message = array(\t\t\t\t\t\r\n\t'title'\t\t=&gt; 'Demo Notification',\r\n\t'message'\t=&gt; 'Demo Message Demo Message'\r\n);\r\n\r\n\/\/ Set POST variables\r\n$url = 'https:\/\/android.googleapis.com\/gcm\/send';\r\n\r\n$topic = 'global';\/\/ global topic will send notification to all the apps with the same authorization id\r\n\r\n\/\/ the data will be send in the array below\r\n$fields = array(\r\n\t'to'\t\t\t=&gt; \"\/topics\/\".$topic,\r\n\t'data'\t\t\t=&gt; $message,\r\n\t'time_to_live'\t        =&gt; 30,\r\n\t'delay_while_idle'\t=&gt; true\r\n);\r\n\r\n$auth_key = 'AIzaSyDXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; \/\/ authorization key of the app\r\n\r\n$headers = array(\r\n\t'Authorization: key='.$auth_key,\r\n\t'Content-Type: application\/json'\r\n);\r\n\r\n\/\/ Open connection\r\n$ch = curl_init();\r\n\r\n\/\/ Set the url, number of POST vars, POST data\r\ncurl_setopt($ch, CURLOPT_URL, $url);\r\n\r\ncurl_setopt($ch, CURLOPT_POST, true);\r\ncurl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\r\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\r\n\r\n\/\/ Disabling SSL Certificate support temporarly\r\ncurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);\r\n\r\ncurl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));\r\n\r\n\/\/ Execute post\r\n$result = curl_exec($ch);\r\nif ($result === FALSE) {\r\n\tdie('Curl failed: ' . curl_error($ch));\r\n}\r\n\r\n\/\/ Close connection\r\ncurl_close($ch);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this age of smartphones, the android app plays a great role for your e-commerce store. In order to notify the customers about the latest featured products and categories, the notifications seem\u00a0to be a required option. The notifications in the\u00a0android app can send in two ways: pull and push. &nbsp; In pull notification, the app <a href=\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":70,"featured_media":43102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[271],"tags":[221,1865,2071,2722],"class_list":["post-41010","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-2","tag-android","tag-mobikul","tag-opencart","tag-push-notification"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Send push notifications from your server through GCM - Webkul 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:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Send push notifications from your server through GCM - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"In this age of smartphones, the android app plays a great role for your e-commerce store. In order to notify the customers about the latest featured products and categories, the notifications seem\u00a0to be a required option. The notifications in the\u00a0android app can send in two ways: pull and push. &nbsp; In pull notification, the app [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-02-11T18:27:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Vikhyat Sharma\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vikhyat Sharma\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/\"},\"author\":{\"name\":\"Vikhyat Sharma\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/af7160d2546c64a1856ab1b5ce77d9b0\"},\"headline\":\"Send push notifications from your server through GCM\",\"datePublished\":\"2016-02-11T18:27:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/\"},\"wordCount\":254,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"keywords\":[\"android\",\"Mobikul\",\"opencart\",\"push notification\"],\"articleSection\":[\"Android\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/\",\"url\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/\",\"name\":\"Send push notifications from your server through GCM - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"datePublished\":\"2016-02-11T18:27:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"width\":825,\"height\":260},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Send push notifications from your server through GCM\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/af7160d2546c64a1856ab1b5ce77d9b0\",\"name\":\"Vikhyat Sharma\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7c9c700cc2d7120c9faf1ab3392b4e533808ba197f58c0441d6caecc68179e12?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7c9c700cc2d7120c9faf1ab3392b4e533808ba197f58c0441d6caecc68179e12?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Vikhyat Sharma\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/vikhyat-sharma83\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Send push notifications from your server through GCM - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/","og_locale":"en_US","og_type":"article","og_title":"Send push notifications from your server through GCM - Webkul Blog","og_description":"In this age of smartphones, the android app plays a great role for your e-commerce store. In order to notify the customers about the latest featured products and categories, the notifications seem\u00a0to be a required option. The notifications in the\u00a0android app can send in two ways: pull and push. &nbsp; In pull notification, the app [...]","og_url":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-02-11T18:27:55+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","type":"image\/png"}],"author":"Vikhyat Sharma","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Vikhyat Sharma","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/"},"author":{"name":"Vikhyat Sharma","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/af7160d2546c64a1856ab1b5ce77d9b0"},"headline":"Send push notifications from your server through GCM","datePublished":"2016-02-11T18:27:55+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/"},"wordCount":254,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","keywords":["android","Mobikul","opencart","push notification"],"articleSection":["Android"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/","url":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/","name":"Send push notifications from your server through GCM - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","datePublished":"2016-02-11T18:27:55+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","width":825,"height":260},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/create-push-notifications-from-your-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Send push notifications from your server through GCM"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/af7160d2546c64a1856ab1b5ce77d9b0","name":"Vikhyat Sharma","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7c9c700cc2d7120c9faf1ab3392b4e533808ba197f58c0441d6caecc68179e12?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7c9c700cc2d7120c9faf1ab3392b4e533808ba197f58c0441d6caecc68179e12?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Vikhyat Sharma"},"url":"https:\/\/webkul.com\/blog\/author\/vikhyat-sharma83\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/41010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/70"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=41010"}],"version-history":[{"count":6,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/41010\/revisions"}],"predecessor-version":[{"id":41023,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/41010\/revisions\/41023"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/43102"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=41010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=41010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=41010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}