我们有一个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是我们所有活动正在实施的接口,以确保至少存在适当的方法。在onResume(),onStop()方法中,活动分别在Receiver处注册和取消注册。
出于测试目的,我有一个Debug Activity,证明了以下两种方法:
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", ourSenderIdregistered@googlemail.com);
startService(registrationIntent);
}
在android.manifest中我添加了<application>
标签中的以下行:
<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>
因此,我们想要接收广播的每个活动都是在它的onResume()上的BroadcastReceiver上注册自己,当发生某些事情时,BroadcastReceiver将捕获并调用已实现的方法。例如。 ,记录消息或显示Toast。
然而,当我发送“C2DM消息”时,我发现这种结构适用于自制广播。 (Bender的消息在Toast中弹出)但registerC2DM().startService(registrationIntent);
只记录:
无法启动服务Intent { act = com.google.android.c2dm.intent.REGISTRATION(有额外内容)}:没有 结果
我不知道我在这里缺少什么。一般建议似乎是:检查你的android.manifest(完成)或:用注册的gmail帐户登录
不确定这个。我已经使用我的Gmail帐户登录了但是没有使用我们在注册时放入意图的ourSenderIdregistered@googlemail.com
。我也坚信这不是解决方案。 (告诉我们所有客户使用此帐户登录...嗯不?!)
所以我想这是别的,但我找不到它:C
答案 0 :(得分:3)
好的,这真的很有意义:
如果您想注册c2dm,请使用
com.google.android.c2dm.intent.REGISTER
但是为了得到这条消息的答案,你必须设置你是广播接收器来听取
com.google.android.c2dm.intent.REGISTRATION
微妙?是的,这里再次出现错误和更正后的版本:
//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", ourSenderIdregistered@googlemail.com);
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", ourSenderIdregistered@googlemail.com);
startService(registrationIntent);
}
在实现这一点之前捣毁3个键盘......:)