Search code examples
android-notificationsandroid-mediaplayerexoplayerandroid-mediasessionandroid-media3

Android Media3: App launched from tapping media notification but extras from PendingIntent are not set


I would like to take the user directly to the player UI when the user taps on media notification. I see that app is launched (or brought to foreground) but intent extras are null and I cannot take the user to the player screen.

In MediaSessionService onCreate() I have

      val intent = packageManager.getLaunchIntentForPackage(packageName)
      intent?.putExtra("DESTINATION", "player")
      val pendingIntent = PendingIntent.getActivity(this, 0, intent, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)

      mediaSession = MediaSession.Builder(this, player)
          .setSessionActivity(pendingIntent)
          .build()

And in the MainActivity onResume/onStart/onNewIntent I see that the intent extras are not set.

    override fun onStart() {
        super.onStart()
        Timber.e("onStart: intents: ${intent.extras?.size()}") // <--- this is 0
    }

This is what I have in manifest.

      <!-- in app gradle module -->
      <activity
          android:name=".MainActivity"
          android:exported="true">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
      </activity>

      <!-- in UI gradle module -->
      <service
          android:name="com.scribd.app.ui.service.PlaybackService"
          android:foregroundServiceType="mediaPlayback"
          android:exported="true">
          <intent-filter>
              <action android:name="androidx.media3.session.MediaSessionService"/>
          </intent-filter>
      </service>

I am testing this in API level 34.

What am I missing? Also, I have MainActivity and MediaSessionService in different gradle modules but I suppose manifests are merged anyway.


Solution

  • The problem, apparently, was with packageManager.getLaunchIntentForPackage(packageName). Though it correctly identified the MainActivity, intent extras were missing possibly due to the fact that activity is in a different gradle module. When I moved the activity in the same module as the service, I started seeing the extra values. Not sure about the root cause but at least this is the solution I arrived at.