GVKun编程网logo

在Android上使用本地设备组的Google Cloud Messaging(GCM)提供HTTP错误代码401

23

这篇文章主要围绕在Android上使用本地设备组的GoogleCloudMessaging(GCM)提供HTTP错误代码401展开,旨在为您提供一份详细的参考资料。我们将全面介绍在Android上使用

这篇文章主要围绕在Android上使用本地设备组的Google Cloud Messaging(GCM)提供HTTP错误代码401展开,旨在为您提供一份详细的参考资料。我们将全面介绍在Android上使用本地设备组的Google Cloud Messaging(GCM)提供HTTP错误代码401,同时也会为您带来Agora Cloud Recording:错误404(Flutter + Google Cloud功能)、Android Google Cloud Messaging(GCM)和不匹配的发件人ID、Android Push Notifications using Google Cloud Messaging (GCM、android – Google Cloud Messaging(GCM) – 无法实例化接收器 – java.lang.ClassNotFoundException的实用方法。

本文目录一览:

在Android上使用本地设备组的Google Cloud Messaging(GCM)提供HTTP错误代码401

在Android上使用本地设备组的Google Cloud Messaging(GCM)提供HTTP错误代码401

我正在努力让Google Cloud Messaging(GCM)在Android上与本地设备组一起工作,如https://developers.google.com/cloud-messaging/android/client-device-group所述.

我在Web控制台(https://console.developers.google.com)中启用了以下服务:

>适用于Android的Google Cloud Messaging
> Google Cloud Pub / Sub
> Google Api

我还创建了以下凭据:

> API密钥(具有指定的包名称和SHA-1证书keyprint)
> OAuth 2.0客户端ID(Web应用程序)

我的Android代码如下所示(错误处理等已被删除):

String account = ACCOUNT_NAME; // E.g. "johndoe@google.com"
String scope = "audience:server:client_id:" + WEB_APPLICATION_CLIENT_ID;
String token = GoogleAuthUtil.getToken(context, account, scope);

// Token is successfully found here :-)

URL url = new URL("https://android.googleapis.com/gcm/notification");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("project_id", NUMERICAL_PROJECT_ID);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.connect();

JSONObject requestBody = new JSONObject();
requestBody.put("operation", "add");
requestBody.put("notification_key_name", "foobar");
requestBody.put("registration_ids", 
  new JSONArray(Arrays.asList(new String[]{"42", "44"})));
requestBody.put("id_token", token);

OutputStream os = connection.getoutputStream();
os.write(requestBody.toString().getBytes("UTF-8"));
os.close();

// connection.getResponseCode() is Now 401

提交的JSON到https://android.googleapis.com/gcm/notification看起来像这样:

{
  "operation": "add",
  "notification_key_name": "foobar",
  "registration_ids": ["42","44"],
  "id_token": "[veeeeeery long string]"
}

回复内容是:

<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

我究竟做错了什么?

解决方法:

找到诀窍:你使用谷歌帐户获取id_token,你需要使用确切的电子邮件作为notification_key_name.因此,如果您使用的是foo@gmail.com,则需要将此地址用作notification_key_name.

Agora Cloud Recording:错误404(Flutter + Google Cloud功能)

Agora Cloud Recording:错误404(Flutter + Google Cloud功能)

我在这里找到了document,为您报告的错误提供了一些背景信息。

“没有与这些值匹配的路由”:出现此消息的可能原因是您在请求中输入了错误的HTTP方法,例如在应为POST的情况下使用GET。另一个可能的原因是您将请求发送到错误的URL。

,

请确保通过Agora控制台在您的项目上启用云记录。

要在您的项目上启用Cloud Recording,请访问https://console.agora.io/cloud-recording,然后从左上角的下拉菜单中选择 Project 名称,然后点击 Duration em> Cloud Recording 下面的链接,然后单击按钮以在该项目上启用服务。

Screenshot of button to enable Agora Cloud Recording

单击启用云记录后,系统将提示您确认默认为50的并发通道设置,然后单击确定。启用后,您应该会看到使用情况图表。

Screenshot of Agora Cloud Recording usage graph enabled

Android Google Cloud Messaging(GCM)和不匹配的发件人ID

Android Google Cloud Messaging(GCM)和不匹配的发件人ID

我想在我的 Android应用程序中使用GCM服务.

为此,我使用了http://developer.android.com/guide/google/gcm/gcm.html的android文档

我创建了客户端注册过程与发件人ID等和服务器端应用程序,我使用注册ID和发件人ID发送邮件.

当我通过Eclipse在我的手机中安装应用程序时,推送通知工作正常,因此我拥有的发件人ID是正确的.

然后,当我用Eclipse导出apk文件并将其安装在我的手机中时,我收到SenderId错误的错误消息

MissmatchedSenderId

任何人都知道为什么我会得到这个.

我读过这些话题:

Why do I get “MismatchSenderId” from GCM server side?

When sending messages using GCM,I keep getting the response ‘MismatchSenderId’

但在我的情况下奇怪的是,在将应用程序导出为apk之前一切正常,然后我遇到了这个问题.

任何想法都是最好的.

解决方法

我实际上遇到了同样的问题,并且正在研究超过10个小时.

我终于找到了问题!
与服务器API密钥或浏览器API密钥或SenderID无关.
问题是Google文档:

final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this,SENDER_ID);
} else {
  Log.v(TAG,"Already registered");
}

Google表示你必须调用getRegistrationId函数并且只有当id为空时才调用register!
这对我来说根本不起作用……当我这样做时,我总是在发送到这个regId时找回MismatchSenderId.

我的解决方案是:
总是打电话

GCMRegistrar.register(this,SENDER_ID);

