【问题标题】:Using 2 or more GCM Intent Services in one Application在一个应用程序中使用 2 个或更多 GCM Intent 服务
【发布时间】:2016-04-11 00:38:56
【问题描述】:

我正在编写一个与SmoochCarnival 集成的应用程序。这两个库都使用定义 GCM Intent 服务以接收消息的标准方法接收 GCM 推送消息。

当我只使用 Smooch 时,一切都很好。当我只使用嘉年华时,一切都很好。当我尝试同时使用两者时,问题就出现了。我发现 GCM 接收器将简单地启动清单中列出的第一个定义意图 com.google.android.c2dm.intent.RECEIVE 的服务。

事实上,我发现我的build.gradle 中列出的库的顺序会影响它们的清单合并到应用程序清单中的顺序。所以,如果我把 smooch 放在首位,它会起作用(但 Carnival 没有收到任何东西)。如果我把嘉年华放在首位,它会起作用(但 Smooch 从来没有收到任何东西)。

当我无法控制多个 GCM Intent 服务时,如何处理其中一个?一般来说,应用程序应该如何定义和管理多个 GCM Intent 服务?

【问题讨论】:

    标签: android android-intent google-cloud-messaging smooch


    【解决方案1】:

    您无法在 Carnival 和 Smooch 中使用 push 的原因是这两个库都在注册自己的 GcmListenerService,而在 Android 中,清单中定义的第一个 GcmListenerService 将接收所有 GCM 消息。

    我主要根据以下 SO 文章为您提供解决方案: Multiple GCM listeners using GcmListenerService

    最好的解决方案是只有一个 GcmListenerService 实现,并让这个处理两个消息。

    要指定您自己的 GcmListenerService,请按照Google's Cloud Messaging Documentation 的说明进行操作。

    Smooch 为您提供必要的工具,让您在拥有自己的 GCM 时禁用其内部注册。

    为此,只需在初始化 Smooch 时调用 setGoogleCloudMessagingAutoRegistrationEnabled

    Settings settings = new Settings("<your_app_token>");
    settings.setGoogleCloudMessagingAutoRegistrationEnabled(false);
    Smooch.init(this, settings);
    

    在您自己的GcmRegistrationIntentService 中,使用您的令牌调用Smooch.setGoogleCloudMessagingToken(token);

    一旦完成,您就可以将 GCM 消息传递给您想要的任何 GCM 接收器。

    @Override
    public void onMessageReceived(String from, Bundle data) {
        final String smoochNotification = data.getString("smoochNotification");
    
        if (smoochNotification != null && smoochNotification.equals("true")) {
            data.putString("from", from);
    
            Intent intent = new Intent();
            intent.putExtras(data);
            intent.setAction("com.google.android.c2dm.intent.RECEIVE");
            intent.setComponent(new ComponentName(getPackageName(), "io.smooch.core.GcmService"));
    
            GcmReceiver.startWakefulService(getApplicationContext(), intent);
        }
    }
    

    编辑

    从 Smooch 版本 3.2.0 开始,您现在可以通过在 onMessageReceived 中调用 GcmService.triggerSmoochGcm 来更轻松地触发 Smooch 的通知。

    @Override
    public void onMessageReceived(String from, Bundle data) {
        final String smoochNotification = data.getString("smoochNotification");
    
        if (smoochNotification != null && smoochNotification.equals("true")) {
            GcmService.triggerSmoochGcm(data, this);
        }
    }
    

    【讨论】:

    • 谢谢,很高兴看到 Smooch 提供了这样做的能力。不幸的是,我相信嘉年华不会。但是,我应该能够使用相同的方法并为每个服务调用两次startWakefulService,并确保我的中继意图服务在清单中首先列出。将尝试此操作并在此处更新。
    • 这行得通。我实际上不需要禁用 GCM 注册并自己做,因为我对这两个服务使用相同的发件人 ID 和 API 密钥。我也可以使用 smoochNotification 标志将消息发送到正确的位置,而不仅仅是盲目地发送给两者。所以,一切都好...谢谢!
    【解决方案2】:

    您将两者都用作 gradle 依赖项?您必须下载这两个库并将它们用作模块,它们可能使用相同的服务,如果您下载它们,您可以更改服务名称并解决可能与两者相关的任何问题。

    我的猜测是,您可能必须使用您的应用模块创建 GCM 广播接收器(即使它调用 libs 服务)。

    【讨论】:

    • 对不起,我不明白你的回答。服务名称不是问题——它们都已经有了唯一的名称。我曾考虑定义自己的服务来调用其他两个服务,但似乎充满了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多