android-C2DM inapp注册:无法启动服务意图
作者:互联网
好吧,我真的不知道我在这里缺少什么.我试图让C2DM能够为我们的应用程序工作,尤其是处理广播使我感到挣扎.
我们有一个适用于整个应用程序的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()方法分别在接收方注册和注销活动.
出于测试目的,我有一个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);只是记录:
Unable to start service Intent {
act=com.google.android.c2dm.intent.REGISTRATION (has extras) }: not
found
我不知道我在这里想念什么.一般建议似乎是:检查您的android.manifest(已完成)或:使用注册的gmail帐户登录.
不知道这个.是的,我用gmail帐户登录,但没有使用ourSenderIdregistered@googlemail.com登录,我们在注册时将其放入了意图.我也坚信这不是解决方案. (告诉我们所有的客户使用该帐户登录…嗯,不是吗?!).
所以我想那是另外一回事,但我只是找不到:C
解决方法:
好的,这真的是要发现:
如果要注册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个键盘…
标签:broadcastreceiver,android-c2dm,android 来源: https://codeday.me/bug/20191201/2080731.html