All Collections
FAQ
[Android ] Handling deeplinks sent via FCM
[Android ] Handling deeplinks sent via FCM

To handle deep links from push notifications sent via FCM in your Android app, follow these steps

M
Written by Marat Zhanabekov
Updated over a week ago

1. Update the AndroidManifest.xml

First, you need to declare the intent filter for deep linking in your AndroidManifest.xml for the activity that should handle the deep link:

<activity android:name=".YourDeepLinkActivity">
<!-- Intent filter for handling deep links -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="<your_app_url_schema>" />
</intent-filter>
</activity>

2. Handle the Deep Link in the Activity

In the activity that handles the deep link (YourDeepLinkActivity in this case), you can retrieve the deeplink data:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_your_deeplink);

Intent intent = getIntent();
Uri data = intent.getData();
if (data != null) {
// Handle the deep link. For example:
String path = data.getPath();
// Do something with the path or other parts of the URI
}
}

3. Handle the FCM Push Notification

To handle the FCM push notification and extract the deep link, you can use the FirebaseMessagingService:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if the message contains data payload
if (remoteMessage.getData().size() > 0) {
String deepLink = remoteMessage.getData().get("deeplink");
if (deepLink != null) {
// Handle the deep link
handleDeepLink(deepLink);
}
}
}

private void handleDeepLink(String deepLink) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(deepLink));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}

Make sure you've declared this service in your AndroidManifest.xml:

<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>

4. Ensure Firebase Setup

Ensure you've set up Firebase Cloud Messaging correctly in your app. This includes adding the necessary dependencies in your build.gradle file and initializing Firebase in your app.

That's it! With these steps, your app should be able to handle deep links sent via FCM.

Did this answer your question?