I have some problem to using GCM in Fragment.
first, I get example App to using GCM I try and it work fine.
so I using this example code to I use in my Application but I always getting empty string from GCMRegistrar.getRegistrationId
this is my code.
Controller.java
publicclass Controller extends Application{ private finalint MAX_ATTEMPTS = 5; private finalint BACKOFF_MILLI_SECONDS = 2000; private final Random random = new Random(); static Context ct; public Controller(Context context) { ct = context.getApplicationContext(); } // Register this account with the server. publicvoid register(final Context context, String name, String email, final String regId) { Log.i(Config.TAG, "registering device (regId = " + regId + ")"); String serverUrl = Config.YOUR_SERVER_URL; Map<String, String> params = new HashMap<String, String>(); params.put("regId", regId); params.put("name", name); params.put("email", email); .... } // Notifies UI to display a message. publicvoid displayMessageOnScreen(Context context, String message) { Intent intent = new Intent(Config.DISPLAY_MESSAGE_ACTION); intent.putExtra(Config.EXTRA_MESSAGE, message); context.sendBroadcast(intent); } }
Config.java
publicinterface Config { staticfinal String YOUR_SERVER_URL = "url Server"; staticfinal String GOOGLE_SENDER_ID = "sender id"; staticfinal String TAG = "GCM Android "; staticfinal String DISPLAY_MESSAGE_ACTION ="com.MyApp.gcm.DISPLAY_MESSAGE"; staticfinal String EXTRA_MESSAGE = "message"; }
GCMIntentService.java
publicclass GCMIntentService extends GCMBaseIntentService { privatestaticfinal String TAG = "GCMIntentService"; private Controller aController = null; public GCMIntentService() { super(Config.GOOGLE_SENDER_ID); } @Override protectedvoid onRegistered(Context context, String registrationId) { if(aController == null) aController = (Controller) getApplicationContext(); Log.i(TAG, "Device registered: regId = " + registrationId); aController.displayMessageOnScreen(context, "Your device registred with GCM"); aController.register(context, ProfileFragment.name, ProfileFragment.email, registrationId); } @Override protectedvoid onUnregistered(Context context, String registrationId) { if(aController == null) aController = (Controller) getApplicationContext(); Log.i(TAG, "Device unregistered"); aController.displayMessageOnScreen(context, getString(R.string.gcm_unregistered)); aController.unregister(context, registrationId); } @Override protectedvoid onMessage(Context context, Intent intent) { if(aController == null) aController = (Controller) getApplicationContext(); Log.i(TAG, "Received message"); String message = intent.getExtras().getString("price"); aController.displayMessageOnScreen(context, message); generateNotification(context, message); } @Override protectedvoid onDeletedMessages(Context context, int total) { if(aController == null) aController = (Controller) getApplicationContext(); Log.i(TAG, "Received deleted messages notification"); String message = getString(R.string.gcm_deleted, total); aController.displayMessageOnScreen(context, message); generateNotification(context, message); } @Override publicvoid onError(Context context, String errorId) { if(aController == null) aController = (Controller) getApplicationContext(); Log.i(TAG, "Received error: " + errorId); aController.displayMessageOnScreen(context, getString(R.string.gcm_error, errorId)); } @Override protectedboolean onRecoverableError(Context context, String errorId) { if(aController == null) aController = (Controller) getApplicationContext(); Log.i(TAG, "Received recoverable error: " + errorId); aController.displayMessageOnScreen(context, getString(R.string.gcm_recoverable_error, errorId)); return super.onRecoverableError(context, errorId); } privatestaticvoid generateNotification(Context context, String message) { int icon = R.drawable.ic; long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, message, when); String title = context.getString(R.string.app_name); Intent notificationIntent = new Intent(context, ProfileFragment.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, title, message, intent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.defaults |= Notification.DEFAULT_SOUND; notification.defaults |= Notification.DEFAULT_VIBRATE; notificationManager.notify(0, notification); }}
AndroidManifest.xml
<permission android:name="com.MyApp.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="com.MyApp.permission.C2D_MESSAGE" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/><application android:allowBackup="true" android:icon="@drawable/ic" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity ..... <intent-filter> <action android:name="android.intent.action.VIEW"/> <action android:name="android.intent.action.DELETE"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </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.MyApp" /> </intent-filter> </receiver> <service android:name=".gcm.GCMIntentService" android:enabled="true"/> </application>
LoginFragment.java
GCMRegistrar.checkDevice(getActivity()); GCMRegistrar.checkManifest(getActivity()); final String regaId = GCMRegistrar.getRegistrationId(getActivity().getApplicationContext()); if (regaId.equals("")) { GCMRegistrar.register(getActivity(), Config.GOOGLE_SENDER_ID); }else{ ..... }
please help me..