Android implement Push Notification system

Costas

Administrator
Staff member
start points :
http://developer.android.com/google/gcm/index.html
http://developer.android.com/google/gcm/gs.html
First, install ‘Google Play services’ through SDK Manager.exe is under Extras

snap278s.jpg


This one will download a library project called google-play-services_lib exists at :

C:\you_androidSDK_path\extras\google\google_play_services\libproject

Go  to eclipse and import this library project!

snap279s.jpg


snap282s.jpg


Warning you must check ‘Copy project into workspace’

otherwise will not work because the path is too long.

after succefully import, goto library manifest and replace with this tag

JavaScript:
    <uses-sdk android:minSdkVersion="16"/>

restart eclipse!

Sign in with your google account to https://cloud.google.com/console

And create a new Project, you will get a Project ID and a  Project Number

We will use the Project Number later as sender-id..

snap274s.jpg


Enable the Google Cloud Messaging for Android (only this needed to be turned on) at API

snap275s.jpg


[update 23/1/2014]
Google Developer Console change, there is a secret when creating ServerKey!

snap575.png


when you are here and Press 'Create New Key' 1st modal appears asking for which purpose when choosing Server 2nd modal appears asking to

write your server IP! dont do anything! just leave black the editbox and press Create!!!!  live your myth with GOOGLE!

FYI :

Registered Apps made Credentials

--

new sample -- https://github.com/GoogleCloudPlatform/solutions-mobile-backend-starter-android-client

[update 23/1/2014]

Goto Registered Apps and create a new one choosing platform Web Application. will we use later the Server Key - Api Key at PHP implementation

snap320.png


snap321.png


Goto Registered Apps and create a new one.

snap277s.jpg


You can find the sign-key SHA1 at Eclipse > Window > Preferences (warning this is for debug only, you have to update it, when make the real build)

snap276s.jpg


In general you reveal it also with a simple command line (when ask for password type android) :

keytool -exportcert -alias androiddebugkey -keystore C:\Users\you_user_name\.android\debug.keystore -list –v
pause

We create a test android project

snap273.png


That makes a reference  to  library project called google-play-services_lib (that already imported into workspace)

We select the new project > r-click > properties

snap288s.jpg


snap289s.jpg


Press ok, you will see the green tick.

snap290s.jpg


So now the new project has dependencies right?

snap291.png


In next we have to set the proper permissions in manifest file :

As described at http://developer.android.com/google/gcm/client.html so I add these lines

JavaScript:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    

    <uses-permission android:name="com.example.toto.permission.C2D_MESSAGE" />

Next we go to

http://code.google.com/p/gcm/source/browse/gcm-client/src/com/google/android/gcm/demo/app/DemoActivity.java

and copy & paste the code!

First the public vars :

JavaScript:
    public static final String EXTRA_MESSAGE = "message";
    public static final String PROPERTY_REG_ID = "registration_id";
    private static final String PROPERTY_APP_VERSION = "appVersion";
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;

    /**
     * Substitute you own sender ID here. This is the project number you got
     * from the API Console, as described in "Getting Started."
     */
    String SENDER_ID = "Your-Sender-ID";

    /**
     * Tag used on log messages.
     */
    static final String TAG = "GCM Demo";

    TextView mDisplay;
    GoogleCloudMessaging gcm;
    AtomicInteger msgId = new AtomicInteger();
    Context context;

    String regid;

After paste > press CTRL+SHIFT+O to make the auto import, replace SENDER_ID variable with your application Project Number

Then the other procedures, ok done, first run :

Snap292s.jpg


Oops I got :

snap293.png
wtf? google doesnt say anything about this!?

Go to manifest and paste this inside application tag!

JavaScript:
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

snap295s.jpg


Re-run! Works!

Snap296n.png


---------------------------------------------------

Continuing to implement the WakefulBroadcastReceiver (GcmBroadcastReceiver)
http://code.google.com/p/gcm/source/browse/gcm-client/src/com/google/android/gcm/demo/app/GcmBroadcastReceiver.java

I copy the source to my application, but I got an error in imports about

android.support.v4.content.WakefulBroadcastReceiver

seems need

C:\androidSDK_path\extras\android\support\v4\android-support-v4.jar

Fire up SDK.exe and download :

snap299s.jpg


Then drop it to folder libs

snap300.png


then I go to my application manifest and merge from (http://developer.android.com/google/gcm/client.html)

JavaScript:
        <receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.toto" />
            </intent-filter>
        </receiver>
        <service android:name=".GcmIntentService" />

Continuing to implement the IntentService (GcmIntentService)

http://code.google.com/p/gcm/source/browse/gcm-client/src/com/google/android/gcm/demo/app/GcmIntentService.java

---------------------------------------------------

now, after you get a deviceID (aka use your mobile), create a sample PHP file using curl to post variables at google server, you first test!

JavaScript:
<?php
// Replace with real SERVER key > API key from Google Cloud Console
$apiKey = "xx-03HLEiyj1_l9Aim3I2Gmps";

// Replace with real client registration IDs (aka mobile)
$registrationIDs = array( "asddasdQDD9GoCQyAoz-vH2rqstB5zIIl7UoMCvJ-90vWmyMXBH8T5EcvYtI3JnuwDIeN9w" );

// Message to be sent
$message = "pipiscrew";

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
                'registration_ids'  => $registrationIDs,
                'data'              => array( "message" => $message ),
                );

$headers = array(
                    'Authorization: key=' . $apiKey,
                    'Content-Type: application/json'
                );

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );

curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);

echo $result;
?>

workssss!

--stop reading-------OLD before using Registered Apps > Server--------stop reading----------OLD before using Registered Apps > Server---

All done going for PHP Server implementation, after some 0's
Unauthorized

Error 401

I realize the server have to be on whitelist :
1-
https://www.google.com/webmasters/tools
2-
Google Cloud Console
API & Auth > Notification endpoints

references :
http://developer.android.com/google/gcm/http.html
http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging
http://distriqt.com/post/1223
http://www.robertprice.co.uk/robblog/2013/02/posting-json-to-a-web-service-with-php/
http://github.com/Redth/PushSharp
http://twisted.dyndns.tv:3194/DevForums/viewtopic.php?t=63#p181

Note: If your organization has a firewall that restricts the traffic to or from the Internet, you need to configure it to allow connectivity with GCM in order for your Android devices to receive messages. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM doesn't provide specific IPs, so you should allow your firewall to accept outgoing connections to all IP addresses contained in the IP blocks listed in Google's ASN of 15169.

**************************************** NOOOOOOOOOOOOOOOOOO

the only solution is to go at Google old console mode, by going to https://code.google.com/apis/console/b/0/?noredirect

or by clicking Go back

snap309.png


then there, click the Create new Project big button!

snap312.png


now the Project Number is in URL

snap3101.png


then natigate to API Access to get the API key for PHP + manipulate Notification Endpoints
snap3131.png


and dont forget to enable Google Cloud Messaging for Android under Services

snap314.png
 
Top