Tutorial Implementation Google Cloud Messaging (GCM) with Django


Setup GCM for Android Client and Django in Server require efforts to know how it’s works. Here are simple steps to setup the newest GCM (Google Cloud Messaging) in Android.

To understand about GCM, the flow is :

1. Create a Google APIs GCM project, which generates SENDER_ID and API_KEY for server implementation.

2. An Android app registers the device its running on with GCM, using PROJECT_ID, which generates a registration id. The app should send registration ID to the server.

3. The project’s server code can now send a broadcast message to any registered devices by communicating with the GCM service, using the API KEY.

For the implementation:

1. Dependencies
Update your ADT and Google Play Service packages to be able using the latest GCM. Make sure to import google-play-services lib into your project libraries.

2. AndroidManifest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!– GCM requires a Google account. –>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!– Keeps the processor from sleeping when a message is received. –>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


    <permission
        android:name="com.polatic.hospitrack.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.polatic.hospitrack.gcm.permission.C2D_MESSAGE" />

    ….



        <service
            android:name="com.polatic.hospitrack.network.GCMListenerInstanceID"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>

        <service
            android:name="com.polatic.hospitrack.network.GCMListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.polatic.hospitrack" />
            </intent-filter>
        </receiver>

        <service
            android:name="com.polatic.hospitrack.network.GCMRegistrationIntentService"
            android:exported="false" >
        </service>

In Django Server, we send the information.

Reference:
https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.