17

Obecnie tworzę aplikację korzystającą z nowego systemu Google Cloud Messaging. Mam serwer klienta HTTP skonfigurowany w środowisku na żywo, aby umożliwić mi testowanie za pomocą moich urządzeń mobilnych, ale gdy kiedykolwiek spróbuję zarejestrować się w GCM, ciągle otrzymuję wyjątek IOException, który stwierdza, że ​​gcm.register() otrzymuje nieznane źródło, co powstrzymuje mnie przed otrzymaniem Identyfikator rejestracji Potrzebuję zezwolenia serwerowi na wysyłanie wiadomości do mojego urządzenia.Nowy interfejs API GCM Zarejestruj nieznany błąd źródła

Sprawdziłem kilka razy, aby sprawdzić, czy mój identyfikator nadawcy jest nieprawidłowy, a także sprawdziłem mój manifest, aby sprawdzić, czy nie mam również tego błędu i nie mogę znaleźć problemu. Rozglądanie się też nie daje żadnej odpowiedzi na mój problem.

Oto moja Log:

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) 

Oto mój Manifest:

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

Każda pomoc, aby naprawić to byłoby bardzo mile widziane.

Odpowiedz

27

To błąd otrzymujesz:

java.io.IOException: MAIN_THREAD 

Oznacza to, że są wywołanie metody Rejestry od głównego wątku, co jest niedozwolone. Powinieneś zadzwonić do niego z innego wątku.

public static final String ERROR_MAIN_THREAD

metody GCM blokują. Nie należy ich uruchamiać w głównym wątku ani w odbiornikach telewizyjnych. Stała wartość: "MAIN_THREAD"

Oto przykładowy kod z 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); 
} 
+0

To działało dziękuję bardzo doceniane – MarcD

+0

nie działać dla mnie, tworzę demo z Androidem Dokumentu jak wspomina powyżej, tworzę cały kod od strony dewelopera, ale wciąż mam ten sam wyjątek IOException ... –

8

1.Just stworzyć GCMHelper klasę, aby uzyskać CGM ID.

public final class GCMHelper { 

static GoogleCloudMessaging gcm = null; 

static Context context= null; 

public GCMHelper (Context context) 
{ 
    this.context= context; 
} 

public String GCMRegister (String SENDER_ID) throws Exception 
{ 
    String regid = ""; 
    //Check if Play store services are available. 
    if(!checkPlayServices()) 
     throw new Exception("Google Play Services not supported. Please install and configure Google Play Store."); 

    if (gcm == null) { 
     gcm = GoogleCloudMessaging.getInstance(context); 
    } 
    regid = gcm.register(SENDER_ID); 

    return regid; 
} 


private static boolean checkPlayServices() { 
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context); 
    if (resultCode != ConnectionResult.SUCCESS) { 
     return false; 
    } 
    return true; 
} 
} 

2.Then tworzyć GetGCM() metody w aktywności.

//**To avoid java.io.IOException: MAIN_THREAD**  

private void GetGCM() { 

    Thread thread = new Thread(new Runnable() { 

     @Override 
     public void run() { 
      try { 
       GCMHelper gcmRegistrationHelper = new GCMHelper (
      getApplicationContext()); 
       String gcmRegID = gcmRegistrationHelper.GCMRegister(SENDER_ID); 

      } catch (Exception bug) { 
       bug.printStackTrace(); 
      } 

     } 
    }); 

    thread.start(); 
} 

3.Then wezwanie GetGCM() metoda gdziekolwiek chcesz.

kodowanie 4.happy ...

+0

Działa .. Wielkie dzięki – asmgx

+0

Próbowałem wielu opcji, to działa! Dziękuję Ci bardzo. – Leon

Powiązane problemy