新GCM API注册未知源错误错误、GCM、API

2023-09-06 07:45:28 作者:花落,淚無聲

我目前正在建设一个使用新的谷歌云消息传递系统中的应用。我已成立了一个现场环境HTTP客户端服务器,让我来测试通过我的移动设备,但是当过我尝试与GCM我一直得到一个IOException异常,指出来注册 gcm.register( )正在接受来源不明的,从获得的注册ID我需要让我的服务器将邮件发送到我的设备阻止我。

I am currently building an application that uses the new Google Cloud Messaging system. I have a HTTP client server set up on a live environment to allow me to test through my mobile devices, but when ever I try to register with the GCM I keep getting a IOException that states that gcm.register() is receiving an Unknown Source stopping me from getting the Registration Id I need to allow my server send messages to my device.

我已经检查了好几次,看看我的发件人ID是错误的,我检查了我的表现,以及看看我有错,以及和我似乎无法发现问题。环顾四周也没有提供任何回答我的问题。

I have checked several times to see if my Sender Id is wrong and I have checked over my manifest as well to see if I have that wrong as well and I cannot seem to find the problem. Looking around also hasn't provided any answer to my problem.

下面是我的记录:

07-18 11:34:25.487: W/System.err(3093): java.io.IOException: MAIN_THREAD
07-18 11:34:25.497: W/System.err(3093):     at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
07-18 11:34:25.497: W/System.err(3093):     at koodoo.pushtest.MainActivity.onCreate(MainActivity.java:94)
07-18 11:34:25.497: W/System.err(3093):     at android.app.Activity.performCreate(Activity.java:4492)
07-18 11:34:25.497: W/System.err(3093):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-18 11:34:25.497: W/System.err(3093):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-18 11:34:25.497: W/System.err(3093):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-18 11:34:25.497: W/System.err(3093):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-18 11:34:25.497: W/System.err(3093):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-18 11:34:25.497: W/System.err(3093):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 11:34:25.497: W/System.err(3093):     at android.os.Looper.loop(Looper.java:137)
07-18 11:34:25.497: W/System.err(3093):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-18 11:34:25.497: W/System.err(3093):     at java.lang.reflect.Method.invokeNative(Native Method)
07-18 11:34:25.497: W/System.err(3093):     at java.lang.reflect.Method.invoke(Method.java:511)
07-18 11:34:25.497: W/System.err(3093):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-18 11:34:25.497: W/System.err(3093):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-18 11:34:25.497: W/System.err(3093):     at dalvik.system.NativeStart.main(Native Method)

下面是我的清单:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <!-- GCM connects to Internet Services. -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

    <!-- Keeps the processor from sleeping when a message is received. -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- Creates a custom permission so only this app can receive its messages. -->
    <permission
        android:name="koodoo.pushtest.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="koodoo.pushtest.permission.C2D_MESSAGE" />

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

    <!-- Network State Permissions to detect Internet status -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <!-- Permission to vibrate -->
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="koodoo.pushtest.MainActivity"
            android:label="@string/app_name" >

        </activity>

        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>

                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="koodoo.pushtest" />
            </intent-filter>
        </receiver>

        <service android:name="com.google.android.gcm.GCMIntentService" />

        <activity
            android:name="koodoo.pushtest.RegisterActivity"
            android:label="@string/title_activity_register" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

任何帮助解决这将是极大的AP preciated。

Any help to fix this would be greatly appreciated.

推荐答案

这就是你所得到的错误:

That's the error you are getting :

java.io.IOException: MAIN_THREAD

这意味着你正在调用从主线程,这是不允许的注册方法。你应该从另一个线程调用它。

It means you are calling the register method from the main thread, which is not allowed. You should call it from another thread.

公共静态最后弦乐ERROR_MAIN_THREAD

public static final String ERROR_MAIN_THREAD

GCM方法阻止。你不应该在主线程或广播接收机运行它们。   常数值:MAIN_THREAD

GCM methods are blocking. You should not run them in the main thread or in broadcast receivers. Constant Value: "MAIN_THREAD"

下面是从 GCM文档一个code样品:

Here's a code sample from the GCM documentation :

/**
 * Registers the application with GCM servers asynchronously.
 * <p>
 * Stores the registration id, app versionCode, and expiration time in the 
 * application's shared preferences.
 */
private void registerBackground() {
    new AsyncTask() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }
                regid = gcm.register(SENDER_ID);
                msg = "Device registered, registration id=" + regid;

                // You should send the registration ID to your server over HTTP,
                // so it can use GCM/HTTP or CCS to send messages to your app.

                // For this demo: we don't need to send it because the device
                // will send upstream messages to a server that echo back the message
                // using the 'from' address in the message.

                // Save the regid - no need to register again.
                setRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            mDisplay.append(msg + "\n");
        }
    }.execute(null, null, null);
}