Zap to App Communication

Zap experiences can send arbitrary strings of data to the host app using the Z.device.messageHost(...) function. This article shows how to receive these messages within your Android host app.

Register BroadcastReceiver

The embed component broadcasts host messages using a LocalBroadcastManager. You can use a BroadcastReceiver to process messages sent with Z.device.messageHost(...) like this:

private BroadcastReceiver mZapparMessageReceiver = null;

@Override
protected void onCreate(Bundle savedInstanceState) {

    // ...

    IntentFilter filter = new IntentFilter(ZapparEmbed.ACTION_HOST_MESSAGE);
    mZapparMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent zapparIntent) {
            Log.d("MyApp", zapparIntent.getStringExtra(ZapparEmbed.EXTRA_HOST_MESSAGE));
        }
    };
    LocalBroadcastManager.getInstance(this).registerReceiver(mZapparMessageReceiver, filter);

    // ...
}

Unregister BroadcastReceiver

In order to avoid leaking the BroadcastReceiver it's best to unregister it in an appropriate place:

@Override
protected void onDestroy() {
    super.onDestroy();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mZapparMessageReceiver);
}
zapcode branded_zapcode i