Google Cloud Messaging返回失败:HTTP状态代码:401

时间:2016-06-23 01:12:20

标签: android google-cloud-messaging

我正在使用在线工具将通知发送到我的设备

  1. http://gcm-alert.appspot.com/
  2. http://techzog.com/development/gcm-notification-test-tool-android/
  3. 他们都返回,

      

    失败:HTTP状态代码:401 -Unauthorized

    我已按照以下步骤实施Google Clound Messaging API

    1. 创建配置文件将其添加到我的Project-> app文件夹
    2. 在主要活动中创建Async Task并编写以下代码。

      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. 然后我创建了NotificationService并使用GcmListenerService扩展此类

    4. 公共类GCMNotificationService扩展了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());
      }
      

      清单文件

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

      请指导我在这里做错了什么

1 个答案:

答案 0 :(得分:1)

添加此权限

<uses-permission android:name="android.permission.INTERNET" />