问题描述:

OK, I really don't know what I'm lacking here. I try to get C2DM working for our application and especially handling the Broadcasts make me struggle.

We have an appwide BroadcastReceiver:

public final class AppBroadcastReceiver extends BroadcastReceiver {

//get an instance if not already present

public static AppBroadcastReceiver getInstance(final IntentFilter filter) {

if (instance == null) {

instance = new GlobalBroadcastReceiver();

}

if (filter != null) {

filter.addAction("com.google.android.c2dm.intent.REGISTRATION";

filter.addAction("com.google.android.c2dm.intent.RECEIVE");

filter.addCategory("my.package.name");

}

return instance;

}

@Override

public void onReceive(Context context, Intent intent) {

final String broadcastAction = intent.getAction();

Log.d(logTag, String.format("GlobalBroadcastReceiver::onReceive for action = %s", broadcastAction));

if ("com.google.android.c2dm.intent.REGISTRATION".equals(broadcastAction)) {

for (final AppBroadcastListener l : listeners) {

l.c2dmRegistration(intent);

}

} else if ("com.google.android.c2dm.intent.RECEIVE".equals(broadcastAction)) {

for (final ApplBroadcastListener l : listeners) {

l.c2dmReceive(intent);

}

}//else

}//onReceive

}

AppBroadcastListener is an interface all our activities are implementing to assure the appropriate method are at least present. In their onResume(), onStop() methods the activities registering and unregistered themselves at the Receiver, respectively.

For testing purposes I have a Debug Activity proving me the following two methods:

public void sendC2DM(View v){

Intent intent= new Intent();

intent.setAction(com.google.android.c2dm.intent.RECEIVE);

intent.putExtra("message","Bender: \"kiss my shiny metal ass!\"");

intent.addCategory(getPackageName() );

sendBroadcast(intent);

}

public void registerC2DM(View v){

Intent registrationIntent = new Intent(com.google.android.c2dm.intent.REGISTRATION);

registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate

registrationIntent.putExtra("sender", [email protected]);

startService(registrationIntent);

}

And in the android.manifest I've added the follwoing lines in the <application>-tag:

 <receiver

android:name=".AppBroadcastReceiver"

android:permission="com.google.android.c2dm.permission.SEND" >

<intent-filter>

<action android:name="com.google.android.c2dm.intent.RECEIVE" />

<category android:name="my.package.name" />

</intent-filter>

<intent-filter>

<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

<category android:name="my.package.name" />

</intent-filter>

</receiver>

So every activity we want to receive broadcast is registering itself at the BroadcastReceiver on it's onResume() and when something is happening the BroadcastReceiver will catch and call the implemented methods. E.g. ,logging the message or displaying a Toast.

However when I send a "C2DM message" I see this structure is working for self crafted broadcast. (Bender's message is popping up in a Toast) but registerC2DM().startService(registrationIntent); just logs:

Unable to start service Intent {

act=com.google.android.c2dm.intent.REGISTRATION (has extras) }: not

found

I've no clue what I'm missing here. General advice seems to be: check your android.manifest (done) or: log in with the registered gmail account.

Not sure bout this. I'm logged in with my gmail account yeah but not with [email protected] which we put in the intent when registering. I also strongly believe this can not be the solution. (Telling all our customers to log in with this account... ehm no?!).

So I guess it's something else but I just can't find it :C

网友答案:

OK, this is really mean to spot:

If you want to register for c2dm you use

com.google.android.c2dm.intent.REGISTER

but to catch the answer to this message you have to set up you're broadcast receiver to listen to

com.google.android.c2dm.intent.REGISTRATION

Subtle? Yes, here once again the error and the corrected version:

//false
public void registerC2DM(View v){
    Intent registrationIntent = new Intent(com.google.android.c2dm.intent.REGISTRATION);
    registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
    registrationIntent.putExtra("sender", [email protected]);
    startService(registrationIntent);
}


//true
public void registerC2DM(View v){
    Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate
registrationIntent.putExtra("sender", [email protected]);
    startService(registrationIntent);
}

Smashed 3 keyboards before realising this... :)

相关阅读:
Top