1

How can i push notification via Parse.com using php code. I want to push this to specific defined channel i.e subscribeInBackground.

If anyone give a quick solution than it will be really helpful for me. I have refered below links but it is not helpful.

manojprasad
  • 158
  • 2
  • 8

1 Answers1

0

in server-side (php) use this code ( it works for me )

function sendPush($message,$email)
{
$APPLICATION_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";  //put your identifiers here
$REST_API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";  // and here 

$url = 'https://api.parse.com/1/push';

$data = array(
    // 'where' => '{}',     //uncomment this line to send push to all users
    'where' => array(     // send to specific user via email
        'email' => $email
    ),

    'data' => array(
        'alert' => $message,
    ),
);

$_data = json_encode($data);

$headers = array(
    'X-Parse-Application-Id: ' . $APPLICATION_ID,
    'X-Parse-REST-API-Key: ' . $REST_API_KEY,
    'Content-Type: application/json',
    'Content-Length: ' . strlen($_data),
);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($curl);
echo $result;
}

and in client side (android) use this code to register and identify each user via email:

public static void subscribeWithEmail(String email) {
    ParseInstallation installation = ParseInstallation.getCurrentInstallation();
    installation.put("email", email);
    installation.saveInBackground();
}

Then ... when you call your sendPush function in server it will send message to specific user via passed email and message to sendPush function ...

and you wanted specific channel yeah? Replace this in php code ...

 $data = array(
    'channel' => 'yourChannelName',
    'data' => array(
        'alert' => $message,
    ),
);

and use this code in android for subscribe to channel :

ParsePush.subscribeInBackground("yourChannelName", new SaveCallback() {
        @Override
        public void done(ParseException e) {
            Log.d("LOG","successfully subscribed to channel :)"); 
        }
    });
Ali Sherafat
  • 3,506
  • 2
  • 38
  • 51