Skip to content

Dealing with user tapping on notification

Android

The default behavior when tapping a notification is opening a URI. If you want a different action, then you have to write your own BroadcastReceiver.

An example of a custom receiver which starts the NotificationOpenActivity (see our example project):

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.plotprojects.retail.android.FilterableNotification;

public class MyNotificationOpenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        FilterableNotification notification = intent.getParcelableExtra("notification");
        if (notification.getData() == null)
            return;

        Intent openIntent = new Intent(context, NotificationOpenActivity.class);
        openIntent.setAction(NotificationOpenActivity.ACTION);
        openIntent.putExtras(intent.getExtras());
        openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

        context.startActivity(openIntent);
    }
}

And to add this receiver to the manifest instead of the default one:

<receiver android:exported="false" android:name="com.example.MyNotificationOpenReceiver">
  <intent-filter>
    <action android:name="${applicationId}.plot.OpenNotification" />
  </intent-filter>
</receiver>