Skip to content

Library events

Ivan Krešić edited this page Oct 18, 2024 · 11 revisions

Events are intended to be used in BroadcastReceiver registered locally or at the application level.

You can register receivers at the application level by adding specific event to intent filter to AndroidManifest.xml:

<receiver android:name=".MyMessageReceiver" android:exported="false">
    <intent-filter>
        <action android:name="org.infobip.mobile.messaging.MESSAGE_RECEIVED"/>
        <!-- register here another event you want to receive updates from -->
    </intent-filter>
</receiver>

or register receivers in you Activity by adding:

class MyActivity : AppCompatActivity() {
    private var isReceiverRegistered = false
    private val messageReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            val message = Message.createFrom(intent.extras)
            //... process your message here
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        registerReceiver()
    }

    override fun onResume() {
        super.onResume()
        registerReceiver()
    }

    override fun onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(messageReceiver)
        isReceiverRegistered = false
        super.onPause()
    }

    private fun registerReceiver() {
        if (isReceiverRegistered) {
            return
        }
        LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver,
                IntentFilter(Event.MESSAGE_RECEIVED.key))
        isReceiverRegistered = true
    }
}
expand to see Java code

public class MyActivity extends AppCompatActivity {

    private boolean isReceiverRegistered;
    private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            Message message = Message.createFrom(intent.getExtras());
            //... process your message here
        }
    };

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver();
    }

    protected void onResume() {
        super.onResume();
        registerReceiver();
    }

    protected void onPause() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(messageReceiver);
        isReceiverRegistered = false;
        super.onPause();
    }

    private void registerReceiver() {
        if (isReceiverRegistered) {
            return;
        }
        LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver,
        new IntentFilter(Event.MESSAGE_RECEIVED.getKey()));
        isReceiverRegistered = true;
    }
}

Event list

Library informs you about following events using Event (core module) class:

Event name Description
TOKEN_RECEIVED Triggered by core module when cloud token is received (invoked each time library checks for token changes). Contains the cloud registration token.
REGISTRATION_CREATED Triggered by core module when cloud registration token successfully stored on the registration server and the registration is created on a server.
INSTALLATION_UPDATED Triggered by core module when installation is updated on a server.
MESSAGE_RECEIVED Triggered by core module when message is received. Contains the received message information.
MESSAGES_SENT Triggered by core module when messages are sent to MO service. Contains information and about each sent message.
API_COMMUNICATION_ERROR Triggered by core module on every error returned by API. Contains the exception information.
DELIVERY_REPORTS_SENT Triggered by core module when message delivery is reported. Contains the list of all reported message IDs.
SEEN_REPORTS_SENT Triggered by core module when message seen is reported. Contains the list of all reported message IDs.
NOTIFICATION_TAPPED Triggered by core module when notification is tapped.
NOTIFICATION_DISPLAYED Triggered by core module when notification is displayed. Silent notifications will never be displayed.
GOOGLE_PLAY_SERVICES_ERROR Triggered when google play services are unavailable on the device.
USER_UPDATED Triggered when user data is updated on the server.
PERSONALIZED Triggered when installation is successfully personalized on a server.
DEPERSONALIZED Triggered when installation is depersonalized.

Following interactive events are being reported using InteractiveEvent (interactive module) class:

InteractiveEvent name Description
NOTIFICATION_ACTION_TAPPED Triggered by core module when notification action is tapped.
MODAL_IN_APP_NOTIFICATION_IS_READY_TO_DISPLAY Triggered by core module when modal in-app notification is ready to be shown.

Notice!

Geofencing feature is DEPRECATED and not supported from SDK version 13.0.3 onwards.

Following geo events are being reported using GeoEvent (geo module) class:

GeoEvent name Description
GEOFENCE_AREA_ENTERED Triggered by geo module when monitored geo area is entered.
GEOFENCE_EVENTS_REPORTED Triggered by geo module when geofence events are reported to the server.

Following chat events are being reported using Event (core module) class:

Event name Parameters Description
MESSAGE_RECEIVED Message.createFrom(intent) Triggered by chat module when the message is received by the SDK.
NOTIFICATION_TAPPED Message.createFrom(intent) Triggered by chat module when notification for particular message is tapped by user.

Examples

TOKEN_RECEIVED

Action name to subscribe to: org.infobip.mobile.messaging.TOKEN_RECEIVED. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val cloudToken = intent.getStringExtra(BroadcastParameter.EXTRA_CLOUD_TOKEN)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    String cloudToken = intent.getStringExtra(BroadcastParameter.EXTRA_CLOUD_TOKEN);
}

REGISTRATION_CREATED

Action name to subscribe to: org.infobip.mobile.messaging.REGISTRATION_CREATED. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context, intent: Intent) {
    val pushRegistrationId = intent.getStringExtra(BroadcastParameter.EXTRA_INFOBIP_ID)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    String pushRegistrationId = intent.getStringExtra(BroadcastParameter.EXTRA_INFOBIP_ID);
}

INSTALLATION_UPDATED

Action name to subscribe to: org.infobip.mobile.messaging.INSTALLATION_UPDATED. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val installation = Installation.createFrom(intent.extras)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    Installation installation = Installation.createFrom(intent.getExtras());
}

MESSAGE_RECEIVED

Action name to subscribe to: org.infobip.mobile.messaging.MESSAGE_RECEIVED. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val message = Message.createFrom(intent.extras)
    val customPayload: JSONObject = message.customPayload
}
expand to see Java code

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

MESSAGES_SENT

