1

After struggling for a few hours, I hope I can find someone who understands this. Nativescript offers support for Application events for Angular + Typescript:

http://docs.nativescript.org/angular/core-concepts/application-management.html

However, I do not understand where I should add these. The example from the website shows the following code:

import application = require("application");
application.on(application.launchEvent, function (args: application.ApplicationEventData) {
    if (args.android) {
        // For Android applications, args.android is an android.content.Intent class.
        console.log("Launched Android application with the following intent: " + args.android + ".");
    } else if (args.ios !== undefined) {
        // For iOS applications, args.ios is NSDictionary (launchOptions).
        console.log("Launched iOS application with options: " + args.ios);
    }
});

...

application.start({ moduleName: "main-page" });

How can I use this within my Angular application? To me it feels like this isn't actually code for the Angular version, but perhaps I'm missing something.

I have tried importing the application and adding it the ngOnInit of my main AppComponent:

ngOnInit() {
    application.on(application.launchEvent, function (args: application.ApplicationEventData) {
        if (args.android) {
            // For Android applications, args.android is an android.content.Intent class.
            console.log("Alert something"); // : " + args.android + ".");
        } else if (args.ios !== undefined) {
            // For iOS applications, args.ios is NSDictionary (launchOptions).
            console.log("Launched iOS application with options: " + args.ios);
        }
        console.log('launch detected')
    });

    console.log('application initiated');
}

Although I do not get any errors, I only get to see the application initiated message. Any idea on how I can use the mentioned android args?

Update: In the end it was much easier than expected, please see the code I used to fix this:

import app = require('application');
import "reflect-metadata";
import {nativeScriptBootstrap} from "nativescript-angular/application";
import {AppComponent} from "./app.component";

app.on(app.launchEvent, function (args: app.ApplicationEventData) {
    // on launch code
});

nativeScriptBootstrap(AppComponent);

Thank you for your help!

1 Answer 1

1

You need add application event functions to main.ts or app.ts file and before line with Bootstrap

import app = require('application');
import "reflect-metadata";
import {nativeScriptBootstrap} from "nativescript-angular/application";
import {AppComponent} from "./app.component";

app.on(app.launchEvent, function (args: app.ApplicationEventData) {
    // on launch code
});

nativeScriptBootstrap(AppComponent);
Sign up to request clarification or add additional context in comments.

2 Comments

There is no application.start in my angular project, all I see is: nativeScriptBootstrap(AppComponent);
I'll mark this answer as the right answer, because in the end it helped me solve it. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.