Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Push Notification for Android using Laravel 4.x

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 612
    Comment on it

    There are few steps which we have to follow to implement Push Notification.

    Step 1: First we will write the api to get token and device type . Here is the controller code of push notification.

    public function pushNotification() {
            $validator = Validator::make(Request::all(), [
                        'userId'=>'required',
                        'accessToken'=>'required',
                        'senderId'=>'required',
                        'token'=>'required',
                        'deviceType'=>'required'
            ]);
            if ($validator->fails()) {
                #display error if validation fails      
    
                $this->status = 'Validation fails';
                $this->message = 'arguments missing';
            } else {
                $userId =Request::get('userId');
                $accessToken=Request::get('accessToken');
                $senderId=Request::get('senderId');
                $token=Request::get('token');
                $deviceType=Request::get('deviceType');
                $checkUserStatus =User::where('id','=',$userId)->first();
                if(!empty($checkUserStatus)){
                    if( $deviceType=='Android'){
                        $setMessage='You have a new activity on KarmaCircles.';
                        $pushNotificationStatus=NotificationHelper::androidPushNotification($token,$setMessage);
                        $this->status = $pushNotificationStatus;
                        $this->message = 'Push Notification Message';
                    }else{
                        $pushNotificationStatus=NotificationHelper::iphonePushNotification($token,$setMessage);
                        $this->status = $pushNotificationStatus;
                        $this->message = 'Push Notification Message';
                    }
                }else{
                    $this->status = 'Failure';
                    $this->message = 'There is no such user available';
                }
            }
            return Response::json(array(
                'status'=>$this->status,
                'message'=>$this->message
            ));
           }
    

    Now we use NotificationHelper to perform notification.

    Step 2:

    In NotificationHelper.php the code is:

    #======================================================================================
        /**
         * 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 static function androidPushNotification ($deviceToken, $setMessage = "Hello Android Rohit!"){
            // Set parameters:
            $apiKey = 'AIzaSyBgiOAHXJRTCbehKUprmqFS89ZuHZ0vTJQ'; // API key from Google Developer Console
            $gcmUrl = 'https://android.googleapis.com/gcm/send';
    
            // Get the device token (fetch from database for example):
            $regid  = $deviceToken;
    
            // Set message:
            $message = $setMessage;
    
            // Send message:
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $gcmUrl);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER,
                array(
                    'Authorization: key=' . $apiKey,
                    'Content-Type: application/json'
                )
            );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
                    array(
                        'registration_ids' => array($regid),
                        'data' => array(
                            'message' => $message
                        )
                    ),
                    JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
                )
            );
    
            $result = curl_exec($ch);
            Log::info("curl result ::". var_;export($result,TRUE) );
    
            if ($result === FALSE){
                $status = 'failed: ' . curl_error($ch);
            }
            else
            {
                echo $status = 'success';
            }
    
            // Close connection
            curl_close($ch);
            Log::info("returning  ::". $status );
    
            return $status;
        }
    

    By this way we can implementing push notification using laravel 4.x

 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: