0

I'm trying to setup the application server part of C2DM push messaging using this code - https://github.com/lytsing/c2dm-php.

I have completed the app side of things and have registered an email address with Google - every time I run the code (on a server with php/cURL installed) i get the error 'get auth token error'. It driving me nuts as I've no idea where to begin to solve the problem.

The only lines I have changed in the code are - in the s2dm.php file -

  'source'        => 'com.phonegap.chillimusicapp', 

and I added my email/password into the post.php file -

  $result = $c2dm->getAuthToken("email@googlemail.com", "password");

Any advice would be great! Cheers Paul

Dancer
  • 17,035
  • 38
  • 129
  • 206

1 Answers1

1

Try using below sample code, It is working fine.

<?php
define("C2DM_ACCOUNT_EMAIL","[C2DM_EMAIL]");
define("C2DM_ACCOUNT_PASSWORD","[C2DM_PASSWORD]");
define("C2DM_CLIENT_LOGIN_URL","https://www.google.com/accounts/ClientLogin");
define("C2DM_MSG_SEND_URL","https://android.apis.google.com/c2dm/send");

function sendPushNotification($device_reg_id,$msg){

    $auth_id=get_auth_id(); // To get Auth ID

    $post_fields=array(
        'collapse_key=ck_1',
        'registration_id='. trim($device_reg_id),
        'data.payload='. trim($msg),
    );

    $data_str=implode('&', $post_fields);

    $headers = array(
    'Authorization: GoogleLogin auth='.trim($auth_id),
    'Content-Type: application/x-www-form-urlencoded',
    'Content-Length: '.trim(strlen($data_str)),
    'Connection: close'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,C2DM_MSG_SEND_URL);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec ($ch);
    curl_close ($ch);
//  print_r($server_output);
}

function get_auth_id(){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,C2DM_CLIENT_LOGIN_URL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "Email=".C2DM_ACCOUNT_EMAIL."&Passwd=".C2DM_ACCOUNT_PASSWORD."&accountType=GOOGLE&source=Google-cURL-Example&service=ac2dm");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec ($ch);
    curl_close ($ch);
//  print_r($server_output);
    $parts=explode("Auth=",$server_output);
    $auth_id=$parts[1];
//  echo $auth_id;
    return $auth_id;
}

$reg_id = "[DEVICE_REG_ID]";
sendPushNotification($reg_id,"Hello World...!! Jay is testing C2DM...");

FYI! No need to call get_auth_id() every time you send notification, You can call once and store auth_id somewhere in config file also.

jaym
  • 1,253
  • 13
  • 18
  • Hi Jay - thats great thanks - this replaces the previous solution entirely then? – Dancer Jun 13 '12 at 14:05
  • would a usual approach be to tie this in with a protected form for message submission? – Dancer Jun 13 '12 at 14:06
  • Yes Paul, this will replace your solution. but I think this will fulfill your need correct ? – jaym Jun 13 '12 at 19:36
  • Hi Jay, Cheers for your help - though I just get a 'invalid registration' error when submitting at the moment!? – Dancer Jun 14 '12 at 08:32
  • You can get registration ID from Android side, you can save registration id somewhere in DB via WS. and then you can use that ID to send Push notification. – jaym Jun 14 '12 at 11:16
  • Example RegID : APA91bGKLr_gFrK8rjPie9iHyX4avTjOew1cedpsg-u-7jkE_Ff6faqlDj5xSmSoUKq3xcOfvxc1YIBW_eI1sfZrptuJJIij-MPwOSB_It6tQQ8wmY5opzANkLR64KCBBMZFTSFZKd6yIzHAPtV7_kY9skufpdOwAQ this is of my application and my mobile, so don't use this!! :) – jaym Jun 14 '12 at 11:19