2

I am using online tool to send Notification to my Device

  1. http://gcm-alert.appspot.com/
  2. http://techzog.com/development/gcm-notification-test-tool-android/

They both returns,

Failure: HTTP Status Code: 401 -Unauthorized

I have followed following steps to implement Google Clound Messaging API

  1. Create Configuration File added it in my Project->app Folder
  2. Created Async Task In Main Activity and write Following Code.

    SharedPreferences getPreference= getSharedPreferences("GCMToken", Context.MODE_PRIVATE); String tokenID= getPreference.getString("tokenID","");

        if(tokenID.isEmpty()) {
            InstanceID gcmTokenistanceID = InstanceID.getInstance(getApplicationContext());
            token = gcmTokenistanceID.getToken(getString(R.string.project_id),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
            GCMServiceURL= "MyWebServerURL.com/="+token;
            Log.i("TOKEN From IF Condition", token);
            saveTokenID(token);
    
  3. Then I created NotificationService and extend this class with GcmListenerService

public class GCMNotificationService extends GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("Test Message from Server");
    NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Title")
            .setContentText(message);
    notificationManager.notify(1, mBuilder.build());
}

Manifest File

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.app.myapp.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.app.myapp.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.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="com.app.myapp" />
    </intent-filter>
</receiver>


<service
    android:name="com.app.myapp.GCMNotificationService" android:exported="false" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    </intent-filter>
</service>

Kindly guide me what i am doing wrong here

dev90
  • 7,187
  • 15
  • 80
  • 153
  • how does your manifest look like? – CodeDaily Jun 23 '16 at 01:14
  • @SalvatoryBaya: I have updated my question – dev90 Jun 23 '16 at 01:19
  • how about the permissions? – CodeDaily Jun 23 '16 at 01:20
  • @SalvatoryBaya:Question Updated – dev90 Jun 23 '16 at 01:23
  • In permission i am providing Root package name, under myapp i have another folder `services` which contains NotificationClass ..Do i need to provide full package name or it is enough – dev90 Jun 23 '16 at 01:26
  • Try as my sample project at https://github.com/ngocchung/GCMAndroid/blob/master/app/src/main/AndroidManifest.xml – BNK Jun 23 '16 at 01:31
  • @Kirmani88 What type of API Key are you using? – AL. Jun 23 '16 at 01:33
  • I am using the senderId provided in Configuration File as API key – dev90 Jun 23 '16 at 06:24
  • @BNK :: Thanks for the Link. Will your project work if i try to send notification using it? – dev90 Jun 23 '16 at 06:49
  • https://github.com/ngocchung/GCMAndroid is my client app, it's working with https://github.com/ngocchung/gcmserver the server app, I am not sure with your server apps, you can try – BNK Jun 23 '16 at 06:54
  • @BNK : I have import your project, added a new configuration file and tried it running on online `GCM-Notification-Tools`, Its still giving me `Unauthorized Error 401`. I changed nothing in your code, Just add configuration file and a key. Kindly guide me – dev90 Jun 23 '16 at 07:04
  • I have tried with `http://gcm-alert.appspot.com/`, my client app can receive the message. I think you should provide the correct information, please take a look at my answer at http://stackoverflow.com/questions/33489862/google-gcm-token-vs-registration-id/33490670#33490670 – BNK Jun 23 '16 at 07:31
  • @BNK : I have asked a question, kindly review it http://stackoverflow.com/questions/37984993/google-cloude-messaging-returns-error-401-unauthorized – dev90 Jun 23 '16 at 07:33

1 Answers1

1

Add this permission

<uses-permission android:name="android.permission.INTERNET" />
AL.
  • 36,815
  • 10
  • 142
  • 281
CodeDaily
  • 756
  • 4
  • 17