Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Push Notification - Android using GCM

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 263
    Comment on it

    This post will help you to send push notification to Android devices using server side scripting language called php.
    click below Link to get Google cloud messaging api-

    https://console.developers.google.com/project

    Here is a code to send push notification

    // Google Cloud Messaging API Access Key
    define("GOOGLE_CLOUD_MESSAGING_API_KEY", "AIzaSyBiGzlss534iVEQ2ddddpkm931Tef7OFNKldlddl0PkJI5nFm08");
    
    class PushNotificationComponent
    {
        /**
         * Android Push Notifications using Google Cloud Messaging (GCM)
         * Send push notification from web server to registered android devices.
         *
         * @param   array   data
         * @param   string  message
         * @param   array   custom_fields
         * @return  string  status
         */
        public function androidPushNotification( $data, $message, $custom_fields = array() )
        {
            $data       =   isset($data) && is_array($data) ? $data : array();
            $message    =   isset($message) && !empty($message) ? trim($message) : '';

    $device_tokens = array(); $c = count($data); for( $i=0; $i<$c; $i++ ) { // Get Android Device Token Without Any Spaces $deviceToken = isset($data[$i]['device_token']) && !empty($data[$i]['device_token']) ? trim($data[$i]['device_token']) : ''; $deviceToken = str_replace(' ', '', $deviceToken); // Check if device token not empty if( !empty($deviceToken) ) $device_tokens[] = $deviceToken; } if( count($device_tokens)==0 ) return 'empty device token.'; if( empty($message) ) return 'empty message.'; // Prepared the bundle $body = array ( 'message' => $message, 'vibrate' => 1, 'sound' => 1, 'delay_while_idle' => 1, 'custom_fields' => $custom_fields ); // Set POST variables $fields = array ( 'registration_ids' => $device_tokens, 'data' => $body ); // Set Request Header $headers = array ( 'Authorization: key='.GOOGLE_CLOUD_MESSAGING_API_KEY, 'Content-Type: application/json' ); // Open connection $ch = curl_init(); // Set the url curl_setopt( $ch,CURLOPT_URL,'https://android.googleapis.com/gcm/send' ); curl_setopt( $ch,CURLOPT_POST,true ); curl_setopt( $ch,CURLOPT_HTTPHEADER,$headers ); curl_setopt( $ch,CURLOPT_RETURNTRANSFER,true ); // Disabling SSL Certificate support temporarly curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER,false ); curl_setopt( $ch,CURLOPT_POSTFIELDS,json_encode($fields) ); // Execute curl $result = curl_exec($ch); if ($result === FALSE) $status = 'Curl failed: ' . curl_error($ch); else $status = 'success'; // Close connection curl_close($ch); return $status; } }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: