Explicit intent com.google.android.c2dm.intent.REGISTER

I switched to GCM from the C2DM path back when, so I'm still logging in by creating an intent com.google.android.c2dm.intent.REGISTER

and passing it in startService

, as stated in the GCM Migration Documentation (which seems to be outdated):

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(context, 0, new Intent(), 0));
registrationIntent.putExtra("sender", "xxxxxx");
context.startService(registrationIntent);

      

Android 5.0 launch must be explicit: how can I do this here? I could call setComponent

in intent, but what would I use as the component name?

+3


source to share


2 answers


I am using a function that converts ExplicitFromImplicitIntent

:



Intent explicit = createExplicitFromImplicitIntent(mContext, registrationIntent);
startService(explicit);

public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
    // Retrieve all services that can match the given intent
    PackageManager pm = context.getPackageManager();
    List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
    if (resolveInfo == null ) {
        return null;
    }

    // Get component info and create ComponentName
    ResolveInfo serviceInfo = resolveInfo.get(0);
    String packageName = serviceInfo.serviceInfo.packageName;
    String className = serviceInfo.serviceInfo.name;
    ComponentName component = new ComponentName(packageName, className);

    // Create a new intent. Use the old one for extras and such reuse
    Intent explicitIntent = new Intent(implicitIntent);

    // Set the component to be explicit
    explicitIntent.setComponent(component);

    return explicitIntent;
}

      

+1


source


You should just stop using this intent, which is deprecated. Since mid-2013, the recommended way to register with GCM is through a library Google Play Services

, and all that is required is calling a single register

class method GoogleCloudMessaging

.

Example (taken from the official demo ):



GoogleCloudMessaging gcm;
...

/**
 * Registers the application with GCM servers asynchronously.
 * <p>
 * Stores the registration ID and the app versionCode in the application's
 * shared preferences.
 */
private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {
        @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.
                sendRegistrationIdToBackend();

                // 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.

                // Persist the regID - no need to register again.
                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
                // If there is an error, don't just keep trying to register.
                // Require the user to click a button again, or perform
                // exponential back-off.
            }
            return msg;
        }

        @Override
        protected void onPostExecute(String msg) {
            mDisplay.append(msg + "\n");
        }
    }.execute(null, null, null);
}

      

Note that registration is done in the background (via AsyncTask

) as the new registration method is blocked, so it cannot be called on the main thread.

+1


source







All Articles