Action name to subscribe to: org.infobip.mobile.messaging.MESSAGES_SENT. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val messages = Message.createFrom(intent.getParcelableArrayListExtra(BroadcastParameter.EXTRA_MESSAGES))
    for (message in messages) {
        // ... process message here
    }
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    List<Message> messages = Message.createFrom(intent.<Bundle>getParcelableArrayListExtra(BroadcastParameter.EXTRA_MESSAGES));
    for (Message message : messages) {
        // ... process message here
    }
}

API_COMMUNICATION_ERROR

Action name to subscribe to: org.infobip.mobile.messaging.API_COMMUNICATION_ERROR. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val mobileMessagingError = intent.getSerializableExtra(BroadcastParameter.EXTRA_EXCEPTION) as MobileMessagingError
    val errorCode = mobileMessagingError.code
    val errorMessage = mobileMessagingError.message
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    MobileMessagingError mobileMessagingError = (MobileMessagingError) intent.getSerializableExtra(BroadcastParameter.EXTRA_EXCEPTION);
    String errorCode = mobileMessagingError.getCode();
    String errorMessage = mobileMessagingError.getMessage();
}

DELIVERY_REPORTS_SENT

Action name to subscribe to: org.infobip.mobile.messaging.DELIVERY_REPORTS_SENT. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val messageIDs = intent.getStringArrayExtra(BroadcastParameter.EXTRA_MESSAGE_IDS)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    String[] messageIDs = intent.getStringArrayExtra(BroadcastParameter.EXTRA_MESSAGE_IDS);
}

SEEN_REPORTS_SENT

Action name to subscribe to: org.infobip.mobile.messaging.SEEN_REPORTS_SENT. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val messageIDs = intent.getStringArrayExtra(BroadcastParameter.EXTRA_MESSAGE_IDS)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    String[] messageIDs = intent.getStringArrayExtra(BroadcastParameter.EXTRA_MESSAGE_IDS);
}

GOOGLE_PLAY_SERVICES_ERROR

Action name to subscribe to: org.infobip.mobile.messaging.GOOGLE_PLAY_SERVICES_ERROR. Receiving event data in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val playServicesErrorCode = intent.getIntExtra(BroadcastParameter.EXTRA_PLAY_SERVICES_ERROR_CODE)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    int playServicesErrorCode = intent.getIntExtra(BroadcastParameter.EXTRA_PLAY_SERVICES_ERROR_CODE);
}
You will be able to use this error code to build and error dialog for the error. For more details refer to: https://developers.google.com/android/reference/com/google/android/gms/common/GoogleApiAvailability.html#isUserResolvableError(int)

NOTIFICATION_DISPLAYED

Action name to subscribe to: org.infobip.mobile.messaging.NOTIFICATION_DISPLAYED. Receiving event bundle in BroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {
    Message message = Message.createFrom(intent.getExtras());
    int notificationId = intent.getIntExtra(BroadcastParameter.EXTRA_NOTIFICATION_ID, BroadcastParameter.NOTIFICATION_NOT_DISPLAYED_ID);
}

NOTIFICATION_TAPPED

Action name to subscribe to: org.infobip.mobile.messaging.NOTIFICATION_TAPPED. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val message = Message.createFrom(intent.extras)
}
expand to see Java code

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

NOTIFICATION_ACTION_TAPPED

Action name to subscribe to: org.infobip.mobile.messaging.interactive.NOTIFICATION_ACTION_TAPPED. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val message = Message.createFrom(intent.extras)
    val notificationAction = NotificationAction.createFrom(intent.extras)
    val notificationCategory = NotificationCategory.createFrom(intent.extras)
}
expand to see Java code

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

MODAL_IN_APP_NOTIFICATION_IS_READY_TO_DISPLAY

Action name to subscribe to: org.infobip.mobile.messaging.interactive.MODAL_IN_APP_NOTIFICATION_IS_READY_TO_DISPLAY. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val message = Message.createFrom(intent.extras)
}
expand to see Java code

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

GEOFENCE_AREA_ENTERED

Action name to subscribe to: org.infobip.mobile.messaging.geo.GEOFENCE_AREA_ENTERED. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val geoMessage = GeoMessage.createFrom(intent.extras)
    val geo = geoMessage.geo
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    GeoMessage geoMessage = GeoMessage.createFrom(intent.getExtras());
    Geo geo = geoMessage.getGeo();
}

GEOFENCE_EVENTS_REPORTED

Action name to subscribe to: org.infobip.mobile.messaging.geo.GEOFENCE_EVENTS_REPORTED. Receiving event bundle in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val geoReports = GeoReport.createFrom(intent.extras)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    List<GeoReport> geoReports = GeoReport.createFrom(intent.getExtras());
}

USER_UPDATED

Action name to subscribe to: org.infobip.mobile.messaging.USER_UPDATED. Receiving event data in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val user = User.createFrom(intent.extras)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    User user = User.createFrom(intent.getExtras());
}

PERSONALIZED

Action name to subscribe to: org.infobip.mobile.messaging.PERSONALIZED. Receiving event data in BroadcastReceiver:

override fun onReceive(context: Context?, intent: Intent) {
    val user = User.createFrom(intent.extras)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    User user = User.createFrom(intent.getExtras());
}

DEPERSONALIZED

Action name to subscribe to: org.infobip.mobile.messaging.DEPERSONALIZED. Receiving event data in BroadcastReceiver:

override fun onReceive(context: Context, intent: Intent) {
    val user = User.createFrom(intent.extras)
}
expand to see Java code

@Override
public void onReceive(Context context, Intent intent) {
    User user = User.createFrom(intent.getExtras());
}
Clone this wiki locally