如何将 GCM 消息从 GcmListenerService 路由到它们的相关处理程序?
How do I route GCM messages from a GcmListenerService to their relevant handlers?
我正在开发的应用程序有两个使用 GCM 进行推送通知的服务(Pushwoosh 和 Helpshift)。我正在尝试在 Pushwoosh 文档中实现此处显示的功能,以允许两个系统运行; https://docs.pushwoosh.com/v1.0/docs/gcm-integration-legacy。然而,我的 Android 知识让我失望了,因为我实际上是如何将收到的包路由到相关处理程序的。该项目是在 Unity 中完成的,但这在很大程度上 Android 领域。
这是我创建的与示例非常相似的 GcmListenerService
class;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.gcm.*;
import android.content.ComponentName;
public class GCMListenerRouterService extends GcmListenerService
{
public GCMListenerRouterService()
{
super();
Log.i("Unity", "GCMListener - Constuctor");
}
private void dispatchMessage(String component, Bundle data)
{
Log.i("Unity", "GCMListener - dispatchMessage: " + (data != null ? data.toString() : "<null>") + " component: " + component);
Intent intent = new Intent();
intent.putExtras(data);
intent.setAction("com.google.android.c2dm.intent.RECEIVE");
intent.setComponent(new ComponentName(getPackageName(), component));
GcmReceiver.startWakefulService(getApplicationContext(), intent);
}
@Override
public void onMessageReceived(String from, Bundle data)
{
Log.i("Unity", "GCMListener - onMessageReceived: " + (data != null ? data.toString() : "<null>") + " from: " + from);
// Base GCM listener service removes this extra before calling onMessageReceived
// Need to set it again to pass intent to another service
//data.putString("from", from);
//if (TextUtils.equals(from, getString(R.string.PUSHWOOSH_PROJECT_ID)))
//{
// dispatchMessage(PushGcmIntentService.class.getName(), data);
//}
//else if (TextUtils.equals(from, getString(R.string.PRIVATE_PROJECT_ID)))
//{
// dispatchMessage(PrivateGCMListenerService.class.getName(), data);
//}
}
}
我能够确认推送通知来自正确的消息服务,并且我可以确定推送通知是来自一个插件还是另一个插件。如何将这些捆绑对象路由到正确的处理程序?我不明白下面的示例代码;
if (TextUtils.equals(from, getString(R.string.PUSHWOOSH_PROJECT_ID)))
{
dispatchMessage(PushGcmIntentService.class.getName(), data);
}
else if (TextUtils.equals(from, getString(R.string.PRIVATE_PROJECT_ID)))
{
dispatchMessage(PrivateGCMListenerService.class.getName(), data);
}
我很欣赏这是示例代码,但我在 Android 文档中找不到与 dispatchMessage
具有相同签名的任何函数。我是否需要为所需的每种不同类型的消息制作意图服务?
我知道对于 Helpshift,我需要调用一个带有签名 handlePush(Context context, Bundle data)
的函数,但我不确定 Context
对象是什么。对于 Pushwoosh,我不确定处理程序是什么。当我谈论两个特定服务时,我假设此设置是接收和处理消息的标准方法。
事实证明,对于 GCM,Bundle
对象是您需要处理的原始推送详细信息。不需要进一步处理,支持 GCM 的 plugins/frameworks 应该有一个处理这个的函数,例如对于 helpshift,该函数在 com.helpshift.Core
中称为 handlePush(Context context, Bundle data)
。
请注意,GCM 实际上已被弃用,Firebase Cloud Messenger 是未来的新系统。此服务以不同的方式处理多个推送处理程序,您应该检查您的插件以获取相关文档。
我正在开发的应用程序有两个使用 GCM 进行推送通知的服务(Pushwoosh 和 Helpshift)。我正在尝试在 Pushwoosh 文档中实现此处显示的功能,以允许两个系统运行; https://docs.pushwoosh.com/v1.0/docs/gcm-integration-legacy。然而,我的 Android 知识让我失望了,因为我实际上是如何将收到的包路由到相关处理程序的。该项目是在 Unity 中完成的,但这在很大程度上 Android 领域。
这是我创建的与示例非常相似的 GcmListenerService
class;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.gcm.*;
import android.content.ComponentName;
public class GCMListenerRouterService extends GcmListenerService
{
public GCMListenerRouterService()
{
super();
Log.i("Unity", "GCMListener - Constuctor");
}
private void dispatchMessage(String component, Bundle data)
{
Log.i("Unity", "GCMListener - dispatchMessage: " + (data != null ? data.toString() : "<null>") + " component: " + component);
Intent intent = new Intent();
intent.putExtras(data);
intent.setAction("com.google.android.c2dm.intent.RECEIVE");
intent.setComponent(new ComponentName(getPackageName(), component));
GcmReceiver.startWakefulService(getApplicationContext(), intent);
}
@Override
public void onMessageReceived(String from, Bundle data)
{
Log.i("Unity", "GCMListener - onMessageReceived: " + (data != null ? data.toString() : "<null>") + " from: " + from);
// Base GCM listener service removes this extra before calling onMessageReceived
// Need to set it again to pass intent to another service
//data.putString("from", from);
//if (TextUtils.equals(from, getString(R.string.PUSHWOOSH_PROJECT_ID)))
//{
// dispatchMessage(PushGcmIntentService.class.getName(), data);
//}
//else if (TextUtils.equals(from, getString(R.string.PRIVATE_PROJECT_ID)))
//{
// dispatchMessage(PrivateGCMListenerService.class.getName(), data);
//}
}
}
我能够确认推送通知来自正确的消息服务,并且我可以确定推送通知是来自一个插件还是另一个插件。如何将这些捆绑对象路由到正确的处理程序?我不明白下面的示例代码;
if (TextUtils.equals(from, getString(R.string.PUSHWOOSH_PROJECT_ID)))
{
dispatchMessage(PushGcmIntentService.class.getName(), data);
}
else if (TextUtils.equals(from, getString(R.string.PRIVATE_PROJECT_ID)))
{
dispatchMessage(PrivateGCMListenerService.class.getName(), data);
}
我很欣赏这是示例代码,但我在 Android 文档中找不到与 dispatchMessage
具有相同签名的任何函数。我是否需要为所需的每种不同类型的消息制作意图服务?
我知道对于 Helpshift,我需要调用一个带有签名 handlePush(Context context, Bundle data)
的函数,但我不确定 Context
对象是什么。对于 Pushwoosh,我不确定处理程序是什么。当我谈论两个特定服务时,我假设此设置是接收和处理消息的标准方法。
事实证明,对于 GCM,Bundle
对象是您需要处理的原始推送详细信息。不需要进一步处理,支持 GCM 的 plugins/frameworks 应该有一个处理这个的函数,例如对于 helpshift,该函数在 com.helpshift.Core
中称为 handlePush(Context context, Bundle data)
。
请注意,GCM 实际上已被弃用,Firebase Cloud Messenger 是未来的新系统。此服务以不同的方式处理多个推送处理程序,您应该检查您的插件以获取相关文档。