Skip to content

Custom message payload

Alexander Boldyrev edited this page Oct 30, 2024 · 22 revisions

By using Custom Payload you can send custom data together with broadcast push messages from New Broadcast campaign page on Portal. The custom payload can be made of key-value pairs or plain JSON:

Key-value pairs Plain JSON
CUP Settings CUP Settings

It is also possible to send Custom Payload using Single PUSH message and Multiple PUSH messages APIs.

The following example code shows how you can retrieve the custom payload bundle from within your application by subscribing to message received Event:

// declare message receiver
private val messageReceiver = object: BroadcastReceiver() {
    override fun onReceive(context:Context, intent: Intent) {
        val message = Message.createFrom(intent.extras)
        // extract your custom data
        val customPayload = message.customPayload
        if (customPayload != null) {
            val url = customPayload.optString("url", "")
            val dish = customPayload.optString("dish", "")
            Log.d("CustomPayload", String.format("dish is: %s, url is: %s", url, dish))
        }
    }
}

override fun onResume() {
    super.onResume()
    // subscribe to MESSAGE_RECEIVED event
    val localBroadcastManager = LocalBroadcastManager.getInstance(this)
    localBroadcastManager.registerReceiver(messageReceiver, IntentFilter(Event.MESSAGE_RECEIVED.key))
}
expand to see Java code

// declare message receiver
private final BroadcastReceiver messageReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Message message = Message.createFrom(intent.getExtras());

        // extract your custom data
        JSONObject customPayload = message.getCustomPayload();
        if (customPayload != null) {
            final String url = customPayload.optString("url", "");
            final String dish = customPayload.optString("dish", "");

            Log.d("CustomPayload", String.format("dish is: %s, url is: %s", url, dish));
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    // subscribe to MESSAGE_RECEIVED event
    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
    localBroadcastManager.registerReceiver(messageReceiver, new IntentFilter(Event.MESSAGE_RECEIVED.getKey()));
}
Clone this wiki locally