cancel
Showing results for 
Search instead for 
Did you mean: 

magento 2 rest API send mobile notifications and save devices token

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

magento 2 rest API send mobile notifications and save devices token

Hello All,

i want to consume the Magento 2.1.6  restful API from Android and IOs native apps, i want to save the devices tokens to send notifications to the apps, could you please help me do it as i tried to search for it but no answer.

 

Thanks

1 REPLY 1

Re: magento 2 rest API send mobile notifications and save devices token

Hello, 

You can follow the link in order to implement send notification for mobile application using Magento 2 rest api.

 

<?php
/**
 * Developer: Hemant Singh Magento 2x Developer
 * Category:  Push Notification Magento 2 API
 */
    $ch = curl_init("https://fcm.googleapis.com/fcm/send");

   //The device token.
   $token =  'fNkJjlW8RBe6aJ4LdkSXL0:APA91bH-0CS3RS9D-DHm5W4QqCPF3qT6YmhrlVqObSBpQn1vdEOGe2xoA4yfMR-Lgzqe8baPg4Mpi7AhW_Do1rKyJ5wlFRzBV8baGA8TyjXGleEi54N-pnIKmO0dA_iS69p8IQ3_NjXA';
 
   //Title of the Notification.
   $title = 'Hello';

    //Body of the Notification.
   $body = 'Hello Body';

   //Creating the notification array.
   $notification = array('title' => $title, 'body'=>$body, 'sound'=>'default');

   //This array contains, the token and the notification. The 'to' attribute stores the token.

   
       $arrayToSend = array(
                       'to' => $token,
                       'notification' => $notification,
                       'data' => $notification,
                       'priority'=>'high'              
                   );

   //Generating JSON encoded string form the above array.
   $json = json_encode($arrayToSend);
   //Setup headers:
   $headers = array();
   $headers[] = 'Content-Type: application/json';
   $headers[] = 'Authorization: key= AAAABfcWdgc:APA91bGZcOSwGbuZrVIFdiFUMyjvnGvXYB31MeYmzdw3ZdE4dT6LTgsb85cm_n3Nth0kbWI7mNmNNWkb0uLyx3KPMfjW-tgEmnQzQDumeH2kL9ew-_yyluxvq4Kfv8DNmlK9pXKVTkLQ'; // key here

   //Setup curl, add headers and post parameters.
   curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                        
   curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
   curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     
   //Send the request
   $response = curl_exec($ch);
   //Close request
   curl_close($ch);
   //Print Success and Faliure Message 
   echo "<pre>"; print_r(json_decode($response));
   
?>

 

Thank You!