android - GCMBaseIntentService 回调仅在根包中

标签 android push-notification google-cloud-messaging intentservice

我已经在我的应用程序中实现了 GCM,但遇到了一个奇怪的问题。 当我的 GCMBaseIntentService 的重写 IntentService 位于我的根包中时,它工作正常。我在 list 中使用 .MyGCMintentService 引用它。 根包将是 com.example.rootpackage

当我将 Intent 服务移动到不同的包时,例如com.example.rootpackage.service, Intent 服务永远不会被调用。此时,我将更新我的 list 以指向 com.example.rootpackage.service.MyGCMItentService 并且没有骰子。

我是否遗漏了 Google 文档中有关查找它的内容,或者这就是它的工作原理?

最佳答案

是的,它应该在根包中:

This intent service will be called by the GCMBroadcastReceiver (which is provided by the GCM library), as shown in the next step. It must be a subclass of com.google.android.gcm.GCMBaseIntentService, must contain a public constructor, and should be named my_app_package.GCMIntentService (unless you use a subclass of GCMBroadcastReceiver that overrides the method used to name the service).

(引自 here )

编辑:

正如文档所述,如果您使用覆盖 getDefaultIntentServiceClassNameGCMBroadcastReceiver 子类,则可以更改它:

public class GCMBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "GCMBroadcastReceiver";
    private static boolean mReceiverSet = false;

    @Override
    public final void onReceive(Context context, Intent intent) {
        Log.v(TAG, "onReceive: " + intent.getAction());
        // do a one-time check if app is using a custom GCMBroadcastReceiver
        if (!mReceiverSet) {
            mReceiverSet = true;
            String myClass = getClass().getName();
            if (!myClass.equals(GCMBroadcastReceiver.class.getName())) {
                GCMRegistrar.setRetryReceiverClassName(myClass);
            }
        }
        String className = getGCMIntentServiceClassName(context);
        Log.v(TAG, "GCM IntentService class: " + className);
        // Delegates to the application-specific intent service.
        GCMBaseIntentService.runIntentInService(context, intent, className);
        setResult(Activity.RESULT_OK, null /* data */, null /* extra */);
    }

    /**
     * Gets the class name of the intent service that will handle GCM messages.
     */
    protected String getGCMIntentServiceClassName(Context context) {
        return getDefaultIntentServiceClassName(context);
    }

    /**
     * Gets the default class name of the intent service that will handle GCM
     * messages.
     */
    static final String getDefaultIntentServiceClassName(Context context) {
        String className = context.getPackageName() +
                DEFAULT_INTENT_SERVICE_CLASS_NAME;
        return className;
    }
}

关于android - GCMBaseIntentService 回调仅在根包中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16951216/

相关文章:

android - 在 android asyncTask 中重复运行 doInBackground

java - 不能从另一个类访问字符串?

ios - PodFile中 'GoogleCloudMessaging'和 'Google/CloudMessaging'之间的区别

android - 如何检查 Google Play Services API 的可用性? (特别是 GCM API)

android - 如何修复 "program type already present"错误?

android - 如何在与 facebook 集成时解决这些问题

android - 应用程序被杀死时的 GCM 推送通知 Android Pre lollipop

node.js - 确定是否向 Firebase 实时数据库添加或删除数据

azure - 如何使用 Azure 通知中心向 iOS 和 Android 发送静默通知

android - GCM android 推送通知始终显示旧消息。收到的 Intent 不正确