1

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

  1. http://gcm-alert.appspot.com/
  2. http://techzog.com/development/gcm-notification-test-tool-android/
  3. 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"
        }
dev90
  • 7,187
  • 15
  • 80
  • 153
  • 1
    try this answer...............http://stackoverflow.com/a/21457262/3678308 – Muhammad Waleed Jun 23 '16 at 07:38
  • I have just sent GCM message with my API key to your reg id, sucessfully, have your app got it? I think you should check your API key – BNK Jun 23 '16 at 07:39
  • @BNK : I haven't received any message, I have added my configuration file in the question, Kindly check it – dev90 Jun 23 '16 at 07:58
  • Try putting `Log.i("From GCM API", "Received: " + data.toString());` inside `onMessageReceived`, after that, I will try send you again – BNK Jun 23 '16 at 08:07

0 Answers0