I am getting HTTP Error 401: Unauthorized
Message when i try to test and get notifications from Online GCM Tester
. I am using this example to implement Google Cloud Messaging
I tested my app on following Testers
- http://gcm-alert.appspot.com/
- http://techzog.com/development/gcm-notification-test-tool-android/
- http://www.pushwatch.com/gcm/
My API key is : AIzaSyB2u46YIQ22zAgbxUNP4Y0UuQdAUroc6jM
Device Token ID: eWoZ_ngtrLA:APA91bHC0DEfgYb8QDD3AhGXtuRAv0oW9gUCH1-LvkjRzAlgTTBYbtIEbt1ux2rPylGItXKYL1fGvgRRZhRviFdQ4N1Hp8RFgr2w2g8tqjZw9E8ymcQXRIuKVCjRrwzjUKl1Wmvot-WQ
Manifest File
<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" />
<permission android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<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.REGISTRATION" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<service android:name=".GcmService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
GCM LISTNER
public class GcmService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
JSONObject jsonObject = new JSONObject();
Set<String> keys = data.keySet();
for (String key : keys) {
try {
jsonObject.put(key, data.get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
Main Activity
private void getGCMToken() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
InstanceID instanceID = InstanceID.getInstance(mContext);
String token = instanceID.getToken(SENDER_ID, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
if (token != null && !token.isEmpty()) {
SharedPreferences appPrefs = mContext.getSharedPreferences(SHARD_PREF, Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = appPrefs.edit();
prefsEditor.putString(GCM_TOKEN, token);
prefsEditor.apply();
}
Configuration File
project_info": {
"project_number": "1005995697870",
"project_id": "default-demo-app-2e614"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:1005995697870:android:f36173e948059ccb",
"android_client_info": {
"package_name": "gcm.play.android.samples.com.gcmquickstart"
}
},
"oauth_client": [],
"api_key": [
{
"current_key": "AIzaSyB2u46YIQ22zAgbxUNP4Y0UuQdAUroc6jM"
}