【问题标题】:Google Cloud Messaging returns Failure: HTTP Status Code: 401Google Cloud Messaging 返回失败:HTTP 状态代码:401
【发布时间】:2016-06-23 01:12:19
【问题描述】:

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

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

他们都回来了,

失败:HTTP 状态代码:401 - 未经授权

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

  1. 创建配置文件将其添加到我的项目->应用程序文件夹中
  2. 在 Main Activity 中创建 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 扩展了这个类

公共类 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>

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

【问题讨论】:

  • 您的清单是什么样的?
  • @SalvatoryBaya:我已经更新了我的问题
  • 权限如何?
  • @SalvatoryBaya:问题已更新
  • 许可我提供根包名,在myapp下我有另一个文件夹services,其中包含NotificationClass ..我需要提供完整的包名还是足够了

标签: android google-cloud-messaging


【解决方案1】:

添加此权限

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2011-03-18
    相关资源
    最近更新 更多