您无法在 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);
}
}