localNotification, event listeners,and in-app alerts

You must Login before you can answer or comment on any questions.

I have an app that registers localNotifications on backgrounding, which works as expected. These notifications are set to display at a certain date and time.

Since I have NEVER been able to catch notification events while my app is running (I have tried so many different pieces of code on so many occasions I have lost count, both in app.js and bg.js), I worked around this by creating a couple functions to display alerts if the scheduled events occur while the app is open.

My issue now is that if I have the app in the background, and the localNotification displays, if I click on "View" to relaunch the app, the in-app alert displays immediately on resume, so there are two "alerts" in total. Not the ideal user experience.

I'm looking for some way to 1) finally catch localNotifications while the app is running so I don't have to display regular alerts in the first place or 2) find some way to tell if the app was resumed from a notification, so I can remove the alert in that case.

Here's my code:

APP.JS (just the bit with backgroundService and in-app alert workaround)

// ===== REGISTER BACKGROUND SERVICE FOR NOTIFICATIONS =====
// Includes a file for scheduling notifications based on DB entries.
var backgroundService = Ti.App.iOS.registerBackgroundService({
    url:'bg.js'
});
 
 
// ===== DISPLAYS ALERTS IF IN APP =====
function checkTimesInApp() {
    var currentTime = new Date();
    currentTime.setSeconds(00);
 
    // get a list of all saved items
    var rows = db.execute('SELECT * FROM SAVEDITEMS');
    // loop through all items
    while (rows.isValidRow()) {
        var TimeToCheck = new Date(rows.fieldByName('itemDateTime'));
        if (Date.parse(currentTime)==Date.parse(TimeToCheck)) {
            var alertDialog = Titanium.UI.createAlertDialog({ 
                title: 'My App',
                message: rows.fieldByName('itemContent'),
                buttonNames: ['OK']
            });
            alertDialog.show();
            alertSound.play();
        }
        rows.next();
    }
    // check every minute
    setTimeout(checkTimesInApp, 60000);
}
 
// We only want to run CheckTimesInApp on the minute.
function runNextMinute() {
    var theDateTime = new Date();
    var currentSeconds = theDateTime.getSeconds();
 
    if (currentSeconds==00) {
        checkTimesInApp();
    } else {
        setTimeout(runNextMinute, 1000)
    }
}
// run once at app load to start things going.
runNextMinute();
BG.JS (the whole darn thing)
var db = Titanium.Database.open('mydb');
// cancel all existing notifications before resetting them
Ti.App.iOS.cancelAllLocalNotifications;
// get a list of all saved items
var rows = db.execute('SELECT * FROM SAVEDITEMS');
// loop through all items
var currentDate = new Date();
var notifications = [];
var n = 0;
while (rows.isValidRow()) {
 
    // only schedule if date is in the future.
    if (Date.parse(rows.fieldByName('itemDateTime')) > Date.parse(currentDate)) {
        // schedule the notification using row data for arguments.
        notifications[n] = Ti.App.iOS.scheduleLocalNotification({
            alertBody: rows.fieldByName('itemContent'),
            userInfo:{},
            sound:"doodeet.caf",
            date: new Date(rows.fieldByName('itemDateTime')),
            repeat: rows.fieldByName('itemRepeat')
        });
        n++;
    }
    rows.next();
}

Your Answer

Think you can help? Login to answer this question!