而当功能

protected void onRegistered( Context c,String regId )

被称为在我的服务器数据库中保存regId.

如果我这样做,一切正常!

Android Push Notifications using Google Cloud Messaging (GCM

Android Push Notifications using Google Cloud Messaging (GCM

http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ 按照链接中文章的步骤,实现了GCM-HTTP在client 和app server之间的部署,链接一下供需要的同学参考。 在从本地Server移植到web server遇

http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

按照链接中文章的步骤,实现了GCM-HTTP在client 和app server之间的部署,链接一下供需要的同学参考。


在从本地Server移植到web server遇到的问题:

1.Google Api Key是绑定服务器IP的,所以在本地换到Web的时候,Google Api Key需要重新申请;

2.如果服务器端CURL不可用,那么推送就会出问题,需要安装CURL模块

参考链接:

http://stackoverflow.com/questions/22004716/gcm-is-not-working-on-live-server

http://stackoverflow.com/questions/2939820/how-to-enable-curl-installed-ubuntu-lamp-stack

http://www.2cto.com/os/201102/84031.html

android – Google Cloud Messaging(GCM) – 无法实例化接收器 – java.lang.ClassNotFoundException

android – Google Cloud Messaging(GCM) – 无法实例化接收器 – java.lang.ClassNotFoundException

一旦应用程序启动或从服务器发送推送通知,就会发生以下回溯.
08-31 02:00:14.733: E/AndroidRuntime(30973): java.lang.RuntimeException: Unable to instantiate receiver com.example.smsmining.GcmbroadcastReceiver: java.lang.classNotFoundException: Didn't find class "com.example.smsmining.GcmbroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.example.smsmining-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.smsmining-1,/vendor/lib,/system/lib]]
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2405)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread.access$1500(ActivityThread.java:141)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.os.Looper.loop(Looper.java:137)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at java.lang.reflect.Method.invokeNative(Native Method)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at java.lang.reflect.Method.invoke(Method.java:525)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at dalvik.system.NativeStart.main(Native Method)
08-31 02:00:14.733: E/AndroidRuntime(30973): Caused by: java.lang.classNotFoundException: Didn't find class "com.example.smsmining.GcmbroadcastReceiver" on path: DexPathList[[zip file "/data/app/com.example.smsmining-1.apk"],/system/lib]]
08-31 02:00:14.733: E/AndroidRuntime(30973):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at java.lang.classLoader.loadClass(ClassLoader.java:501)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at java.lang.classLoader.loadClass(ClassLoader.java:461)
08-31 02:00:14.733: E/AndroidRuntime(30973):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2400)
08-31 02:00:14.733: E/AndroidRuntime(30973):    ... 10 more

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smsmining"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.INTERNET"/>

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


     <permission android:name="com.example.smsmining.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.smsmining.permission.C2D_MESSAGE" />

    <!-- This app has permission to register and receive data message. -->
    <uses-permission
        android:name="com.google.android.c2dm.permission.RECEIVE" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

         <receiver android:name="com.example.smsmining.SMSbroadcastReceiver">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>



         <receiver
            android:name=".GcmbroadcastReceiver"
            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.example.smsmining" />
            </intent-filter>
        </receiver>
        <service android:name=".GCMIntentService" />

    </application>

</manifest>

GcmbroadcastReceiver.java

package com.example.smsmining;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulbroadcastReceiver;


/**
 * This {@code WakefulbroadcastReceiver} takes care of creating and managing a
 * partial wake lock for your app. It passes off the work of processing the GCM
 * message to an {@code IntentService},while ensuring that the device does not
 * go back to sleep in the transition. The {@code IntentService} calls
 * {@code GcmbroadcastReceiver.completeWakefulIntent()} when it is ready to
 * release the wake lock.
 */

public class GcmbroadcastReceiver extends WakefulbroadcastReceiver {

    @Override
    public void onReceive(Context context,Intent intent) {
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),GcmIntentService.class.getName());
        // Start the service,keeping the device awake while it is launching.
        startWakefulService(context,(intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

GcmIntentService.java

public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private notificationmanager mnotificationmanager;
    NotificationCompat.Builder builder;

    public GcmIntentService() {
        super("GcmIntentService");
    }
    public static final String TAG = "GCM Demo";

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your broadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM will be
             * extended in the future with new message types,just ignore any message types you're
             * not interested in,or that you don't recognize.
             */
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " + extras.toString());
            // If it's a regular GCM message,do some work.
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i = 0; i < 5; i++) {
                    Log.i(TAG,"Working... " + (i + 1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG,"Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
                Log.i(TAG,"Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulbroadcastReceiver.
        GcmbroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg) {
        mnotificationmanager = (notificationmanager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this,new Intent(this,MainActivity.class),0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("GCM Notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mnotificationmanager.notify(NOTIFICATION_ID,mBuilder.build());
    }
}

解决方法

我有同样的错误.这对我有帮助.
尝试检查v.4库.
属性 – > Java构建路径 – >订单和导出

编辑:选择项目并转到:Properties-> Java Build Path-> Order and Export,确保在列表中选中了android.support.v4库,如果没有,请标记它. (我假设您已经将库导入到项目中),清理项目并且它应该可以工作

关于在Android上使用本地设备组的Google Cloud Messaging(GCM)提供HTTP错误代码401的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Agora Cloud Recording:错误404(Flutter + Google Cloud功能)、Android Google Cloud Messaging(GCM)和不匹配的发件人ID、Android Push Notifications using Google Cloud Messaging (GCM、android – Google Cloud Messaging(GCM) – 无法实例化接收器 – java.lang.ClassNotFoundException的相关知识,请在本站寻找。

本文标签: