Launch main Android activity?


#1

This is going to sound utterly noobish, but I’m having a hard time converting this code to TS:

startActivity(new Intent(this, MainActivity.class));

If I can get to a specific route within Angular, even better, but I’ll settle for just launching the NativeScript main activity for now! (I’m trying to launch the app when a certain push notification comes in)


#2

Where exactly you are going to write this launch code?
Are you using Firebase and the custom messaging service?


#3

Yes, in the message received callback, as suggested in:


#4

Anybody? I’ll try to figure it out in the meanwhile, but any help would be appreciated. I am not sure how to represent “MainActivity.class” in Javascript.


#5

I’m not sure if this works, give a try to application.android.startActivity.getClass().


#6

It works if the app is in the background, but it’s undefined when the app is in a terminated state. I tried to access com.tns.NativeScriptActivity.getClass() but that didn’t work either.


#7

Then you will have to use fully qualified package name get the launch activity.


#8

Awesome. I got it mostly working, so I’ll paste it here to help others.

private launchBackgroundedApp() {
        if (application.android) {
            const startActivity: android.app.Activity = application.android.startActivity;
            const context: android.content.Context = application.android.context;
            let intent: android.content.Intent;
            
            if (startActivity) {
                intent = new android.content.Intent(context, startActivity.getClass());
                intent.addFlags(android.content.Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                const pendingIntent = android.app.PendingIntent.getActivity(context, 0, intent, 0);
                pendingIntent.send(context, 0, intent);
            } else {
                console.log("App is in a terminated state, relaunching...");

                const pm = context.getPackageManager();
                const packageName = "com.xxxx.xxxx";
                intent = pm.getLaunchIntentForPackage(packageName);

                if (intent) {
                    intent.addFlags(android.content.Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    context.startActivity(intent);
                }
                else
                    console.log("Intent could not be created for", packageName);
            }
        }
    }

It works perfectly if the app is running in the background. If the user swipes the app away, it will relaunch the app, but I’m thinking it creates a different instance of it, even with android:launchMode="singleInstance" set in the Android manifest. Because if I do:

this.launchBackgroundedApp();
this.routerExtensions.navigate(['/alert']);

It won’t navigate in the case of the latter launch.


#9

Figured it out. I extended the NativeScript activity and in the onCreate method accessed an extra (serialized string) that I injected via intent.putExtra(...) (before calling startActivity). Then, I wrote a singleton / static class to update some initial data onCreate, at which point I dynamically updated Angular’s starting route (so that it launches right into my alert page depending on the state of the initial data). :grinning: