Skip to content

Conversation

zeevenn
Copy link

@zeevenn zeevenn commented Jul 6, 2025

Notification Click Behavior Feature

This feature adds configurable notification click behavior to react-native-track-player for Android.

Problem

Previously, when users clicked on the notification from react-native-track-player, the app would always launch with a hardcoded URI (trackplayer://notification.click) that allows the app to identify that it was opened from a notification click. This behavior was inflexible and didn't provide options for different use cases.

#2433

And in expo case, expo-router will handle the deep link and navigate to an unmatched route. User must add a notification.click.tsx to redirect certain page. But sometimes user just want to open app when click notification other than navigate to certain page.

Solution

Added a new notificationClickBehavior configuration option under android options that allows developers to:

  1. Disable URI data: Launch app without any URI information
  2. Use custom URI: Replace the default URI with a custom one
  3. Use custom action: Replace the default action with a custom one
  4. Complete customization: Combine custom URI and action

@zeevenn zeevenn requested review from jspizziri and dcvz as code owners July 6, 2025 04:44
@zeevenn
Copy link
Author

zeevenn commented Jul 7, 2025

@jspizziri Can you take a look, thanks. I think this is very helpful.

@farooqdotdev
Copy link

farooqdotdev commented Sep 9, 2025

From this Expo page, they described how to handle incoming native links.

So, I implemented the code below in app/+native-intent.tsx file and it worked.

import { appScheme } from "@/features-users/auth/utils";

/**
 * Handles deep link redirection for your app, including third-party schemes like TrackPlayer notifications.
 * 
 * @param path - The incoming URL or path to process
 * @param initial - Whether this is the initial URL (cold start) or a hot-start URL
 * @returns A string representing the path to redirect to within your app
 */
export function redirectSystemPath( { path, initial }: { path: string; initial: boolean } ): string {
  try {
    console.log( "redirectSystemPath called with:", { path, initial } );

    // Construct a URL object. The base is your app scheme in case the path is relative.
    const url = new URL( path, `${appScheme}://` );

    // Handle TrackPlayer notification click
    // trackplayer://notification.click → url.host === "notification.click"
    if ( url.host === "notification.click" ) {
      console.log( "Redirecting to /player from TrackPlayer notification" );
      return "/player";
    }

    // No special rule matched → return the original path for normal routing
    console.log( "No redirection rule matched, returning original path:", path );
    return path;
  } catch ( err ) {
    // If parsing fails, log the error and redirect safely to home
    console.warn( "Error parsing URL in redirectSystemPath:", { path, initial, error: err } );
    return "/";
  }
}

Perhaps, someone can add this to the examples in TrackPlayer page.

@zeevenn
Copy link
Author

zeevenn commented Sep 10, 2025

It seems will redirect to a certain route. I will try it later.

@zeevenn
Copy link
Author

zeevenn commented Sep 12, 2025

I tried it again, and +native-intent.tsx only redirects the route, but I just want to call the app. I will resolve these conflicts later so that I can merge this feature.

@zeevenn zeevenn force-pushed the feat/notification-click branch from efc9531 to ff030d8 Compare September 12, 2025 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants