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: .