I've written an API based on the Slim PHP network that is to be consumed both by a web (browser) and Android client and I have a question as to implementing Google Cloud Messaging for push notifications for the android client. Basically, this is my current data structure
users:
--Fields:
----id
----name
----email
----password
posts:
--Fields:
----id
----post_title
----post_category
----post_content
----image_url
----created_at
comments:
--Fields:
----id
----comment
----mood
----created_at
post_comments (which holds the relationship between the post and the comment):
--Fields:
----id
----post_id
----comment_id
user_comments (which holds the relationship between the user and the comment):
--Fields:
----id
----user_id
----comment_id
What I want to do is have a notification sent to all devices (that fulfill certain requirements) when the post table has a row added. I'm quite sure I understand the actual push functionality for PHP but I'm not sure how to structure it to register devices that are running on an android device. I was thinking to add two fields to my users table like this:
----has_device
----device_registration_id
and have the android device send a has_device=1 param on login (as opposed to the web client which would send has_device=0 for false) and check whether that user's device_registration_id field is empty and if it is then generate a new registration id and populate that field. Am I on the right track here?
EDIT: This would be my PHP send function.
function sendNotification( $apiKey, $registrationIdsArray, $messageData )
{
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}