gpt4 book ai didi

android - 在一个应用程序中使用 2 个或更多 GCM Intent Services

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:30:31 26 4
gpt4 key购买 nike

我正在编写一个与 Smooch 集成的应用程序和 Carnival .这两个库都使用定义 GCM Intent Service 来接收消息的标准方法来接收 GCM 推送消息。

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

事实上,我发现库在我的 build.gradle 中列出的顺序会影响它们的 list 合并到应用程序 list 中的顺序。所以,如果我把 smooch 放在第一位,它就会起作用(但 Carnival 不会收到任何东西)。如果我把嘉年华放在第一位,它就会起作用(但 Smooch 永远不会收到任何东西)。

当我不控制任何一个时,如何处理多个 GCM Intent 服务?一般而言,应用程序应如何定义和管理多个 GCM Intent 服务?

最佳答案

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

我有一个主要基于以下 SO 文章的解决方案: Multiple GCM listeners using GcmListenerService

The best solution would be to just have one GcmListenerService implementation, and have this handle messages for both.

为了指定您自己的 GcmListenerService,请按照 Google's Cloud Messaging Documentation 中的说明进行操作.

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

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

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

并在您自己的 GcmRegistrationIntentService 中,使用您的 token 调用 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 版开始,您现在可以通过调用 GcmService.triggerSmoochGcm 更轻松地触发 Smooch 的通知在你的 onMessageReceived 中。

@Override
public void onMessageReceived(String from, Bundle data) {
final String smoochNotification = data.getString("smoochNotification");

if (smoochNotification != null && smoochNotification.equals("true")) {
GcmService.triggerSmoochGcm(data, this);
}
}

关于android - 在一个应用程序中使用 2 个或更多 GCM Intent Services,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36538113/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com