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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 485
    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:

      /**
         * Apple Push Notification Services (APNS)
         *
         * @param   string  device_token
         * @param   string  message
         * @return  string  status  success|failure
         */
         public static function iphonePushNotification($device_token, $msg = null) {
    
    
            // Set private key's passphrase_local cert file path
            $passphrase = PASSPHRASE;
            $local_cert = LOCAL_CERT_PATH;
            $streamContext = stream_context_create();
            stream_context_set_option($streamContext, 'ssl', 'local_cert', $local_cert);
            stream_context_set_option($streamContext, 'ssl', 'passphrase', $passphrase);
            $fp = stream_socket&_client('ssl://gateway.sandbox.push.apple.com:2195', $error, $errorString, 20, STREAM_CLIENT_CONNECT, $streamContext);
            // Open a connection to the APNS server
    
            if (!$fp) {
    
                $status = "failure";
    
            } else {
    
                    // iPhone Device Token Without Any Spaces
                    $device_token = str_replace(' ', '', $device_token);
    
                    // Set badge
                    $badge = 1;
    
                    // Check if device token not empty
                    if (!empty($device_token)) {
                        // Create the payload body
                        $body['aps'] = array('alert' => $msg, 'badge' => $badge, 'sound' => 'default');
    
                        // Encode the payload as JSON
                        $payload = json_encode($body);
                        try {
    
                            // Build the binary notification
                            $msg = chr(0) . pack('n', 32) . pack('H*', $device_token) . pack('n', strlen($payload)) . $payload;
    
                            // Send it to the server
                            $result = fwrite($fp, $msg, strlen($msg));
    
                            Log::info("ios result ::". var_export($result,TRUE) );
    
    
                        } catch(Exception $e) {
                            //echo "1. Exception: " . $e -> getMessage() . "<br />\n";
                            Log::info("ios error ::". $e -> getMessage() );
                        }
    
                    }
    
                $status = 'success';
            }
            fclose($fp);
    
            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: