2. Push and Pull Message
What is push message?
◦ Server 主動推送訊息到app端。
What is pull message?
◦ App 主動向Server 索取需要的訊息。
Why push is more difficult than pull?
◦ The IP address of each mobile device always change.
◦ Therefore, if we want to solve this problem, we have to set timer to connect to server periodically.
Fortunately, there are some useful tools to deal with this problem.
◦ Ex. Google cloud messaging
4. GCM overview
Google Cloud Messaging (GCM) is a service that allows
◦ Send data from your server to your Android device
◦ Receive messages from devices on the same connection.
It can deal with push message problem!
5. Two kinds of messages
Collapsible message
◦ May be replaced by a new message containing the same collapse key.
◦ Only the most recent message is relevant.
◦ Ex. Send-to-sync messages
Non-collapsible message
◦ Each individual message is delivered to the device.
◦ Ex. IM
6. Key Concepts
Components
◦ Client App
◦ 3rd-party Application Server
◦ GCM Connection Servers
Credentials
◦ Sender ID - Google API project ID
◦ Application ID – App’s Package name
◦ Registration ID – Issued by the GCM servers to the Apps. (Each device has one reg_id)
◦ Google User Account
◦ Sender Auth Token - API Key. Use as password in server
8. GCM Client
Use GoogleCloudMessage API
◦ Setup google play service SDK in the project.
Modify Manifest file
◦ Allow some permission (like internet…)
◦ Set a Receiver to receive GCM’s broadcast
◦ Set a Service to handle GCM message
Register for GCM
◦ Each device needs to register a unique registration ID.
◦ Let server keep the Reg id.
You can also implement
◦ User notification
◦ …
9. GCM Server
Two kinds of connections:
◦ GCM HTTP
◦ Downstream only: cloud-to-device.
◦ Synchronous messaging
◦ HTTP POST
◦ CCS(Cloud Connection Server, XMPP)
◦ Upstream and downstream (device-to-cloud, cloud-to-device).
◦ Asynchronous messaging
◦ JSON in XMPP message
Use device’s registration ID to send message.
10. GCM samples
單向:
Gmail : send notification after new mails arrived.
交通公具app:Server當車在進站前五分鐘、十分鐘時,提前通知使用者到站等候。
14. Comparison
GCM:
◦ Pros: Easy to develop. GCM API can be simply used in Android Studio. (Google play service)
◦ Can send message in both direction.
◦ Cons: GCM cannot be used in China.
百度雲:
◦ 優點:解決在中國無法使用的問題。
◦ 缺點:僅能單向傳遞訊息。
◦ 其實不太容易import到自己的project中。
A collapsible message is a message that may be replaced by a new message containing the same collapse key if it has yet to be delivered to the device.A non-collapsible message implies that each individual message is delivered to the device.
三個東西:1.我們的app 2.我們的server 3.gcm server幾個認證的token:特別要說reg id,每台手機就都有一份reg id,而server就是透過reg id 來傳給對應的device。
也就是說,其實真的在實作時,在server端對每一台device都應該要有一個欄位存相對應的reg id table。