【问题标题】:How do I route GCM messages from a GcmListenerService to their relevant handlers?如何将 GCM 消息从 GcmListenerService 路由到其相关处理程序?
【发布时间】:2018-01-15 12:12:54
【问题描述】:

我正在开发的应用程序有两个使用 GCM 推送通知的服务(Pushwoosh 和 Helpshift)。我正在尝试实现 Pushwoosh 文档中显示的功能,以允许两个系统都运行; https://docs.pushwoosh.com/v1.0/docs/gcm-integration-legacy。然而,我的 Android 知识让我无法了解我如何将收到的捆绑包实际路由到相关处理程序。该项目是在 Unity 中完成的,但这属于 Android 领域。

这是我创建的GcmListenerService 类,与示例非常相似;

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,我不确定处理程序是什么。当我谈论两个特定的服务时,我假设此设置是接收和处理消息的标准方法。

【问题讨论】:

    标签: java android push-notification google-cloud-messaging gcmlistenerservice


    【解决方案1】:

    事实证明,对于 GCM,Bundle 对象是您需要处理的原始推送细节。不需要进一步处理,支持 GCM 的插件/框架应该有一个处理这个的函数,例如对于 helpshift,该函数位于 com.helpshift.Core 中,称为 handlePush(Context context, Bundle data)

    请注意,GCM 实际上已被弃用,而 Firebase Cloud Messenger 是未来的新系统。该服务以不同的方式处理多个推送处理程序,您应该检查您的插件以获取有关这方面的文档。

    【讨论】:

      猜你喜欢
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      相关资源
      最近更新 更多