Ganesh V.V.
Ganesh V.V.

Reputation: 21

Phonegaph Pushbots Plugin: How to get device registration id from google cloud messaging?

I am using PushBots plugin in my Phonegap application for android phones. I need to get the device registration id from google cloud messaging server to send user specific push notification. I am using PHP and mySQL in back-end. Basically I need to save the device registration id to mysql db once the device is ready. Please help me to get a solution.

Upvotes: 0

Views: 1041

Answers (1)

shamon shamsudeen
shamon shamsudeen

Reputation: 5858

First get the device token and store it to the database,and later it can be used for sending push-notification.

document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady(){
     //setting up for android
     if(PushbotsPlugin.isAndroid()){
        PushbotsPlugin.initializeAndroid("PUSHBOTS_APP_ID", "GCM_SENDER_ID");
    }
     //get the device token
    PushbotsPlugin.getToken(function(token){
        console.log(token);
    //setup an ajax request to store the device token
     $.ajax({
      url:'register.php',
      method:"POST",
      data:{token :token},
      success:function(data){
        console.log("from server"+data);
      },
      error:function(err){
       console.log("error"+err);   
     }
    });

    });
}

register.php

 <?php 
    $token=$_POST['token'];
   //setup database query for store the token
   if($query==true){
    echo 'success';
    }else{
      echo 'error';
    }
  ?>

Also replace PUSH_BOT_APP_ID and GCM_SENDER_ID with your values

Upvotes: 0

Related Questions