English 中文(简体)
Problem registering for C2DM in Android
原标题:

I m trying to test the C2DM framework. I got the confirmation email a couple of days ago and then tryied to create a client that could register. For that purpose, I created a simple client following the steps described in this tutorial: http://code.google.com/intl/es-419/android/c2dm/index.html.

The Android manifest file contains among other things this code:

<permission android:name="com.bilthon.ufrj.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.bilthon.ufrj.permission.C2D_MESSAGE" />

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<uses-permission android:name="android.permission.INTERNET"/>

<receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND">

<intent-filter>
   <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
   <category android:name="com.bilthon.ufrj" />
</intent-filter>

<intent-filter>
   <action android:name="com.google.android.c2dm.intent.RECEIVE" />
   <category android:name="com.bilthon.ufrj" />
</intent-filter>
</receiver>

And then, the main activity launched when the program starts has the following code:

Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender","mytestemail@gmail.com");
Log.d("WelcomeScreen","mytestemail@gmail.com");
startService(registrationIntent);

I also registered a google account on the AVD running my client, as they said it was required. But the problem is that I cannot get the broadcast receiver to "wake up". I don t know what could be wrong. By analysing the logs, I can see that the registration intent is created and apparently used correctly, but the receiver code just never is executed, what could be wrong?

Thanks in advance Nelson

最佳回答

Well.. just sorted it out, the problem was with the declaration of the receiver. The tags for the receiver should go inside the application tag, just as demonstrated here: http://developer.android.com/guide/topics/manifest/manifest-intro.html

Here s an example of a well formated Manifest for a C2DM application. Thanks to Mark Murphy for posting the link at the android-c2dm group.

And sorry for the silly mistake.

Nelson

问题回答

I just got this working myself after wrestling with it for some time.

In the manifest, you have the line

<receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND">

Which means you need a class called C2DMReceiver that extends C2DMBaseReceiver in the c2dm package. To get implement this, I copied both the c2dm package and C2DMReceiver.java file from the chrometophone-android example over to my project and was able to get a registration id from the C2DM server as intended.

I had the same problem. My solution was moving all of the permissions in my manifest above the application tag.

Things you can check:

1 I noticed is that you are declaring a C2DM permission but don t use it in your application like so:

<uses-permission android:name="com.bilthon.ufrj.permission.C2D_MESSAGE" />

2 If you have a look at the c2dm library you will see that the helper C2DMessaging s register method creates the intent with an additional call to setPackage.

registrationIntent.setPackage("com.google.android.gsf");




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签