Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on send puch notification on iPhone in cakephp 2.0.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 738
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss on send push notification on iPhone in CakePHP 2.0.

    Push notification is basically used for sending messages on mobiles. It pops up on a mobile device.

    If you are app publishers and you want to send a push notification, Then you can send them at any time. If the user installs your app and login in an app then he/she can receive the push notification.

    Hence, you can say that push notification is providing convenience and value for the app users.

    There are 4 main things needed to send push notification

    1-Pem file=> Pem file is very important because without pem file you can not send any push notification on iPhone device.
     
    2-Device Token => Device token is very important for sending push notification because device token is a unique thing for all devices.
    3- Device Type => Device Type is also important parts because it indicate that on which device you want to send push notification (iPhone or iPad)
    4-Message => It includes what you what you want to send to the user in the push notification.

    You can see below code, how to send push notification on iPhone device.

    so firstly you have to create a function for sending push notification like below example

    <?php
        private function pushnotification() {
             $dtoken = "a9a9d431e214b92bf9ce112e39d0d9bd3805b6d3f59e191993699c7908a129f1"; 
             // here define device token 
             $notification = "notification message";//here define notification message
             $mobileos = "iPhone"; //here define device type on which you want to send notification
            if ($mobileos == "ipad" && $dtoken != '') {  //here check device type         
                  $return = $this->iphonePushNotification($dtoken, $notification );
                 // here call iphonePushNotification()
            }elseif ($mobileos == "iphone" && $dtoken != '') {
                  $return = $this->iphonePushNotification($dtoken, $notification);
        } 
    ?>

    There is a protected function iphonePushNotification() below which is access device token and notification message from above pushnotification() and send the notification message to the user device.

    <?php 
    
                protected function iphonePushNotification($dtoken, $notification) {
                     //here you can define  developer account and distribution accout      
                    // $apnsHost = 'gateway.sandbox.push.apple.com';
                    $apnsHost ='gateway.push.apple.com';
    
                   
                    $data["msg"] = $msg;
                    
                    $apnsPort = 2195;
                    $passphrase = "123456";
                    $error = true;
                    $errorString = STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
                    $ctx = stream_context_create();
                    stream_context_set_option($ctx, 'ssl', 'local_cert', "/var/www/html/app/webroot/ck_new.pem");
                  
                  //$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
                  
                    $stream = stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
                    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $ctx);
                    if ($apns === False) {
                      return;
                    }
                  
                    $output = json_encode($payload);
                    file_put_contents('newfile.txt', print_r($output, true));
                    file_put_contents('newfile1.txt', print_r($data, true));
                    $token = pack('H*', str_replace(' ', '', $token));
                    $apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) .       chr(strlen($output)) . $output;
                    $result = fwrite($apns, $apnsMessage);
                    @socket_close($apns);
                    fclose($apns);
                    if (!$result)
                    {
                     
                      return 'Message not delivered';
                    }
                    else
                    {  
                      echo "send successfully";
                      file_put_contents('newfile.txt', print_r($data, true));
                      return $msg;
    
                   }
              }    
    
     ?>         

    I hope this blog will help you to send notification on iPhone devices.

 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: