C2DM应用内注册:无法启动服务意图。

3

好的,我很不清楚我在这里缺少了什么。我尝试为我们的应用程序启用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);只会记录以下内容: “无法启动服务意图{ act=com.google.android.c2dm.intent.REGISTRATION (有额外信息) }:未找到” 我不知道我错过了什么。一般建议是:检查您的android.manifest(已完成)或:使用注册的gmail帐户登录。 对此不太确定。我确实使用我的gmail帐户登录,但没有使用我们在注册时放置在意图中的ourSenderIdregistered@googlemail.com。我还坚信这不可能是解决方案。(告诉所有客户使用这个帐户登录...嗯,不行!)。所以我猜这是别的问题,但我就是找不到解决方案:C
1个回答

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个键盘... :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接