GCMRegistrat.getRegistrationId(this)返回一个空字符串

时间:2013-03-07 08:53:45

标签: registration google-cloud-messaging

我知道网上有很多关于这个主题的类似问题,但我觉得我做的每个人都在说。当我说

final String regId = GCMRegistrar.getRegistrationId(this);

它回馈了一个amty字符串。但是后来在我的LogCat中,它声明设备已经注册了一个regId。

这是我的代码:

PushAndroidActivity.java

package com.w.v;

import static com.w.v.CommonUtilities.SENDER_ID;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.w.v.ServerUtilities;
import com.google.android.gcm.GCMRegistrar;

public class PushAndroidActivity extends Activity {

private String TAG = "** pushAndroidActivity **";
private TextView mDisplay;
AsyncTask<Void, Void, Void> mRegisterTask;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    checkNotNull(SENDER_ID, "SENDER_ID");

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);


    Intent i = getIntent();
    String name         = i.getStringExtra("namepass");
    String email        = i.getStringExtra("emailpass");
    String password     = i.getStringExtra("passwordpass");
    String id           = i.getStringExtra("idpass");


    //setContentView(R.layout.main);
    //mDisplay = (TextView) findViewById(R.id.display);

        final String regId = GCMRegistrar.getRegistrationId(this);
            Log.i(TAG, "registration id =====  "+regId);

            if (regId.equals("")) {
                GCMRegistrar.register(this, SENDER_ID);
                Toast.makeText(getApplicationContext(), "Registered with GCM", Toast.LENGTH_LONG).show();
                Log.d("SENDER_ID =",SENDER_ID);
            } else {
                // Device is already registered on GCM
                if (GCMRegistrar.isRegisteredOnServer(this)) {
                    // Skips registration.              
                    Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
                } else {
                    // Try to register again, but not in the UI thread.
                    // It's also necessary to cancel the thread onDestroy(),
                    // hence the use of AsyncTask instead of a raw thread.
                    final Context context = this;
                    mRegisterTask = new AsyncTask<Void, Void, Void>() {

                        @Override
                        protected Void doInBackground(Void... params) {
                            // Register on our server
                            // On server creates a new user

                            Intent i = getIntent();
                            String name         = i.getStringExtra("namepass");
                            String email        = i.getStringExtra("emailpass");
                            String password     = i.getStringExtra("passwordpass");
                            String id           = i.getStringExtra("idpass");

                            ServerUtilities.register(context, name, email, regId);

                            Log.d("name",name);
                            Log.d("email",email);
                            Log.d("regId",regId);

                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void result) {
                            mRegisterTask = null;
                        }

                    };
                    mRegisterTask.execute(null, null, null);
                }
            }

            Intent dashboard = new Intent(getApplicationContext(), WebViewActivity.class);
                dashboard.putExtra("namepass", name);
                dashboard.putExtra("emailpass", email);
                dashboard.putExtra("passwordpass", password);
                dashboard.putExtra("idpass", id);
                dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(dashboard);

            //mDisplay.setText("ffffff        "+regId);
        }

    private void checkNotNull(Object reference, String name) {
        if (reference == null) {
            throw new NullPointerException(getString(R.string.error_config, name));
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        GCMRegistrar.unregister(this);
    }
}

GCMIntentService.java

package com.w.v;

import static com.w.v.CommonUtilities.SENDER_ID;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentService extends GCMBaseIntentService{

    public GCMIntentService() {
        super(SENDER_ID);
    }

    private static final String TAG = "===GCMIntentService===";


    @Override
    protected void onRegistered(Context arg0, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
    }

    @Override
        protected void onUnregistered(Context arg0, String arg1) {
        Log.i(TAG, "unregistered = "+arg1);
    }

    @Override
        protected void onMessage(Context arg0, Intent arg1) {
        Log.i(TAG, "new message= ");
    }

    @Override
        protected void onError(Context arg0, String errorId) {
        Log.i(TAG, "Received error: " + errorId);
    }

    @Override
        protected boolean onRecoverableError(Context context, String errorId) {
        return super.onRecoverableError(context, errorId);
    }
}

在android 4.2.2中运行应用程序时的LogCat

