android - 新 GCM API 注册未知来源错误

标签 android push-notification ioexception google-cloud-messaging

我目前正在构建一个使用新的 Google Cloud Messaging 系统的应用程序。我在实时环境中设置了一个 HTTP 客户端服务器,以允许我通过我的移动设备进行测试,但是当我尝试向 GCM 注册时,我不断收到一个 IOException,指出 gcm.register() 正在接收未知来源,阻止我获取注册 ID,我需要允许我的服务器向我的设备发送消息。

我已经检查了几次以查看我的发件人 ID 是否错误,我还检查了我的 list 以查看我是否也有错误,但我似乎找不到问题。环顾四周也没有为我的问题提供任何答案。

这是我的日志:

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)

这是我的 list :

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

最佳答案

这就是你得到的错误:

java.io.IOException: MAIN_THREAD

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

public static final String ERROR_MAIN_THREAD

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

这是来自 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);
}

关于android - 新 GCM API 注册未知来源错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17721541/

相关文章:

java - 我应该关闭从适配器获取的光标吗?

Android:循环浏览图片并将每个图片设置为背景

javascript - 使用哪个 Node 模块或 JS 代码才能将推送通知(通过 FCM)发送到 Android 设备?

android - 是否可以从设备向服务器发送推送通知请求?

swift - 应用程序未运行时的远程推送通知崩溃

Android 多点触控 : ACTION_UP not always called?

java - android webview 无连接错误

java - Android Freebase 查询 IOException

c# - 如何像 HttpWebRequest 一样使用 TcpClient 执行 HTTPS?

.net - 除了解析 .message 属性之外,是否有可靠的方法来识别由锁定文件引起的 IOexceptions?