Rob Laverty Rob Laverty - 3 months ago 34
TypeScript Question

How to Extend Application Class on Android with Angular nativeScriptBootstrap


I'm trying to implement OneSignal's Push Notification SDK on android and in order to do so, I need to Extend Application Class and call
OneSignal.startInit
in the
onCreate
method.


I've been following the guides posted in the documentation and the
Android Extend Sample App
. Neither of these are working at all.

Trying to build the
Android Extend Sample App
results in
Java ID 0 not found
error.

I'm also exploring the possibility of
export class AppComponent extends android.app.Application
which I'm having no luck with so far.

Any tips would greatly be appreciated!

Answer

There are two ways to achieve this the later being the complicated one

Place the following in your main.ts

var application = require("application");
application.on(application.launchEvent, function (args) {
    if (args.android) {
        // For Android applications
    com.onesignal.OneSignal.startInit(application.android.context).init();
    } else if (args.ios !== undefined) {
        // For iOS applications, args.ios
    }
});

Create a new JavaScript file in your app folder - name it application.android.js

var application = require("application");
var superProto = android.app.Application.prototype;
var Application = android.app.Application.extend("org.myApp.Application", {
    onCreate: function () {
        superProto.onCreate.call(this);
        application.android.init(this);
        com.onesignal.OneSignal.startInit(this).init();
    }
});

Modify the application entry within the AndroidManifest.xml file found in the <application-name>app/App_Resources/Android/ folder

<application
        android:name="org.myApp.Application"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >