Search code examples
androidandroid-c2dmgoogle-cloud-messaging

GCM Push Notification without using JSON


I am trying to migrate my application from C2DM service to new GCM push notification. I have successfully integrated the GCMIntentService class which extends GCMBaseIntentService. When I send the push notification from server using PHP, the GCM sends the message data as a JSON object. I am using the following code in my Android GCM service, and its returning a null value with the new code.

public void onMessage(Context context, Intent intent)
    {
        String action = intent.getAction();     
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action))   {            
             message=intent.getStringExtra("message");                                  
            createNotification(context);
        }
   }

The PHP Script contains :

$headers = array( 'Authorization: key=' . $apiKey, 'Content-Type: application/json' );

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

Please suggest me what all the changes I require in my PHP script to send the push notification using GCM libraries and without JSON.

Thanks in advance Tim


Solution

  • you have to send the payload via JSON - whats the reason you do not want to use JSON?