I am writing an audio player type app with Ionic and Capacitor, everything is fine.
Setup:
- My application is playing audio on Android.
- I have added foreground service to my plugin (required by Android to play audio if you leave the app, so you can show a notification to start/stop).
- This is all fine.
Problem:
- What isn’t working is getting access to the
MainActivity
fromapp
from within modulecapacitor-plugin-remote-audio.MyForegroundService
in the project. Is this even possible?
Illustrated screenshot to show the problem:
Code block that's relevant
/* Used to build and start foreground service. */
public void startForegroundService()
{
Log.d(TAG, "Start foreground service.");
// Create notification default intent to open the MainActivity from Capacitor app when tapped.
Intent intent = new Intent(this, "What goes here to get the MainActivity");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainAppIntent, 0);
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setOngoing(true)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setShowWhen(false)
.setSmallIcon(android.R.drawable.ic_media_play) // TODO: fix this to use the app icon.
.setContentTitle("Content Title");
if (mainAppPendingIntent != null) {
builder.setContentIntent(mainAppPendingIntent);
}
addPlayAndPauseButtons(builder);
startForeground(1, builder.build());
}