Skip to content

How to retrieve a message in my Activity when user taps on a notification?

Olga Koroleva edited this page Mar 11, 2021 · 2 revisions

In order to retrieve a message inside your activity, you will first have to provide your activity to the library (only if you want to use a non-default activity), and then you will have to handle the intent from the library.

Configuring library

When user taps on a library-generated notification, library will open the default launcher activity of your application. If you want any other activity to be opened on notification tap, then you can provide a callback activity via notification settings:

MobileMessaging.Builder(application)
        .withDisplayNotification(NotificationSettings.Builder(application)
                .withCallbackActivity(MyActivity::class.java)
                .build())
        .build()
expand to see Java code

new MobileMessaging.Builder(getApplication())
        .withDisplayNotification(new NotificationSettings.Builder(getApplication())
                .withCallbackActivity(MyActivity.class)
                .build())
        .build();

Handling intent

In order to receive message inside your Activity, you should also override onNewIntent method:

class MyActivity: Activity() {

    ...
    override fun onNewIntent(intent: Intent) {
        val message = Message.createFrom(intent.getBundleExtra(BroadcastParameter.EXTRA_MESSAGE))
        
    }
    ...
}
expand to see Java code

public class MyActivity extends Activity {

    ...
    @Override
    protected void onNewIntent(Intent intent) {
        Message message = Message.createFrom(intent.getBundleExtra(BroadcastParameter.EXTRA_MESSAGE));

    }
    ...
}
Clone this wiki locally