0
votes

I want to receive a push notification from my server to my android app.Im using example from Google Cloud Messaging for Android Library extras/google/samples/ .I've been googling 3 days for helps but without hope :/.

This is how I proceeded:

You will find the source code at the end

I-server part:

1-I open gcm-demo-server\WebContent\WEB-INF\classes\api with block note. I changed it with my api key

2-in a shell window in gcm-demo-server directory i run "ant war"

3-I copy dist\gcm-demo.war file to tomcat\webapps

4-I open the server in a browser with this url localhost:8080/gcm-demo

II-App part:

In CommonUtilities.java I changed the following fields:

1- static final String SERVER_URL = "localhost:8080/gcm-demo/home";(my server url)

2- static final String SENDER_ID = "876836784656"; (my project id)

After that I open the server. it shows "No device registred", then I run the app it shows "device is already registred on the server" but when I refresh my server it shows "No device registred", however my server must shows registred devices and a button to send "push notification" :cry: PLEAAAZ help!!

App code: - The source code and classes are HERE

  • The Manifest:

    <?xml version="1.0" encoding="utf-8"?>
    

    <!-- GCM requires Android SDK version 2.2 (API level 8) or above. -->
    <!-- The targetSdkVersion is optional, but it's always a good practice
         to target higher versions. -->
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/>
    
    <!-- GCM connects to Google Services. -->
    <uses-permission android:name="android.permission.INTERNET" />
    
    <!-- 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" />
    
    <!--
     Creates a custom permission so only this app can receive its messages.
    
     NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
           where PACKAGE is the application's package name.
    -->
    <permission
        android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission
        android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" />
    
    <!-- This app has permission to register and receive data message. -->
    <uses-permission
        android:name="com.google.android.c2dm.permission.RECEIVE" />
    
    <!-- Main activity. -->
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".DemoActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    
        <!--
          BroadcastReceiver that will receive intents from GCM
          services and handle them to the custom IntentService.
    
          The com.google.android.c2dm.permission.SEND permission is necessary
          so only GCM services can send data messages for the app.
        -->
        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.google.android.gcm.demo.app" />
            </intent-filter>
        </receiver>
    
        <!--
          Application-specific subclass of GCMBaseIntentService that will
          handle received messages.
    
          By default, it must be named .GCMIntentService, unless the
          application uses a custom BroadcastReceiver that redefines its name.
        -->
        <service android:name=".GCMIntentService" />
    </application>
    

Server code:

-All classes are HERE

I want to thank everyone how takes a look to this topic :oops: .

1
The behaviour is expected because you are not persisting the list of registered devices on your web server. Every time you restart your web server, you have to register it again, so that it gets stored in some in-memory object(like an array). Adding database support to your server app should resolve the problem.faizal

1 Answers

0
votes

I had issues when I tried that demo because my tomcat was running locally, but the emulator has like a "virtual router" per say so I can't access my tomcat server just using http://localhost:8080 from my android app. I'd have to use the IP of my machine running the Tomcat server, so it'd be like http://[my-machine's ip]:8080/gcm-demo where [my-machine's ip] is something like this 192.168.1.10

NOTE: You can get the IP by running ifconfig on Linux or MacOS, or ipconfig on Windows. the ip adress is the value of IPv4 field.