Android GCM

流程
  1. 註冊 Google Api key 取得 Project Number(給 android app用)  & API key(給 server用)
  2. 架設 server side,儲存 android 端第一次從 GCM註冊取得的裝置ID
  3. 撰寫 server side -> GCM
Android Dev
    http://developer.android.com/google/gcm/gcm.html#send-msg
    將以下資訊POST到 https://android.googleapis.com/gcm/send
方法一(最多同時 1000個裝置ID):
Content-Type:application/json
Authorization:key= GOOGL_API_KEY

{
  "registration_ids" : ["手機裝置ID"],
  "data" : {"price":"123456"}  <-裡面為要傳送給User的資訊(4kb以內)
}

方法二(一次一個裝置):
Content-Type:application/x-www-form-urlencoded;charset=UTF-8
Authorization:key= GOOGL_API_KEY

collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.price=123456&registration_id=裝置ID

-----------------------------------------
    ps.
    手機裝置ID範例
    APA91bFIv37QGLJJLqE5i1MY6TtGcbzic_G3hnuGi_PEf3kbelNiOiJDazzbPzg5vn7U8Sf-GX0EwQs0QOOJ42u2S88
    bRoAzCcG_4dQF0R-fYMJ3WDe56VxIXJM61sx16rBAZFndH6o3EhC-u5wkq9-zUneQkaUBIKf0I7LG_18oPNH6mOai7XY

    GOOGL API KEY 範例
    AIzaSyDJisryW54etkt4K8uzpIZEabhIKL3lvuQ
    -------------------------------------
Sender ID & Appliction ID & RegistationID & Google User Account & Sender Auth Token 規則介紹
    http://developer.android.com/google/gcm/gcm.html#arch
    *.it must have at least one logged in Google account if the device is running a version lower than Android 4.0.4
    *. The sender ID is used in the registration process to identify an Android application that is permitted to send messages to the device.
    *. a Registration ID is tied to a particular Android application running on a particular device.


Demo:
    GCM server (PHP)
    http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/
1.    db
2.    php
3.    android demo apk
gcm_server_php.7z
Android GCM Android GCM Reviewed by Wild on 3/08/2013 11:04:00 上午 Rating: 5

沒有留言:

沒有Google帳號也可發表意見唷!

技術提供:Blogger.