GCM广播接收器区分不同的请求

时间:2016-05-04 11:35:58

标签: java android google-cloud-messaging

我有一个标准的GCM广播接收器。当我向服务器发出请求时,我会发送我的注册ID和用户名以及标题。当我想从服务器向手机发送消息时,如何再次确认该特定标题?到目前为止,我看到的所有例子似乎都依赖于1个请求。

如果我没有充分解释它:

用户A在GCM服务器上注册并获得gcm注册ID 用户A向服务器发送带有gcm注册ID,标题及其用户名的请求。 服务器将消息发送回用户。 我如何确保它是针对特定请求而不是用户自己?

这是我的广播接收器

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Bundle extras = intent.getExtras();
    Intent update = new Intent("update");
    update.putExtra("msg",extras.getString(AppConstants.MSG_KEY));

    ComponentName comp = new ComponentName(context.getPackageName(), GCMNotificationIntentService.class.getName());
    LocalBroadcastManager.getInstance(context).sendBroadcast(update);
    startWakefulService(context, (intent.setComponent(comp)));
    Log.d("ONRECEIVEWAKEFUL", "ONRECEIVEWAKEFUL");
    Log.d("ONRECEIVEWAKEFULMSG",String.valueOf(extras.getString(AppConstants.MSG_KEY)));
    setResultCode(Activity.RESULT_OK);
}
}

我需要设置过滤器吗?

编辑这是我从gcm服务器接收消息的地方

LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("update"));

    //Intent msg sent from Broadcast Receiver
    String str = getIntent().getStringExtra("msg");

1 个答案:

答案 0 :(得分:0)

你的后端应该热点只处理发送消息,但它也应该处理某种"注册"用户/设备,以便您可以告诉哪个用户使用哪个设备。您在设备上收到的registration Id内容不考虑用户 - 您需要在后端自行处理,并以某种方式将注册ID与您的用户。