在一个应用程序中使用 2 个或更多 GCM Intent Services
Using 2 or more GCM Intent Services in one Application
我正在编写一个与 Smooch and Carnival 集成的应用程序。这两个库都使用定义 GCM Intent Service 来接收消息的标准方法来接收 GCM 推送消息。
当我只使用 Smooch 时,一切都很好。当我只使用嘉年华时,一切都很好。当我尝试同时使用两者时,问题就来了。我发现 GCM 接收器将简单地启动清单中列出的定义意图 com.google.android.c2dm.intent.RECEIVE
.
的第一个服务
事实上,我发现库在我的 build.gradle
中列出的顺序会影响它们的清单合并到应用程序清单中的顺序。所以,如果我把 smooch 放在第一位,它就会起作用(但 Carnival 不会收到任何东西)。如果我把嘉年华放在第一位,它就会起作用(但 Smooch 永远不会收到任何东西)。
当我不控制其中任何一个时,如何处理多个 GCM 意图服务?一般来说,应用程序应该如何定义和管理多个 GCM intent 服务?
您将两者都用作 gradle 依赖项?您必须下载这两个库并将它们作为模块使用,它们可能使用相同的服务,如果您下载它们,您可以更改服务名称并解决任何可能与两者相关的问题。
我的猜测是,您可能必须使用您的应用模块创建 GCM 广播接收器(即使它调用库服务)。
您无法在 Carnival 和 Smooch 中使用 push 的原因是这两个库都在注册自己的 GcmListenerService,而在 Android 清单中定义的第一个 GcmListenerService 将接收所有 GCM 消息。
我有一个主要基于以下 SO 文章的解决方案:
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
中,使用您的令牌调用 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 and Carnival 集成的应用程序。这两个库都使用定义 GCM Intent Service 来接收消息的标准方法来接收 GCM 推送消息。
当我只使用 Smooch 时,一切都很好。当我只使用嘉年华时,一切都很好。当我尝试同时使用两者时,问题就来了。我发现 GCM 接收器将简单地启动清单中列出的定义意图 com.google.android.c2dm.intent.RECEIVE
.
事实上,我发现库在我的 build.gradle
中列出的顺序会影响它们的清单合并到应用程序清单中的顺序。所以,如果我把 smooch 放在第一位,它就会起作用(但 Carnival 不会收到任何东西)。如果我把嘉年华放在第一位,它就会起作用(但 Smooch 永远不会收到任何东西)。
当我不控制其中任何一个时,如何处理多个 GCM 意图服务?一般来说,应用程序应该如何定义和管理多个 GCM intent 服务?
您将两者都用作 gradle 依赖项?您必须下载这两个库并将它们作为模块使用,它们可能使用相同的服务,如果您下载它们,您可以更改服务名称并解决任何可能与两者相关的问题。
我的猜测是,您可能必须使用您的应用模块创建 GCM 广播接收器(即使它调用库服务)。
您无法在 Carnival 和 Smooch 中使用 push 的原因是这两个库都在注册自己的 GcmListenerService,而在 Android 清单中定义的第一个 GcmListenerService 将接收所有 GCM 消息。
我有一个主要基于以下 SO 文章的解决方案:
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
中,使用您的令牌调用 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);
}
}