App crashes when opened from notification


#1

I am developing an Android application which sends the users a daily notification at a by the user selected time. To do this I used the local notifications plugin.
In another part of the app I set the Android header and footer colour. To do this I use the following code:

var window = app.android.startActivity.getWindow();
window.setStatusBarColor(androidColor.android);
window.setNavigationBarColor(androidColor.android);

To set the notifications I use the following code:

notificationHandler.schedule([{
            id: 1,
            title: 'Reality Keeper',
            body: 'Vergeet niet je vragen in te vullen!',
            interval: 'day',
            at: date
        }]).then(() => {
            console.log("Notification scheduled");
        }, (error) => {
            console.log("ERROR", error);
        });

However when I open the app from a notification I get the following error:

TypeError: Cannot read property getWindow of undefined
File: file:///data/data/org.nativescript.RealityKeeper/files/app/commonlogic.js, line: 35, column: 50

StackTrace: 
	Frame: function:setAndroidBackground, file:file:///data/data/org.nativescript.RealityKeeper/files/app/commonlogic.js, line: 35, column: 51

It looks like the app doesn’t start properly and causes the android.startactivity to fail.
This is a breaking bug for the app. Does anybody know how to fix this?

It would be acceptable to start the app from scratch when opened from a notification. I however don’t know how to do this.

Thank you in advance!


#2

I guess you are trying to use startActivity even before it’s available. Try to add listeners to activity events, wait for it to be created and ready.


#3

You’re a hero, it works!
However now it doesn’t change the header color when the application is opened from the notification. Any idea what even I’d need to listen to?

Currently I use these:

launchListener = app.on(app.launchEvent, (args) => {
    updateAndroidBackground();
});

resumeListener = app.on(app.resumeEvent, (args) => {
    updateAndroidBackground();
});

function updateAndroidBackground(){
    if (androidVersionSufficient()) {
        var window = app.android.startActivity.getWindow();
        window.setStatusBarColor(androidColor.android);
        window.setNavigationBarColor(androidColor.android);
    }
}

#4

Not sure though you may try setting a timeout before changing status bar color, to make sure it’s not overwritten by some other code in framework / android.