문제

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;
}
도움이 되었습니까?

해결책 2

I've managed to solve the logic here (thanks in part to Eran's pointing out my error in thinking), with a very simple addition to my business logic and an additional table in the database. In case anyone runs into the particular problem I've had here. The table I added simply has a structure of:

--gcm_users
----id
----user_id
----gcm_device_id

In my business logic I go about it this way. When a user logs in, my API checks if the GCM registration id already exists in the gcm_device_id field. If it doesn't a new row is added with the id of the user logging in (user_id) and the GCM registration id (gcm_device_id). If the row already exists, then the user_id of the row is updated with the id of the user logging in. This solves the issue of another user logging in on the same device. Thanks again for the great community support and I hope this answer helps someone someday.

다른 팁

Adding those two fields is not enough, since a registration ID is tied to a device, not to the user that is currently logged in on that device.

If a user logs out on a device and another user logs in, your suggested implementation means that both users would have the same registration ID in your DB, and sending a notification to all devices would send two notifications to that device.

You can overcome this problem if you clear the registration ID column from the row of a user that logs out. If you are expecting the users that have an Adnroid device to be a small percentage of all your users, it would make more sense to have a separate gcm_users table. This way you won't need to go over the bigger users table in order to send GCM message to all the devices.

Finally, here sre some additional suggestions on how to store a GCM registration ID into mysql.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top