android - GCM Reg ID is empty -
hi trying implement gcm push notification. getting empty gcm id. here code:
public void registerclient() { try { // check device supports gcm (should in try / catch) gcmregistrar.checkdevice(this); gcmregistrar.checkmanifest(this); regid = gcmregistrar.getregistrationid(this); if (regid.equals("")) { registrationstatus = "registering..."; gcmregistrar.register(this, project_id); regid="sfsaas"; regid = gcmregistrar.getregistrationid(this); registrationstatus = "registration acquired"; log.i("******in_if_regid*****", regid); log.i("inside","if"); } else { log.i("inside","else"); registrationstatus = "already registered"; log.i("******regid*****", regid); } } catch (exception e) { e.printstacktrace(); registrationstatus = e.getmessage(); } }
here manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.eventnotifier" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <permission android:name="com.example.eventnotifier.permission.c2d_message" android:protectionlevel="signature" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.get_accounts" /> <uses-permission android:name="android.permission.read_contacts"/> <uses-permission android:name="com.example.eventnotifier.permission.c2d_message" /> <uses-permission android:name="android.permission.write_external_storage" /> <!-- camera permission --> <uses-feature android:name="android.hardware.camera" /> <!-- receives gcm messages --> <uses-permission android:name="com.google.android.c2dm.permission.receive" /> <!-- gcm connects google services --> <uses-permission android:name="android.permission.internet" /> <!-- gcm requires google account --> <uses-permission android:name="android.permission.get_accounts" /> <uses-permission android:name="android.permission.use_credentials" /> <uses-permission android:name="android.permission.read_owner_data" /> <!-- wake processor if gcm message received --> <uses-permission android:name="android.permission.wake_lock" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:color/black" > <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.example.eventnotifier" /> </intent-filter> </receiver> <service android:name=".gcmintentservice" /> <activity android:name="com.example.eventnotifier.homeactivity" android:label="@string/app_name" android:screenorientation="portrait" > </activity> <activity android:name="com.example.eventnotifier.registeractivity" android:label="@string/app_name" android:screenorientation="portrait" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.example.eventnotifier.dateactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.descriptionactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.myeventactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.createeventactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.locationactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.addguestactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.contactlistactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.contacttest" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.invitedguestsactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.myinvitationsactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.unregisteredguestsactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> <activity android:name="com.example.eventnotifier.recentguestsactivity" android:label="@string/app_name" android:screenorientation="portrait"> </activity> </application> </manifest>
this line not getting printed -log.i("**in_if_regid***", regid); can guys me out? have submit project tomorrow. appreciated! thank you
it doesn't work that.
gcmregistrar.register
initiates request gcm server, asking registration id. asynch request. gcmregistrar.getregistrationid
returns cached value of registration id, if device not registered, returns null.
the registration id returned in gcmintentservice.onregistered
method.
here's code of gcmintentservice gcm demo.
this code of gcmregistrar.getregistrationid
:
/** * gets current registration id application on gcm service. * <p> * if result empty, registration has failed. * * @return registration id, or empty string if registration not * complete. */ public static string getregistrationid(context context) { final sharedpreferences prefs = getgcmpreferences(context); string registrationid = prefs.getstring(property_reg_id, ""); // check if app updated; if so, must clear registration id // avoid race condition if gcm sends message int oldversion = prefs.getint(property_app_version, integer.min_value); int newversion = getappversion(context); if (oldversion != integer.min_value && oldversion != newversion) { log.v(tag, "app version changed " + oldversion + " " + newversion + "; resetting registration id"); clearregistrationid(context); registrationid = ""; } return registrationid; }
as can see, returns locally stored value of registration id. in order return anything, gcmregistrar.setregistrationid(context context, string regid)
must called, registration id returned gcmintentservice
. , it's called in gcmbaseintentservice
:
private void handleregistration(final context context, intent intent) { gcmregistrar.cancelapppendingintent(); string registrationid = intent.getstringextra(extra_registration_id); string error = intent.getstringextra(extra_error); string unregistered = intent.getstringextra(extra_unregistered); log.d(tag, "handleregistration: registrationid = " + registrationid + ", error = " + error + ", unregistered = " + unregistered); // registration succeeded if (registrationid != null) { gcmregistrar.resetbackoff(context); gcmregistrar.setregistrationid(context, registrationid); onregistered(context, registrationid); return; } ... }
Comments
Post a Comment