2

I'm building an App with Cordova and right now I'm focussed on a Background Service. I need that Service for Notifcations and to update them regularly, which is working quite well already. When a user clicks on the notification, it should open the app, just like with Spotify for example. Now with a standalone App, I would usually just do it like this:

Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(this,0, 
intent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, myChannelID)
.setContentIntent(contentIntent);

And then build the notifcation and so on. The problem is, that I am not able to access the Cordova Activity from within the service. I've tried something like this:

Intent intent = new Intent(this, MyPlugin.class);

But as the Plugin does not extend activity (it extends CordovaPlugin) this does not seem to work. I can access the activity from within the Plugin, but I can't seem to be able to pass the activity to the service. Any help would be appreciated :).

Daniel
  • 53
  • 6

1 Answers1

1

You could expose your plugin instance as a public class property and hence access the Cordova Activity from it:

public class MyPlugin extends CordovaPlugin{
    public static MyPlugin instance = null;
    static CordovaWebView cordovaWebView;
    static CordovaInterface cordovaInterface;

    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);

        instance = this;
        cordovaWebView = webView;
        cordovaInterface = cordova;
    }

    @Override
    public void onDestroy() {
        instance = null;
    }
}

Then your service can reference it by that public static property, something like this:

public class MyService {
    public static void myMethod(){
        if(MyPlugin.instance != null){
            Activity cordovaActivity = MyPlugin.instance.cordovaInterface.getActivity();
            Context applicationContext = cordovaActivity.getApplicationContext();

            Intent intent = new Intent(applicationContext, cordovaActivity.getClass());
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            PendingIntent contentIntent = PendingIntent.getActivity(applicationContext,0, intent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(applicationContext, myChannelID).setContentIntent(contentIntent);
        }
    }
}

Be sure to add <param name="onload" value="true" /> to your <feature> definition in your plugin.xml to ensure that initialize() is called on app startup (see Plugin Initialization and Lifetime):

<feature name="MyPlugin">
    <param name="android-package" value="cordova.plugin.MyPlugin" />
    <param name="onload" value="true" />
</feature>
DaveAlden
  • 30,083
  • 11
  • 93
  • 155