03-07 09:39:29.001: W/Bundle(20972): Key idpass expected String but value was a java.lang.Integer.  The default value <null> was returned.
03-07 09:39:29.001: W/Bundle(20972): Attempt to cast generated internal exception:
03-07 09:39:29.001: W/Bundle(20972): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
03-07 09:39:29.001: W/Bundle(20972):    at android.os.Bundle.getString(Bundle.java:1069)
03-07 09:39:29.001: W/Bundle(20972):    at android.content.Intent.getStringExtra(Intent.java:4302)
03-07 09:39:29.001: W/Bundle(20972):    at com.w.v.PushAndroidActivity.onCreate(PushAndroidActivity.java:36)
03-07 09:39:29.001: W/Bundle(20972):    at android.app.Activity.performCreate(Activity.java:5104)
03-07 09:39:29.001: W/Bundle(20972):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-07 09:39:29.001: W/Bundle(20972):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-07 09:39:29.001: W/Bundle(20972):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-07 09:39:29.001: W/Bundle(20972):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-07 09:39:29.001: W/Bundle(20972):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-07 09:39:29.001: W/Bundle(20972):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-07 09:39:29.001: W/Bundle(20972):    at android.os.Looper.loop(Looper.java:137)
03-07 09:39:29.001: W/Bundle(20972):    at android.app.ActivityThread.main(ActivityThread.java:5041)
03-07 09:39:29.001: W/Bundle(20972):    at java.lang.reflect.Method.invokeNative(Native Method)
03-07 09:39:29.001: W/Bundle(20972):    at java.lang.reflect.Method.invoke(Method.java:511)
03-07 09:39:29.001: W/Bundle(20972):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-07 09:39:29.001: W/Bundle(20972):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-07 09:39:29.001: W/Bundle(20972):    at dalvik.system.NativeStart.main(Native Method)
03-07 09:39:29.001: I/** pushAndroidActivity **(20972): registration id =====  
03-07 09:39:29.001: D/GCMRegistrar(20972): resetting backoff for com.w.v
03-07 09:39:29.001: V/GCMRegistrar(20972): Registering app com.w.v of senders 611573915552
03-07 09:39:29.016: D/SENDER_ID =(20972): 611573915552
03-07 09:39:29.040: D/GCMRegistrar(20972): resetting backoff for com.w.v
03-07 09:39:29.040: V/GCMRegistrar(20972): Unregistering app com.w.v
03-07 09:39:29.079: D/dalvikvm(20972): GC_CONCURRENT freed 579K, 6% free 11104K/11712K, paused 4ms+4ms, total 45ms
03-07 09:39:29.133: D/webviewglue(20972): nativeDestroy view: 0x5cf71940
03-07 09:39:29.211: V/GCMBroadcastReceiver(20972): onReceive: com.google.android.c2dm.intent.REGISTRATION
03-07 09:39:29.211: V/GCMBroadcastReceiver(20972): GCM IntentService class: com.w.v.GCMIntentService
03-07 09:39:29.211: V/GCMBaseIntentService(20972): Acquiring wakelock
03-07 09:39:29.297: V/GCMBaseIntentService(20972): Intent service name: GCMIntentService-611573915552-5
03-07 09:39:29.297: D/GCMBaseIntentService(20972): handleRegistration: registrationId = APA91bFJW_CNYh5AYKomkUPvVGvQay0WgL_GH8DdoxooBXcJSo1ryONrB4VcsRysYedx-oKjgDiPSzhrXR6s8HBH7pIl47SvYR5chuJtJWlymZVRm8j7E77F3TirMqF3ag5D8lbOScTVzzA6ZqWMhTDdiAYPkYc5Hw, error = null, unregistered = null
03-07 09:39:29.297: D/GCMRegistrar(20972): resetting backoff for com.w.v
03-07 09:39:29.297: V/GCMBroadcastReceiver(20972): onReceive: com.google.android.c2dm.intent.REGISTRATION
03-07 09:39:29.297: V/GCMBroadcastReceiver(20972): GCM IntentService class: com.w.v.GCMIntentService
03-07 09:39:29.297: V/GCMBaseIntentService(20972): Acquiring wakelock
03-07 09:39:29.297: V/GCMRegistrar(20972): Saving regId on app version 2
03-07 09:39:29.321: I/===GCMIntentService===(20972): Device registered: regId = APA91bFJW_CNYh5AYKomkUPvVGvQay0WgL_GH8DdoxooBXcJSo1ryONrB4VcsRysYedx-oKjgDiPSzhrXR6s8HBH7pIl47SvYR5chuJtJWlymZVRm8j7E77F3TirMqF3ag5D8lbOScTVzzA6ZqWMhTDdiAYPkYc5Hw
03-07 09:39:29.321: V/GCMBaseIntentService(20972): Releasing wakelock
03-07 09:39:29.329: D/GCMBaseIntentService(20972): handleRegistration: registrationId = null, error = null, unregistered = com.w.v
03-07 09:39:29.329: D/GCMRegistrar(20972): resetting backoff for com.w.v
03-07 09:39:29.329: V/GCMRegistrar(20972): Saving regId on app version 2
03-07 09:39:29.344: I/===GCMIntentService===(20972): unregistered = APA91bFJW_CNYh5AYKomkUPvVGvQay0WgL_GH8DdoxooBXcJSo1ryONrB4VcsRysYedx-oKjgDiPSzhrXR6s8HBH7pIl47SvYR5chuJtJWlymZVRm8j7E77F3TirMqF3ag5D8lbOScTVzzA6ZqWMhTDdiAYPkYc5Hw
03-07 09:39:29.344: V/GCMBaseIntentService(20972): Releasing wakelock

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.w.v"
    android:versionCode="2"
    android:versionName="1.1" >

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

    <permission android:name="com.w.v.permission.C2D_MESSAGE"
                android:protectionLevel="signature" />
    <uses-permission android:name="com.w.v.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/v_icon_72_72"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >

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

        <activity android:name=".LoginActivity" >
            <intent-filter>
                <action android:name="com.w.v.LoginActivity" />

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

        <activity
            android:name=".WebViewActivity"
            android:label="WebView" 
            android:noHistory="true">
        </activity>

        <activity
            android:name=".PushAndroidActivity"
            android:label="PushAndroid" >
        </activity>

        <receiver
            android:name="com.google.android.gcm.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.w.v" />
            </intent-filter>
        </receiver>

        <service android:name=".GCMIntentService" />


    </application>

</manifest>

我在this教程

的帮助下构建了这个应用程序

任何帮助将不胜感激! Tnx提前!

PS:我知道这里有一些我现在不用的代码..它的目的是为了将来的目的

0 个答案:

没有答案