Android Push Notification via PHP in Ionic/Cordova

3:26 PM


If you want to send push notification to a android device via PHP the you've to follow below steps :
  • If you are using phonegap then install phonegap push notification plugin via
    
           cordova plugin add phonegap-plugin-push
          
    OR
    If you are working on native coding, You've to install Android Push Notificaion SDK
  • Create a project on Google Developers Console and enable "Cloud Messaging for Android" API for this project
  • This code execute after device ready :
          
           
       var push = PushNotification.init({
         "android" : {
          "senderID" : "9XX5XXX346XX"// This is your project number which you've created on Google Developers Console
         },
         "ios" : {},
         "windows" : {}
        });
    
       push.on('registration', function(data) {
        //data.registrationId
        //send this token on server
    
       });
       push.on('notification', function(data) {
        if (data.sound) {// play specific sound on notification received(When app running)
         var snd = new Media('file:///android_asset/www/sound/' + data.sound);
         snd.play();
        }
        
        if (!data.additionalData.foreground) {
         // app is NOT RUNNING and new notification is RECEIVED
         
        } else {
         // app is RUNNING and new notification is RECEIVED
        }
       });
    
       push.on('error', function(e) {
        //alert(JSON.stringify(data))
        // e.message
       });
          
         
  • PHP Code for send push notification
          
           $api_key = "AXXaSyXXyKXXNZXXf8O27hXX2DcrXXAEeXXuXX0";// your Google Developers Console Project API key
            
           $data =  array(
                          "message" => $message,
                          'title' => $title,
                      );
             
           
           // URL to POST to
           $gcm_url = 'https://android.googleapis.com/gcm/send';
          
           // data to be posted
           $fields = array('registration_ids' => $registration_ids, 'data' => $data, );
          
           // headers for the request
           $headers = array('Authorization: key=' . $api_key, 'Content-Type: application/json');
          
           $curl_handle = curl_init();
          
           //echo 'Notification PhP file';
          
           // set CURL options
           curl_setopt($curl_handle, CURLOPT_URL, $gcm_url);
          
           curl_setopt($curl_handle, CURLOPT_POST, true);
           curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
           curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
          
           curl_setopt($curl_handle, CURLOPT_POSTFIELDS, json_encode($fields));
          
           // send
           $response = curl_exec($curl_handle);
          
           curl_close($curl_handle);
           
          
         

You Might Also Like

15 comments

  1. Hi,
    Do we need to publish our app on playstore to use this url?
    https://android.googleapis.com/gcm/send

    ReplyDelete
  2. Yes, you've to pass $gcm_url = 'https://android.googleapis.com/gcm/send';

    ReplyDelete
  3. where is the $registration_ids definition?

    ReplyDelete
  4. push.on('registration', function(data) {
    //data.registrationId
    //send this token on server

    });

    Form this code you'll find the $registration_ids(this is part of cordova plugin). You've to save this $registration_ids(data.registrationId) with user id in a table and at the time of send notification you can find the $registration_ids(data.registrationId) in your table

    ReplyDelete
  5. nice blog too informative. looking and reading your points its so impressive. doing more blog like this. i really appreciated doing like this.
    dot net training in chennai

    ReplyDelete
  6. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.Android training in chennai with placement | Android Training in chennai

    ReplyDelete
  7. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

    Texting API
    Text message marketing
    Digital Mobile Marketing
    Sms API
    Sms marketing

    ReplyDelete
  8. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    PSD to Wordpress
    wordpress website development

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.keep it up!!

    android training in chennai

    android online training in chennai

    android training in bangalore

    android training in hyderabad

    android Training in coimbatore

    android training

    android online training

    ReplyDelete
  11. Hello, also a simple way is with INDIGITALL, it has been developed with the latest technology to guarantee maximum effectiveness and simplify the process of creating and sending Notifications with an animated image, Segmented, Geolocated and many more functions, it is great. Here you can see the simple installation for your platform
    https://docs.indigitall.com/

    ReplyDelete