Magento2 Push notification for IOS and Android Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Paypal - Magento MobileWhich api method is best for appsForce store-level values via php instead of defaultIssue (BUG) with ios devices and Magento Mobile Theme HTML5Push notifiation in magento2 for android?What are the steps to build native apps for website powered by magento 2.x?Rest API issue in cart : multiple store only shows default store's price of a product in cart for both store in magento2.1.7List of REST API's for Magento-1.9.1.1How can i get store_id in Magento2 REST APIMagento2: How to stop sending customer welcome email when order created using magento rest api?

Why are there no cargo aircraft with "flying wing" design?

What do you call a plan that's an alternative plan in case your initial plan fails?

Gastric acid as a weapon

What is this single-engine low-wing propeller plane?

Should I call the interviewer directly, if HR aren't responding?

What are the motives behind Cersei's orders given to Bronn?

Why is high voltage dangerous?

WAN encapsulation

How do I stop a creek from eroding my steep embankment?

How to deal with a team lead who never gives me credit?

Why is black pepper both grey and black?

How can I make names more distinctive without making them longer?

How do I determine if the rules for a long jump or high jump are applicable for Monks?

How can whole tone melodies sound more interesting?

Do you forfeit tax refunds/credits if you aren't required to and don't file by April 15?

When is phishing education going too far?

When to stop saving and start investing?

How widely used is the term Treppenwitz? Is it something that most Germans know?

How to bypass password on Windows XP account?

What are the pros and cons of Aerospike nosecones?

Is there a service that would inform me whenever a new direct route is scheduled from a given airport?

Models of set theory where not every set can be linearly ordered

"Seemed to had" is it correct?

Stars Make Stars



Magento2 Push notification for IOS and Android



Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Paypal - Magento MobileWhich api method is best for appsForce store-level values via php instead of defaultIssue (BUG) with ios devices and Magento Mobile Theme HTML5Push notifiation in magento2 for android?What are the steps to build native apps for website powered by magento 2.x?Rest API issue in cart : multiple store only shows default store's price of a product in cart for both store in magento2.1.7List of REST API's for Magento-1.9.1.1How can i get store_id in Magento2 REST APIMagento2: How to stop sending customer welcome email when order created using magento rest api?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am creating a native app for my magento2 store, Want to create facility of Mobile notification for different module (default and custom), I have certificate for IOS that will be use regarding this.



How can I integrate this certificate and send push notification via rest api, And want the same for Android as well.










