gpt4 book ai didi

android - Google Cloud Messaging 返回失败 : HTTP Status Code: 401

转载 作者:行者123 更新时间:2023-11-29 01:19:04 25 4
gpt4 key购买 nike

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

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

他们都回来了,

Failure: HTTP Status Code: 401 -Unauthorized

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

  1. Create Configuration File 将其添加到我的项目->app 文件夹
  2. 在主 Activity 中创建异步任务并编写以下代码。

    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 扩展了这个类

公共(public)类 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());
}

list 文件

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

请指导我这里做错了什么

最佳答案

添加此权限

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

关于android - Google Cloud Messaging 返回失败 : HTTP Status Code: 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37980694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com