share|improve this question




























    1















    I am creating a native app for my magento2 store, Want to create facility of Mobile notification for different module (default and custom), I have certificate for IOS that will be use regarding this.



    How can I integrate this certificate and send push notification via rest api, And want the same for Android as well.










    share|improve this question
























      1












      1








      1


      1






      I am creating a native app for my magento2 store, Want to create facility of Mobile notification for different module (default and custom), I have certificate for IOS that will be use regarding this.



      How can I integrate this certificate and send push notification via rest api, And want the same for Android as well.










      share|improve this question














      I am creating a native app for my magento2 store, Want to create facility of Mobile notification for different module (default and custom), I have certificate for IOS that will be use regarding this.



      How can I integrate this certificate and send push notification via rest api, And want the same for Android as well.







      magento2.2 api rest-api mobile notification






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 2 '18 at 5:06









      YogitaYogita

      308215




      308215




















          2 Answers
          2






          active

          oldest

          votes


















          0














          Use below code to send push notification in Android device.



           $url = 'https://fcm.googleapis.com/fcm/send';
          //echo $url;
          $ch = curl_init();
          $auth_key = ''; //get from fcm.googleapis.com
          $deviceToken=''; // get for device
          $message = array('title'=>'sai', 'body'=>'Hai');

          $fields = array(
          'registration_ids' => array($deviceToken),
          'data' => $message,
          );
          // print_r(json_encode($fields));
          $headers = array(
          'Authorization: key=' .$auth_key,
          'Content-Type: application/json'
          );
          $ch = curl_init();
          curl_setopt( $ch, CURLOPT_URL, $url);
          curl_setopt( $ch, CURLOPT_POST, true );
          curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
          curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
          curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
          curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields));
          $response=curl_exec($ch);
          $response = json_decode($response,true);
          print_r($response);
          exit;





          share|improve this answer
































            0














            Below approach may help you:



            Write the respective function in helper class.



             namespace PackageNameModuleNameHelper;

            class Data extends MagentoFrameworkAppHelperAbstractHelper

            const API_ACCESS_KEY = "";//Api Access key for android.
            const PASSPHASE = ""; // passphase for IOS

            protected $dir;

            /**
            * @param MagentoFrameworkAppHelperContext $context
            * @param MagentoFrameworkFilesystemDirectoryList $dir
            */
            public function __construct(
            MagentoFrameworkAppHelperContext $context,
            MagentoFrameworkFilesystemDirectoryList $dir
            )
            $this->dir = $dir;
            parent::__construct($context);


            /**
            * send push notification for ios
            */
            public function sendIosPushNotification($message,$devicetoken)
            $mediaPath = $this->dir->getPath('media').'/';
            $tCert = $mediaPath.'/iospemfile/pushcert.pem';// your certificates file location
            $ctx = stream_context_create();
            stream_context_set_option($ctx, 'ssl', 'local_cert', $tCert);
            stream_context_set_option($ctx, 'ssl', 'passphrase', self::PASSPHASE);
            $applegateway='ssl://gateway.sandbox.push.apple.com:2195';//for sanbox mode
            //$applegateway='ssl://gateway.push.apple.com:2195';//for production mode
            $fp = stream_socket_client($applegateway, $err, $errstr, 60, STREAM_CLIENT_CONNECT

            /**
            * send push notification for android
            */
            public function sendAndroidPushNotification($message,$fcmtoken)
            $data = array('message' => $message,'title' => 'test title') ;
            $fields = array('data' => $data ,'to' => $fcmtoken);

            $headers = array(
            'Authorization: key=' . self::API_ACCESS_KEY,
            'Content-Type: application/json'
            );

            #Send Reponse To FireBase Server
            $ch = curl_init();
            curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
            curl_setopt( $ch,CURLOPT_POST, true );
            curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
            curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
            $result = curl_exec($ch );
            curl_close( $ch );





            After that you can use those helper functions from anywhere by injecting the helper class in constructor of the where you want to use those functions:



            If your helper class object is $this->notificationHelper then you can call the functions following way:



            //for ios
            $msg = "test push for ios";
            $deviceToken = "iosdevicetoken";
            $this->notificationHelper->sendIosPushNotification($msg ,$deviceToken);

            //for android
            $msg = "test push for android";
            $fcmToken = "fcmToken ";
            $this->notificationHelper->sendAndroidPushNotification($msg ,$fcmToken );





            share|improve this answer























              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "479"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f228405%2fmagento2-push-notification-for-ios-and-android%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Use below code to send push notification in Android device.



               $url = 'https://fcm.googleapis.com/fcm/send';
              //echo $url;
              $ch = curl_init();
              $auth_key = ''; //get from fcm.googleapis.com
              $deviceToken=''; // get for device
              $message = array('title'=>'sai', 'body'=>'Hai');

              $fields = array(
              'registration_ids' => array($deviceToken),
              'data' => $message,
              );
              // print_r(json_encode($fields));
              $headers = array(
              'Authorization: key=' .$auth_key,
              'Content-Type: application/json'
              );
              $ch = curl_init();
              curl_setopt( $ch, CURLOPT_URL, $url);
              curl_setopt( $ch, CURLOPT_POST, true );
              curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
              curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
              curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
              curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
              curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields));
              $response=curl_exec($ch);
              $response = json_decode($response,true);
              print_r($response);
              exit;





              share|improve this answer





























                0














                Use below code to send push notification in Android device.



                 $url = 'https://fcm.googleapis.com/fcm/send';
                //echo $url;
                $ch = curl_init();
                $auth_key = ''; //get from fcm.googleapis.com
                $deviceToken=''; // get for device
                $message = array('title'=>'sai', 'body'=>'Hai');

                $fields = array(
                'registration_ids' => array($deviceToken),
                'data' => $message,
                );
                // print_r(json_encode($fields));
                $headers = array(
                'Authorization: key=' .$auth_key,
                'Content-Type: application/json'
                );
                $ch = curl_init();
                curl_setopt( $ch, CURLOPT_URL, $url);
                curl_setopt( $ch, CURLOPT_POST, true );
                curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
                curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
                curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields));
                $response=curl_exec($ch);
                $response = json_decode($response,true);
                print_r($response);
                exit;





                share|improve this answer



























                  0












                  0








                  0







                  Use below code to send push notification in Android device.



                   $url = 'https://fcm.googleapis.com/fcm/send';
                  //echo $url;
                  $ch = curl_init();
                  $auth_key = ''; //get from fcm.googleapis.com
                  $deviceToken=''; // get for device
                  $message = array('title'=>'sai', 'body'=>'Hai');

                  $fields = array(
                  'registration_ids' => array($deviceToken),
                  'data' => $message,
                  );
                  // print_r(json_encode($fields));
                  $headers = array(
                  'Authorization: key=' .$auth_key,
                  'Content-Type: application/json'
                  );
                  $ch = curl_init();
                  curl_setopt( $ch, CURLOPT_URL, $url);
                  curl_setopt( $ch, CURLOPT_POST, true );
                  curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
                  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
                  curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
                  curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
                  curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields));
                  $response=curl_exec($ch);
                  $response = json_decode($response,true);
                  print_r($response);
                  exit;





                  share|improve this answer















                  Use below code to send push notification in Android device.



                   $url = 'https://fcm.googleapis.com/fcm/send';
                  //echo $url;
                  $ch = curl_init();
                  $auth_key = ''; //get from fcm.googleapis.com
                  $deviceToken=''; // get for device
                  $message = array('title'=>'sai', 'body'=>'Hai');

                  $fields = array(
                  'registration_ids' => array($deviceToken),
                  'data' => $message,
                  );
                  // print_r(json_encode($fields));
                  $headers = array(
                  'Authorization: key=' .$auth_key,
                  'Content-Type: application/json'
                  );
                  $ch = curl_init();
                  curl_setopt( $ch, CURLOPT_URL, $url);
                  curl_setopt( $ch, CURLOPT_POST, true );
                  curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
                  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
                  curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
                  curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
                  curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields));
                  $response=curl_exec($ch);
                  $response = json_decode($response,true);
                  print_r($response);
                  exit;






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jun 2 '18 at 14:17









                  Prasanta Hatui

                  1,4601514




                  1,4601514










                  answered Jun 2 '18 at 12:42









                  Sairam SigirisettySairam Sigirisetty

                  2816




                  2816























                      0














                      Below approach may help you:



                      Write the respective function in helper class.



                       namespace PackageNameModuleNameHelper;

                      class Data extends MagentoFrameworkAppHelperAbstractHelper

                      const API_ACCESS_KEY = "";//Api Access key for android.
                      const PASSPHASE = ""; // passphase for IOS

                      protected $dir;

                      /**
                      * @param MagentoFrameworkAppHelperContext $context
                      * @param MagentoFrameworkFilesystemDirectoryList $dir
                      */
                      public function __construct(
                      MagentoFrameworkAppHelperContext $context,
                      MagentoFrameworkFilesystemDirectoryList $dir
                      )
                      $this->dir = $dir;
                      parent::__construct($context);


                      /**
                      * send push notification for ios
                      */
                      public function sendIosPushNotification($message,$devicetoken)
                      $mediaPath = $this->dir->getPath('media').'/';
                      $tCert = $mediaPath.'/iospemfile/pushcert.pem';// your certificates file location
                      $ctx = stream_context_create();
                      stream_context_set_option($ctx, 'ssl', 'local_cert', $tCert);
                      stream_context_set_option($ctx, 'ssl', 'passphrase', self::PASSPHASE);
                      $applegateway='ssl://gateway.sandbox.push.apple.com:2195';//for sanbox mode
                      //$applegateway='ssl://gateway.push.apple.com:2195';//for production mode
                      $fp = stream_socket_client($applegateway, $err, $errstr, 60, STREAM_CLIENT_CONNECT

                      /**
                      * send push notification for android
                      */
                      public function sendAndroidPushNotification($message,$fcmtoken)
                      $data = array('message' => $message,'title' => 'test title') ;
                      $fields = array('data' => $data ,'to' => $fcmtoken);

                      $headers = array(
                      'Authorization: key=' . self::API_ACCESS_KEY,
                      'Content-Type: application/json'
                      );

                      #Send Reponse To FireBase Server
                      $ch = curl_init();
                      curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
                      curl_setopt( $ch,CURLOPT_POST, true );
                      curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
                      curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
                      curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
                      curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
                      $result = curl_exec($ch );
                      curl_close( $ch );





                      After that you can use those helper functions from anywhere by injecting the helper class in constructor of the where you want to use those functions:



                      If your helper class object is $this->notificationHelper then you can call the functions following way:



                      //for ios
                      $msg = "test push for ios";
                      $deviceToken = "iosdevicetoken";
                      $this->notificationHelper->sendIosPushNotification($msg ,$deviceToken);

                      //for android
                      $msg = "test push for android";
                      $fcmToken = "fcmToken ";
                      $this->notificationHelper->sendAndroidPushNotification($msg ,$fcmToken );





                      share|improve this answer



























                        0














                        Below approach may help you:



                        Write the respective function in helper class.



                         namespace PackageNameModuleNameHelper;

                        class Data extends MagentoFrameworkAppHelperAbstractHelper

                        const API_ACCESS_KEY = "";//Api Access key for android.
                        const PASSPHASE = ""; // passphase for IOS

                        protected $dir;

                        /**
                        * @param MagentoFrameworkAppHelperContext $context
                        * @param MagentoFrameworkFilesystemDirectoryList $dir
                        */
                        public function __construct(
                        MagentoFrameworkAppHelperContext $context,
                        MagentoFrameworkFilesystemDirectoryList $dir
                        )
                        $this->dir = $dir;
                        parent::__construct($context);


                        /**
                        * send push notification for ios
                        */
                        public function sendIosPushNotification($message,$devicetoken)
                        $mediaPath = $this->dir->getPath('media').'/';
                        $tCert = $mediaPath.'/iospemfile/pushcert.pem';// your certificates file location
                        $ctx = stream_context_create();
                        stream_context_set_option($ctx, 'ssl', 'local_cert', $tCert);
                        stream_context_set_option($ctx, 'ssl', 'passphrase', self::PASSPHASE);
                        $applegateway='ssl://gateway.sandbox.push.apple.com:2195';//for sanbox mode
                        //$applegateway='ssl://gateway.push.apple.com:2195';//for production mode
                        $fp = stream_socket_client($applegateway, $err, $errstr, 60, STREAM_CLIENT_CONNECT

                        /**
                        * send push notification for android
                        */
                        public function sendAndroidPushNotification($message,$fcmtoken)
                        $data = array('message' => $message,'title' => 'test title') ;
                        $fields = array('data' => $data ,'to' => $fcmtoken);

                        $headers = array(
                        'Authorization: key=' . self::API_ACCESS_KEY,
                        'Content-Type: application/json'
                        );

                        #Send Reponse To FireBase Server
                        $ch = curl_init();
                        curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
                        curl_setopt( $ch,CURLOPT_POST, true );
                        curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
                        curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
                        curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
                        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
                        $result = curl_exec($ch );
                        curl_close( $ch );





                        After that you can use those helper functions from anywhere by injecting the helper class in constructor of the where you want to use those functions:



                        If your helper class object is $this->notificationHelper then you can call the functions following way:



                        //for ios
                        $msg = "test push for ios";
                        $deviceToken = "iosdevicetoken";
                        $this->notificationHelper->sendIosPushNotification($msg ,$deviceToken);

                        //for android
                        $msg = "test push for android";
                        $fcmToken = "fcmToken ";
                        $this->notificationHelper->sendAndroidPushNotification($msg ,$fcmToken );





                        share|improve this answer

























                          0












                          0








                          0







                          Below approach may help you:



                          Write the respective function in helper class.



                           namespace PackageNameModuleNameHelper;

                          class Data extends MagentoFrameworkAppHelperAbstractHelper

                          const API_ACCESS_KEY = "";//Api Access key for android.
                          const PASSPHASE = ""; // passphase for IOS

                          protected $dir;

                          /**
                          * @param MagentoFrameworkAppHelperContext $context
                          * @param MagentoFrameworkFilesystemDirectoryList $dir
                          */
                          public function __construct(
                          MagentoFrameworkAppHelperContext $context,
                          MagentoFrameworkFilesystemDirectoryList $dir
                          )
                          $this->dir = $dir;
                          parent::__construct($context);


                          /**
                          * send push notification for ios
                          */
                          public function sendIosPushNotification($message,$devicetoken)
                          $mediaPath = $this->dir->getPath('media').'/';
                          $tCert = $mediaPath.'/iospemfile/pushcert.pem';// your certificates file location
                          $ctx = stream_context_create();
                          stream_context_set_option($ctx, 'ssl', 'local_cert', $tCert);
                          stream_context_set_option($ctx, 'ssl', 'passphrase', self::PASSPHASE);
                          $applegateway='ssl://gateway.sandbox.push.apple.com:2195';//for sanbox mode
                          //$applegateway='ssl://gateway.push.apple.com:2195';//for production mode
                          $fp = stream_socket_client($applegateway, $err, $errstr, 60, STREAM_CLIENT_CONNECT

                          /**
                          * send push notification for android
                          */
                          public function sendAndroidPushNotification($message,$fcmtoken)
                          $data = array('message' => $message,'title' => 'test title') ;
                          $fields = array('data' => $data ,'to' => $fcmtoken);

                          $headers = array(
                          'Authorization: key=' . self::API_ACCESS_KEY,
                          'Content-Type: application/json'
                          );

                          #Send Reponse To FireBase Server
                          $ch = curl_init();
                          curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
                          curl_setopt( $ch,CURLOPT_POST, true );
                          curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
                          curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
                          curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
                          curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
                          $result = curl_exec($ch );
                          curl_close( $ch );





                          After that you can use those helper functions from anywhere by injecting the helper class in constructor of the where you want to use those functions:



                          If your helper class object is $this->notificationHelper then you can call the functions following way:



                          //for ios
                          $msg = "test push for ios";
                          $deviceToken = "iosdevicetoken";
                          $this->notificationHelper->sendIosPushNotification($msg ,$deviceToken);

                          //for android
                          $msg = "test push for android";
                          $fcmToken = "fcmToken ";
                          $this->notificationHelper->sendAndroidPushNotification($msg ,$fcmToken );





                          share|improve this answer













                          Below approach may help you:



                          Write the respective function in helper class.



                           namespace PackageNameModuleNameHelper;

                          class Data extends MagentoFrameworkAppHelperAbstractHelper

                          const API_ACCESS_KEY = "";//Api Access key for android.
                          const PASSPHASE = ""; // passphase for IOS

                          protected $dir;

                          /**
                          * @param MagentoFrameworkAppHelperContext $context
                          * @param MagentoFrameworkFilesystemDirectoryList $dir
                          */
                          public function __construct(
                          MagentoFrameworkAppHelperContext $context,
                          MagentoFrameworkFilesystemDirectoryList $dir
                          )
                          $this->dir = $dir;
                          parent::__construct($context);


                          /**
                          * send push notification for ios
                          */
                          public function sendIosPushNotification($message,$devicetoken)
                          $mediaPath = $this->dir->getPath('media').'/';
                          $tCert = $mediaPath.'/iospemfile/pushcert.pem';// your certificates file location
                          $ctx = stream_context_create();
                          stream_context_set_option($ctx, 'ssl', 'local_cert', $tCert);
                          stream_context_set_option($ctx, 'ssl', 'passphrase', self::PASSPHASE);
                          $applegateway='ssl://gateway.sandbox.push.apple.com:2195';//for sanbox mode
                          //$applegateway='ssl://gateway.push.apple.com:2195';//for production mode
                          $fp = stream_socket_client($applegateway, $err, $errstr, 60, STREAM_CLIENT_CONNECT

                          /**
                          * send push notification for android
                          */
                          public function sendAndroidPushNotification($message,$fcmtoken)
                          $data = array('message' => $message,'title' => 'test title') ;
                          $fields = array('data' => $data ,'to' => $fcmtoken);

                          $headers = array(
                          'Authorization: key=' . self::API_ACCESS_KEY,
                          'Content-Type: application/json'
                          );

                          #Send Reponse To FireBase Server
                          $ch = curl_init();
                          curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
                          curl_setopt( $ch,CURLOPT_POST, true );
                          curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
                          curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
                          curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
                          curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
                          $result = curl_exec($ch );
                          curl_close( $ch );





                          After that you can use those helper functions from anywhere by injecting the helper class in constructor of the where you want to use those functions:



                          If your helper class object is $this->notificationHelper then you can call the functions following way:



                          //for ios
                          $msg = "test push for ios";
                          $deviceToken = "iosdevicetoken";
                          $this->notificationHelper->sendIosPushNotification($msg ,$deviceToken);

                          //for android
                          $msg = "test push for android";
                          $fcmToken = "fcmToken ";
                          $this->notificationHelper->sendAndroidPushNotification($msg ,$fcmToken );






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jun 2 '18 at 15:13









                          Prasanta HatuiPrasanta Hatui

                          1,4601514




                          1,4601514



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Magento Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f228405%2fmagento2-push-notification-for-ios-and-android%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Sum ergo cogito? 1 nng

                              三茅街道4182Guuntc Dn precexpngmageondP